From 663ca1ccf0243f4726cbf801e1420eb027f07852 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sun, 2 Jun 2024 22:51:37 +0800 Subject: [PATCH] some optimize --- include/pocketpy/common/memorypool.hpp | 8 ++++---- include/pocketpy/compiler/expr.hpp | 8 +++----- include/pocketpy/interpreter/gc.hpp | 4 ++-- src/common/memorypool.cpp | 14 +++++--------- src/interpreter/gc.cpp | 3 +-- src/interpreter/vm.cpp | 2 +- 6 files changed, 16 insertions(+), 23 deletions(-) diff --git a/include/pocketpy/common/memorypool.hpp b/include/pocketpy/common/memorypool.hpp index a3e0f465..adf12f46 100644 --- a/include/pocketpy/common/memorypool.hpp +++ b/include/pocketpy/common/memorypool.hpp @@ -8,10 +8,6 @@ namespace pkpy{ -void* pool128_alloc(size_t) noexcept; -void pool128_dealloc(void*) noexcept; -void pools_shrink_to_fit() noexcept; - inline const int kPoolExprBlockSize = 128; inline const int kPoolFrameBlockSize = 80; @@ -20,4 +16,8 @@ void PoolExpr_dealloc(void*) noexcept; void* PoolFrame_alloc() noexcept; void PoolFrame_dealloc(void*) noexcept; +void* PoolObject_alloc(size_t size) noexcept; +void PoolObject_dealloc(void* p) noexcept; +void PoolObject_shrink_to_fit() noexcept; + }; // namespace pkpy diff --git a/include/pocketpy/compiler/expr.hpp b/include/pocketpy/compiler/expr.hpp index 93485d0f..1db694e0 100644 --- a/include/pocketpy/compiler/expr.hpp +++ b/include/pocketpy/compiler/expr.hpp @@ -8,8 +8,6 @@ namespace pkpy{ struct CodeEmitContext; struct Expr; -#define PK_POOL128_DELETE(ptr) if(ptr != nullptr) { ptr->~T(); PoolExpr_dealloc(ptr); ptr = nullptr; } - template class unique_ptr_128{ T* ptr; @@ -26,7 +24,7 @@ public: bool operator==(std::nullptr_t) const { return ptr == nullptr; } bool operator!=(std::nullptr_t) const { return ptr != nullptr; } - ~unique_ptr_128(){ PK_POOL128_DELETE(ptr) } + ~unique_ptr_128(){ if(ptr) { ptr->~T(); PoolExpr_dealloc(ptr); } } template unique_ptr_128(unique_ptr_128&& other): ptr(other.detach()) {} @@ -35,13 +33,13 @@ public: template unique_ptr_128& operator=(unique_ptr_128&& other) { - PK_POOL128_DELETE(ptr) + if(ptr) { ptr->~T(); PoolExpr_dealloc(ptr); }; ptr = other.detach(); return *this; } unique_ptr_128& operator=(std::nullptr_t) { - PK_POOL128_DELETE(ptr) + if(ptr) { ptr->~T(); PoolExpr_dealloc(ptr); } ptr = nullptr; return *this; } diff --git a/include/pocketpy/interpreter/gc.hpp b/include/pocketpy/interpreter/gc.hpp index 2a3fd0d4..9b704320 100644 --- a/include/pocketpy/interpreter/gc.hpp +++ b/include/pocketpy/interpreter/gc.hpp @@ -42,7 +42,7 @@ struct ManagedHeap{ 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 - PyObject* p = new(pool128_alloc(py_sizeof<__T>)) PyObject(type); + PyObject* p = new(PoolObject_alloc(py_sizeof<__T>)) PyObject(type); p->placement_new<__T>(std::forward(args)...); gen.push_back(p); gc_counter++; @@ -53,7 +53,7 @@ struct ManagedHeap{ PyObject* _new(Type type, Args&&... args){ using __T = std::decay_t; static_assert(!is_sso_v<__T>); - PyObject* p = new(pool128_alloc(py_sizeof<__T>)) PyObject(type); + PyObject* p = new(PoolObject_alloc(py_sizeof<__T>)) PyObject(type); p->placement_new<__T>(std::forward(args)...); _no_gc.push_back(p); return p; diff --git a/src/common/memorypool.cpp b/src/common/memorypool.cpp index 803f37fb..7be76055 100644 --- a/src/common/memorypool.cpp +++ b/src/common/memorypool.cpp @@ -196,15 +196,6 @@ struct MemoryPool{ } }; -static MemoryPool<128> pool128; - -void* pool128_alloc(size_t size) noexcept { return pool128.alloc(size); } -void pool128_dealloc(void* p) noexcept { pool128.dealloc(p); } - -void pools_shrink_to_fit() noexcept { - pool128.shrink_to_fit(); -} - template struct FixedMemoryPool{ struct Block{ @@ -252,10 +243,15 @@ struct FixedMemoryPool{ static FixedMemoryPool PoolExpr; static FixedMemoryPool PoolFrame; +static MemoryPool<80> PoolObject; + void* PoolExpr_alloc() noexcept { return PoolExpr.alloc(); } void PoolExpr_dealloc(void* p) noexcept { PoolExpr.dealloc(p); } void* PoolFrame_alloc() noexcept { return PoolFrame.alloc(); } void PoolFrame_dealloc(void* p) noexcept { PoolFrame.dealloc(p); } +void* PoolObject_alloc(size_t size) noexcept { return PoolObject.alloc(size); } +void PoolObject_dealloc(void* p) noexcept { PoolObject.dealloc(p); } +void PoolObject_shrink_to_fit() noexcept { PoolObject.shrink_to_fit(); } } // namespace pkpy \ No newline at end of file diff --git a/src/interpreter/gc.cpp b/src/interpreter/gc.cpp index 6eed0d77..eb0e480d 100644 --- a/src/interpreter/gc.cpp +++ b/src/interpreter/gc.cpp @@ -32,8 +32,7 @@ namespace pkpy{ #endif gen.clear(); gen.swap(alive); - // clean up pools - pools_shrink_to_fit(); + PoolObject_shrink_to_fit(); return freed; } diff --git a/src/interpreter/vm.cpp b/src/interpreter/vm.cpp index 331a57f8..2f4eff88 100644 --- a/src/interpreter/vm.cpp +++ b/src/interpreter/vm.cpp @@ -1878,7 +1878,7 @@ void ManagedHeap::_delete(PyObject* obj){ const PyTypeInfo* ti = vm->_tp_info(obj->type); if(ti->vt._dtor) ti->vt._dtor(obj->_value_ptr()); delete obj->_attr; // delete __dict__ if exists - pool128_dealloc(obj); + PoolObject_dealloc(obj); } void Dict::_gc_mark(VM* vm) const{