From 78b73998daadfab02a92091ae0c4fdeac07dc21e Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Wed, 29 Mar 2023 13:38:01 +0800 Subject: [PATCH] update gc --- src/ceval.h | 2 ++ src/common.h | 2 +- src/gc.h | 9 +++++++++ src/obj.h | 3 ++- src/pocketpy.h | 2 +- src/ref.h | 6 +++--- src/vm.h | 11 ++++++----- 7 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/ceval.h b/src/ceval.h index b1f934e6..51033f99 100644 --- a/src/ceval.h +++ b/src/ceval.h @@ -7,6 +7,8 @@ namespace pkpy{ inline PyObject* VM::run_frame(Frame* frame){ while(frame->has_next_bytecode()){ + heap._auto_collect(this); + const Bytecode& byte = frame->next_bytecode(); switch (byte.op) { diff --git a/src/common.h b/src/common.h index 06530277..c9e86185 100644 --- a/src/common.h +++ b/src/common.h @@ -29,7 +29,7 @@ #include #define PK_VERSION "0.9.5" -#define PK_EXTRA_CHECK 0 +#define PK_EXTRA_CHECK 1 #if (defined(__ANDROID__) && __ANDROID_API__ <= 22) || defined(__EMSCRIPTEN__) #define PK_ENABLE_FILEIO 0 diff --git a/src/gc.h b/src/gc.h index 3fb5b76b..a897ff8f 100644 --- a/src/gc.h +++ b/src/gc.h @@ -7,11 +7,13 @@ namespace pkpy { struct ManagedHeap{ std::vector gen; + int counter = 0; template PyObject* gcnew(Type type, T&& val){ PyObject* obj = new Py_>(type, std::forward(val)); gen.push_back(obj); + counter++; return obj; } @@ -36,6 +38,13 @@ struct ManagedHeap{ return freed; } + void _auto_collect(VM* vm){ + if(counter > 1000){ + counter = 0; + collect(vm); + } + } + int collect(VM* vm){ mark(vm); return sweep(); diff --git a/src/obj.h b/src/obj.h index 634debbe..b65e3215 100644 --- a/src/obj.h +++ b/src/obj.h @@ -135,8 +135,9 @@ struct Py_ : PyObject { void _mark() override { if(gc.marked) return; + // std::cout << "marking " << type << std::endl; gc.marked = true; - if(is_attr_valid()) attr()._mark(); + if(_attr != nullptr) _attr->_mark(); pkpy::_mark(_value); // handle PyObject* inside _value `T` } }; diff --git a/src/pocketpy.h b/src/pocketpy.h index a036a8bd..78442bab 100644 --- a/src/pocketpy.h +++ b/src/pocketpy.h @@ -771,7 +771,7 @@ inline void VM::post_init(){ add_module_random(this); add_module_io(this); add_module_os(this); - add_module_c(this); + // add_module_c(this); add_module_gc(this); for(const char* name: {"this", "functools", "collections", "heapq", "bisect"}){ diff --git a/src/ref.h b/src/ref.h index 8129b316..8026929e 100644 --- a/src/ref.h +++ b/src/ref.h @@ -168,12 +168,12 @@ inline void Frame::try_deref(VM* vm, PyObject*& v){ /***** GC's Impl *****/ template<> inline void _mark(AttrRef& t){ - OBJ_MARK(obj); + OBJ_MARK(t.obj); } template<> inline void _mark(IndexRef& t){ - OBJ_MARK(obj); - OBJ_MARK(index); + OBJ_MARK(t.obj); + OBJ_MARK(t.index); } template<> inline void _mark(TupleRef& t){ diff --git a/src/vm.h b/src/vm.h index 1e8c02ce..a49ea8e2 100644 --- a/src/vm.h +++ b/src/vm.h @@ -650,9 +650,6 @@ inline Str VM::disassemble(CodeObject_ co){ } inline void VM::init_builtin_types(){ - // PyTypeObject is managed by _all_types - // PyModuleObject is managed by _modules - // They are not managed by GC, so we use a simple "new" _all_types.push_back({.obj = heap._new(Type(1), Type(0)), .base = -1, .name = "object"}); _all_types.push_back({.obj = heap._new(Type(1), Type(1)), .base = 0, .name = "type"}); tp_object = 0; tp_type = 1; @@ -699,7 +696,10 @@ inline void VM::init_builtin_types(){ builtins->attr().set("range", _t(tp_range)); post_init(); - for(auto& t: _all_types) t.obj->attr()._try_perfect_rehash(); + for(int i=0; i<_all_types.size(); i++){ + // std::cout << i << ": " << _all_types[i].name << std::endl; + _all_types[i].obj->attr()._try_perfect_rehash(); + } for(auto [k, v]: _modules.items()) v->attr()._try_perfect_rehash(); } @@ -922,7 +922,8 @@ inline PyObject* VM::_exec(){ } inline void ManagedHeap::mark(VM *vm) { - // iterate callstack frames + vm->_modules._mark(); + for(auto& t: vm->_all_types) t.obj->_mark(); for(auto& frame : vm->callstack.data()){ frame->_mark(); }