From ec366c15c69d7603437576639d85c97e375a5eb1 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sun, 9 Jun 2024 21:42:06 +0800 Subject: [PATCH] some refactor --- include/pocketpy/interpreter/gc.hpp | 31 ++++++++++++++++++----------- include/pocketpy/objects/object.hpp | 16 --------------- src/objects/namedict.cpp | 4 ---- 3 files changed, 19 insertions(+), 32 deletions(-) diff --git a/include/pocketpy/interpreter/gc.hpp b/include/pocketpy/interpreter/gc.hpp index cf6abcad..5c1060db 100644 --- a/include/pocketpy/interpreter/gc.hpp +++ b/include/pocketpy/interpreter/gc.hpp @@ -4,6 +4,7 @@ #include "pocketpy/common/vector.hpp" #include "pocketpy/common/utils.hpp" #include "pocketpy/objects/object.hpp" +#include "pocketpy/objects/namedict.hpp" namespace pkpy { struct ManagedHeap { @@ -34,9 +35,8 @@ struct ManagedHeap { ScopeLock gc_scope_lock() { return ScopeLock(this); } /********************/ - template - PyObject* gcnew(Type type, Args&&... args) { + PyObject* _basic_new(Type type, Args&&... args) { using __T = std::decay_t; static_assert(!is_sso_v<__T>, "gcnew cannot be used with SSO types"); // https://github.com/pocketpy/pocketpy/issues/94#issuecomment-1594784476 @@ -46,7 +46,22 @@ struct ManagedHeap { }else{ p = new (std::malloc(py_sizeof<__T>)) PyObject(type, true); } - p->placement_new<__T>(std::forward(args)...); + new (p->_value_ptr()) T(std::forward(args)...); + + // backdoor for important builtin types + if constexpr(std::is_same_v<__T, DummyInstance>) { + p->_attr = new NameDict(PK_INST_ATTR_LOAD_FACTOR); + } else if constexpr(std::is_same_v<__T, Type>) { + p->_attr = new NameDict(PK_TYPE_ATTR_LOAD_FACTOR); + } else if constexpr(std::is_same_v<__T, DummyModule>) { + p->_attr = new NameDict(PK_TYPE_ATTR_LOAD_FACTOR); + } + return p; + } + + template + PyObject* gcnew(Type type, Args&&... args) { + PyObject* p = _basic_new(type, std::forward(args)...); gen.push_back(p); gc_counter++; return p; @@ -54,15 +69,7 @@ struct ManagedHeap { template PyObject* _new(Type type, Args&&... args) { - using __T = std::decay_t; - static_assert(!is_sso_v<__T>); - PyObject* p; - if constexpr(py_sizeof<__T> <= kPoolObjectBlockSize){ - p = new (PoolObject_alloc()) PyObject(type, false); - }else{ - p = new (std::malloc(py_sizeof<__T>)) PyObject(type, true); - } - p->placement_new<__T>(std::forward(args)...); + PyObject* p = _basic_new(type, std::forward(args)...); _no_gc.push_back(p); return p; } diff --git a/include/pocketpy/objects/object.hpp b/include/pocketpy/objects/object.hpp index 52641b19..842a8000 100644 --- a/include/pocketpy/objects/object.hpp +++ b/include/pocketpy/objects/object.hpp @@ -32,22 +32,6 @@ struct PyObject final { PyObject(Type type, bool gc_is_large) : type(type), gc_is_large(gc_is_large), gc_marked(false), _attr(nullptr) {} PyVar attr(StrName name) const; - static NameDict* __init_namedict(float lf); - - template - void placement_new(Args&&... args) { - static_assert(std::is_same_v>); - new (_value_ptr()) T(std::forward(args)...); - - // backdoor for important builtin types - if constexpr(std::is_same_v) { - _attr = __init_namedict(PK_INST_ATTR_LOAD_FACTOR); - } else if constexpr(std::is_same_v) { - _attr = __init_namedict(PK_TYPE_ATTR_LOAD_FACTOR); - } else if constexpr(std::is_same_v) { - _attr = __init_namedict(PK_TYPE_ATTR_LOAD_FACTOR); - } - } }; static_assert(sizeof(PyObject) <= 16); diff --git a/src/objects/namedict.cpp b/src/objects/namedict.cpp index e60a0390..8b76fafc 100644 --- a/src/objects/namedict.cpp +++ b/src/objects/namedict.cpp @@ -160,8 +160,4 @@ PyVar PyObject::attr(StrName name) const { return (*_attr)[name]; } -NameDict* PyObject::__init_namedict(float lf) { - return new NameDict(lf); -} - } // namespace pkpy