some optimize

This commit is contained in:
blueloveTH 2024-06-02 22:51:37 +08:00
parent 189c4de298
commit 663ca1ccf0
6 changed files with 16 additions and 23 deletions

View File

@ -8,10 +8,6 @@
namespace pkpy{ 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 kPoolExprBlockSize = 128;
inline const int kPoolFrameBlockSize = 80; inline const int kPoolFrameBlockSize = 80;
@ -20,4 +16,8 @@ void PoolExpr_dealloc(void*) noexcept;
void* PoolFrame_alloc() noexcept; void* PoolFrame_alloc() noexcept;
void PoolFrame_dealloc(void*) 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 }; // namespace pkpy

View File

@ -8,8 +8,6 @@ namespace pkpy{
struct CodeEmitContext; struct CodeEmitContext;
struct Expr; struct Expr;
#define PK_POOL128_DELETE(ptr) if(ptr != nullptr) { ptr->~T(); PoolExpr_dealloc(ptr); ptr = nullptr; }
template<typename T> template<typename T>
class unique_ptr_128{ class unique_ptr_128{
T* ptr; 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; }
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<typename U> template<typename U>
unique_ptr_128(unique_ptr_128<U>&& other): ptr(other.detach()) {} unique_ptr_128(unique_ptr_128<U>&& other): ptr(other.detach()) {}
@ -35,13 +33,13 @@ public:
template<typename U> template<typename U>
unique_ptr_128& operator=(unique_ptr_128<U>&& other) { unique_ptr_128& operator=(unique_ptr_128<U>&& other) {
PK_POOL128_DELETE(ptr) if(ptr) { ptr->~T(); PoolExpr_dealloc(ptr); };
ptr = other.detach(); ptr = other.detach();
return *this; return *this;
} }
unique_ptr_128& operator=(std::nullptr_t) { unique_ptr_128& operator=(std::nullptr_t) {
PK_POOL128_DELETE(ptr) if(ptr) { ptr->~T(); PoolExpr_dealloc(ptr); }
ptr = nullptr; ptr = nullptr;
return *this; return *this;
} }

View File

@ -42,7 +42,7 @@ struct ManagedHeap{
using __T = std::decay_t<T>; using __T = std::decay_t<T>;
static_assert(!is_sso_v<__T>, "gcnew cannot be used with SSO types"); static_assert(!is_sso_v<__T>, "gcnew cannot be used with SSO types");
// https://github.com/pocketpy/pocketpy/issues/94#issuecomment-1594784476 // 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>(args)...); p->placement_new<__T>(std::forward<Args>(args)...);
gen.push_back(p); gen.push_back(p);
gc_counter++; gc_counter++;
@ -53,7 +53,7 @@ struct ManagedHeap{
PyObject* _new(Type type, Args&&... args){ PyObject* _new(Type type, Args&&... args){
using __T = std::decay_t<T>; using __T = std::decay_t<T>;
static_assert(!is_sso_v<__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>(args)...); p->placement_new<__T>(std::forward<Args>(args)...);
_no_gc.push_back(p); _no_gc.push_back(p);
return p; return p;

View File

@ -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<int BlockSize, int BlockCount> template<int BlockSize, int BlockCount>
struct FixedMemoryPool{ struct FixedMemoryPool{
struct Block{ struct Block{
@ -252,10 +243,15 @@ struct FixedMemoryPool{
static FixedMemoryPool<kPoolExprBlockSize, 32> PoolExpr; static FixedMemoryPool<kPoolExprBlockSize, 32> PoolExpr;
static FixedMemoryPool<kPoolFrameBlockSize, 128> PoolFrame; static FixedMemoryPool<kPoolFrameBlockSize, 128> PoolFrame;
static MemoryPool<80> PoolObject;
void* PoolExpr_alloc() noexcept { return PoolExpr.alloc(); } void* PoolExpr_alloc() noexcept { return PoolExpr.alloc(); }
void PoolExpr_dealloc(void* p) noexcept { PoolExpr.dealloc(p); } void PoolExpr_dealloc(void* p) noexcept { PoolExpr.dealloc(p); }
void* PoolFrame_alloc() noexcept { return PoolFrame.alloc(); } void* PoolFrame_alloc() noexcept { return PoolFrame.alloc(); }
void PoolFrame_dealloc(void* p) noexcept { PoolFrame.dealloc(p); } 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 } // namespace pkpy

View File

@ -32,8 +32,7 @@ namespace pkpy{
#endif #endif
gen.clear(); gen.clear();
gen.swap(alive); gen.swap(alive);
// clean up pools PoolObject_shrink_to_fit();
pools_shrink_to_fit();
return freed; return freed;
} }

View File

@ -1878,7 +1878,7 @@ void ManagedHeap::_delete(PyObject* obj){
const PyTypeInfo* ti = vm->_tp_info(obj->type); const PyTypeInfo* ti = vm->_tp_info(obj->type);
if(ti->vt._dtor) ti->vt._dtor(obj->_value_ptr()); if(ti->vt._dtor) ti->vt._dtor(obj->_value_ptr());
delete obj->_attr; // delete __dict__ if exists delete obj->_attr; // delete __dict__ if exists
pool128_dealloc(obj); PoolObject_dealloc(obj);
} }
void Dict::_gc_mark(VM* vm) const{ void Dict::_gc_mark(VM* vm) const{