MCPcopy Index your code
hub / github.com/RustPython/RustPython / collect

Function collect

crates/vm/src/stdlib/gc.rs:50–80  ·  view source on GitHub ↗
(args: CollectArgs, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

48
49 #[pyfunction]
50 fn collect(args: CollectArgs, vm: &VirtualMachine) -> PyResult<i32> {
51 let generation = args.generation;
52 let generation_num = generation.unwrap_or(2);
53 if !(0..=2).contains(&generation_num) {
54 return Err(vm.new_value_error("invalid generation"));
55 }
56
57 // Invoke callbacks with "start" phase
58 invoke_callbacks(vm, "start", generation_num as usize, &Default::default());
59
60 // Manual gc.collect() should run even if GC is disabled
61 let gc = gc_state::gc_state();
62 let result = gc.collect_force(generation_num as usize);
63
64 // Move objects from gc_state.garbage to vm.ctx.gc_garbage (for DEBUG_SAVEALL)
65 {
66 let mut state_garbage = gc.garbage.lock();
67 if !state_garbage.is_empty() {
68 let py_garbage = &vm.ctx.gc_garbage;
69 let mut garbage_vec = py_garbage.borrow_vec_mut();
70 for obj in state_garbage.drain(..) {
71 garbage_vec.push(obj);
72 }
73 }
74 }
75
76 // Invoke callbacks with "stop" phase
77 invoke_callbacks(vm, "stop", generation_num as usize, &result);
78
79 Ok((result.collected + result.uncollectable) as i32)
80 }
81
82 /// Return the current collection thresholds as a tuple.
83 #[pyfunction]

Callers

nothing calls this directly

Calls 10

invoke_callbacksFunction · 0.85
gc_stateFunction · 0.85
collect_forceMethod · 0.80
borrow_vec_mutMethod · 0.80
ErrClass · 0.50
containsMethod · 0.45
lockMethod · 0.45
is_emptyMethod · 0.45
drainMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected