Skip to content

Commit 000aa85

Browse files
committed
feat: run gc per frame
1 parent 0aa925f commit 000aa85

File tree

3 files changed

+80
-4
lines changed

3 files changed

+80
-4
lines changed

Cargo.lock

Lines changed: 59 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

stdlib/src/gc.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,25 @@ mod gc {
66

77
#[pyfunction]
88
fn collect(_args: FuncArgs, _vm: &VirtualMachine) -> i32 {
9-
0
9+
let ret: usize = rustpython_vm::object::gc::collect().into();
10+
ret as i32
1011
}
1112

1213
#[pyfunction]
1314
fn isenabled(_args: FuncArgs, _vm: &VirtualMachine) -> bool {
14-
false
15+
rustpython_vm::object::gc::isenabled()
1516
}
1617

1718
#[pyfunction]
1819
fn enable(_args: FuncArgs, vm: &VirtualMachine) -> PyResult {
19-
Err(vm.new_not_implemented_error("".to_owned()))
20+
rustpython_vm::object::gc::enable();
21+
Ok(vm.new_pyobj(true))
2022
}
2123

2224
#[pyfunction]
2325
fn disable(_args: FuncArgs, vm: &VirtualMachine) -> PyResult {
24-
Err(vm.new_not_implemented_error("".to_owned()))
26+
rustpython_vm::object::gc::disable();
27+
Ok(vm.new_pyobj(true))
2528
}
2629

2730
#[pyfunction]

vm/src/frame.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use crate::common::{boxvec::BoxVec, lock::PyMutex};
2+
#[cfg(feature = "gc")]
3+
use crate::object::gc::GLOBAL_COLLECTOR;
24
use crate::{
35
builtins::{
46
asyncgenerator::PyAsyncGenWrappedValue,
@@ -349,6 +351,18 @@ impl ExecutingFrame<'_> {
349351
self.update_lasti(|i| *i += 1);
350352
let instr = &instrs[idx];
351353
let result = self.execute_instruction(instr, vm);
354+
if matches!(instr, &bytecode::Instruction::ReturnValue) {
355+
// only do gc if after certain instruction(to be decided), so to avoid strange bugs?
356+
// it seems ReturnValue is safe enough & frequent enough to do gc() after execute it?
357+
// &bytecode::Instruction::ReturnValue | &bytecode::Instruction::StoreLocal(..)
358+
#[cfg(feature = "gc")]
359+
{
360+
#[cfg(feature = "threading")]
361+
GLOBAL_COLLECTOR.gc();
362+
#[cfg(not(feature = "threading"))]
363+
GLOBAL_COLLECTOR.with(|v| v.gc());
364+
}
365+
}
352366
match result {
353367
Ok(None) => continue,
354368
Ok(Some(value)) => {

0 commit comments

Comments
 (0)