update gc

This commit is contained in:
blueloveTH 2023-03-29 13:38:01 +08:00
parent 9634e5c402
commit 78b73998da
7 changed files with 24 additions and 11 deletions

View File

@ -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)
{

View File

@ -29,7 +29,7 @@
#include <list>
#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

View File

@ -7,11 +7,13 @@
namespace pkpy {
struct ManagedHeap{
std::vector<PyObject*> gen;
int counter = 0;
template<typename T>
PyObject* gcnew(Type type, T&& val){
PyObject* obj = new Py_<std::decay_t<T>>(type, std::forward<T>(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();

View File

@ -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<T>(_value); // handle PyObject* inside _value `T`
}
};

View File

@ -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"}){

View File

@ -168,12 +168,12 @@ inline void Frame::try_deref(VM* vm, PyObject*& v){
/***** GC's Impl *****/
template<> inline void _mark<AttrRef>(AttrRef& t){
OBJ_MARK(obj);
OBJ_MARK(t.obj);
}
template<> inline void _mark<IndexRef>(IndexRef& t){
OBJ_MARK(obj);
OBJ_MARK(index);
OBJ_MARK(t.obj);
OBJ_MARK(t.index);
}
template<> inline void _mark<TupleRef>(TupleRef& t){

View File

@ -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>(Type(1), Type(0)), .base = -1, .name = "object"});
_all_types.push_back({.obj = heap._new<Type>(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();
}