improve performance

This commit is contained in:
blueloveTH 2025-03-04 23:32:14 +08:00
parent 2fdf09f652
commit 160bc99d04
4 changed files with 16 additions and 13 deletions

View File

@ -9,7 +9,7 @@ typedef struct FixedMemoryPool {
int exceeded_bytes;
char** _free_list;
char** _free_list_end;
int _free_list_length;
} FixedMemoryPool;
void FixedMemoryPool__ctor(FixedMemoryPool* self, int BlockSize, int BlockCount);

View File

@ -11,7 +11,7 @@ void FixedMemoryPool__ctor(FixedMemoryPool* self, int BlockSize, int BlockCount)
self->data = PK_MALLOC(BlockSize * BlockCount);
self->data_end = self->data + BlockSize * BlockCount;
self->_free_list = PK_MALLOC(sizeof(void*) * BlockCount);
self->_free_list_end = self->_free_list;
self->_free_list_length = BlockCount;
for(int i = 0; i < BlockCount; i++) {
self->_free_list[i] = self->data + i * BlockSize;
}
@ -23,9 +23,9 @@ void FixedMemoryPool__dtor(FixedMemoryPool* self) {
}
void* FixedMemoryPool__alloc(FixedMemoryPool* self) {
if(self->_free_list_end != self->_free_list) {
self->_free_list_end--;
return *self->_free_list_end;
if(self->_free_list_length > 0) {
self->_free_list_length--;
return self->_free_list[self->_free_list_length];
} else {
self->exceeded_bytes += self->BlockSize;
return PK_MALLOC(self->BlockSize);
@ -35,8 +35,8 @@ void* FixedMemoryPool__alloc(FixedMemoryPool* self) {
void FixedMemoryPool__dealloc(FixedMemoryPool* self, void* p) {
bool is_valid = (char*)p >= self->data && (char*)p < self->data_end;
if(is_valid) {
*self->_free_list_end = p;
self->_free_list_end++;
self->_free_list[self->_free_list_length] = p;
self->_free_list_length++;
} else {
self->exceeded_bytes -= self->BlockSize;
PK_FREE(p);

View File

@ -199,13 +199,14 @@ FrameResult VM__run_top_frame(VM* self) {
/*****************************************/
case OP_LOAD_FAST: {
assert(!frame->is_locals_special);
PUSH(&frame->locals[byte.arg]);
if(py_isnil(TOP())) {
py_Name name = c11__getitem(uint16_t, &frame->co->varnames, byte.arg);
UnboundLocalError(name);
goto __ERROR;
py_Ref val = &frame->locals[byte.arg];
if(!py_isnil(val)) {
PUSH(val);
DISPATCH();
}
DISPATCH();
py_Name name = c11__getitem(uint16_t, &frame->co->varnames, byte.arg);
UnboundLocalError(name);
goto __ERROR;
}
case OP_LOAD_NAME: {
assert(frame->is_locals_special);

View File

@ -447,7 +447,9 @@ static bool
}
FrameResult VM__vectorcall(VM* self, uint16_t argc, uint16_t kwargc, bool opcall) {
#ifndef NDEBUG
pk_print_stack(self, self->top_frame, (Bytecode){0});
#endif
py_Ref p1 = self->stack.sp - kwargc * 2;
py_Ref p0 = p1 - argc - 2;