mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-23 04:50:17 +00:00
update gc
This commit is contained in:
parent
9634e5c402
commit
78b73998da
@ -7,6 +7,8 @@ namespace pkpy{
|
|||||||
|
|
||||||
inline PyObject* VM::run_frame(Frame* frame){
|
inline PyObject* VM::run_frame(Frame* frame){
|
||||||
while(frame->has_next_bytecode()){
|
while(frame->has_next_bytecode()){
|
||||||
|
heap._auto_collect(this);
|
||||||
|
|
||||||
const Bytecode& byte = frame->next_bytecode();
|
const Bytecode& byte = frame->next_bytecode();
|
||||||
switch (byte.op)
|
switch (byte.op)
|
||||||
{
|
{
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
#include <list>
|
#include <list>
|
||||||
|
|
||||||
#define PK_VERSION "0.9.5"
|
#define PK_VERSION "0.9.5"
|
||||||
#define PK_EXTRA_CHECK 0
|
#define PK_EXTRA_CHECK 1
|
||||||
|
|
||||||
#if (defined(__ANDROID__) && __ANDROID_API__ <= 22) || defined(__EMSCRIPTEN__)
|
#if (defined(__ANDROID__) && __ANDROID_API__ <= 22) || defined(__EMSCRIPTEN__)
|
||||||
#define PK_ENABLE_FILEIO 0
|
#define PK_ENABLE_FILEIO 0
|
||||||
|
9
src/gc.h
9
src/gc.h
@ -7,11 +7,13 @@
|
|||||||
namespace pkpy {
|
namespace pkpy {
|
||||||
struct ManagedHeap{
|
struct ManagedHeap{
|
||||||
std::vector<PyObject*> gen;
|
std::vector<PyObject*> gen;
|
||||||
|
int counter = 0;
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
PyObject* gcnew(Type type, T&& val){
|
PyObject* gcnew(Type type, T&& val){
|
||||||
PyObject* obj = new Py_<std::decay_t<T>>(type, std::forward<T>(val));
|
PyObject* obj = new Py_<std::decay_t<T>>(type, std::forward<T>(val));
|
||||||
gen.push_back(obj);
|
gen.push_back(obj);
|
||||||
|
counter++;
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -36,6 +38,13 @@ struct ManagedHeap{
|
|||||||
return freed;
|
return freed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _auto_collect(VM* vm){
|
||||||
|
if(counter > 1000){
|
||||||
|
counter = 0;
|
||||||
|
collect(vm);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int collect(VM* vm){
|
int collect(VM* vm){
|
||||||
mark(vm);
|
mark(vm);
|
||||||
return sweep();
|
return sweep();
|
||||||
|
@ -135,8 +135,9 @@ struct Py_ : PyObject {
|
|||||||
|
|
||||||
void _mark() override {
|
void _mark() override {
|
||||||
if(gc.marked) return;
|
if(gc.marked) return;
|
||||||
|
// std::cout << "marking " << type << std::endl;
|
||||||
gc.marked = true;
|
gc.marked = true;
|
||||||
if(is_attr_valid()) attr()._mark();
|
if(_attr != nullptr) _attr->_mark();
|
||||||
pkpy::_mark<T>(_value); // handle PyObject* inside _value `T`
|
pkpy::_mark<T>(_value); // handle PyObject* inside _value `T`
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -771,7 +771,7 @@ inline void VM::post_init(){
|
|||||||
add_module_random(this);
|
add_module_random(this);
|
||||||
add_module_io(this);
|
add_module_io(this);
|
||||||
add_module_os(this);
|
add_module_os(this);
|
||||||
add_module_c(this);
|
// add_module_c(this);
|
||||||
add_module_gc(this);
|
add_module_gc(this);
|
||||||
|
|
||||||
for(const char* name: {"this", "functools", "collections", "heapq", "bisect"}){
|
for(const char* name: {"this", "functools", "collections", "heapq", "bisect"}){
|
||||||
|
@ -168,12 +168,12 @@ inline void Frame::try_deref(VM* vm, PyObject*& v){
|
|||||||
|
|
||||||
/***** GC's Impl *****/
|
/***** GC's Impl *****/
|
||||||
template<> inline void _mark<AttrRef>(AttrRef& t){
|
template<> inline void _mark<AttrRef>(AttrRef& t){
|
||||||
OBJ_MARK(obj);
|
OBJ_MARK(t.obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<> inline void _mark<IndexRef>(IndexRef& t){
|
template<> inline void _mark<IndexRef>(IndexRef& t){
|
||||||
OBJ_MARK(obj);
|
OBJ_MARK(t.obj);
|
||||||
OBJ_MARK(index);
|
OBJ_MARK(t.index);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<> inline void _mark<TupleRef>(TupleRef& t){
|
template<> inline void _mark<TupleRef>(TupleRef& t){
|
||||||
|
11
src/vm.h
11
src/vm.h
@ -650,9 +650,6 @@ inline Str VM::disassemble(CodeObject_ co){
|
|||||||
}
|
}
|
||||||
|
|
||||||
inline void VM::init_builtin_types(){
|
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(0)), .base = -1, .name = "object"});
|
||||||
_all_types.push_back({.obj = heap._new<Type>(Type(1), Type(1)), .base = 0, .name = "type"});
|
_all_types.push_back({.obj = heap._new<Type>(Type(1), Type(1)), .base = 0, .name = "type"});
|
||||||
tp_object = 0; tp_type = 1;
|
tp_object = 0; tp_type = 1;
|
||||||
@ -699,7 +696,10 @@ inline void VM::init_builtin_types(){
|
|||||||
builtins->attr().set("range", _t(tp_range));
|
builtins->attr().set("range", _t(tp_range));
|
||||||
|
|
||||||
post_init();
|
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();
|
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) {
|
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()){
|
for(auto& frame : vm->callstack.data()){
|
||||||
frame->_mark();
|
frame->_mark();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user