add inline optimize

This commit is contained in:
blueloveTH 2025-06-30 00:09:15 +08:00
parent 9b8f706010
commit f9f74b7b12
8 changed files with 38 additions and 15 deletions

View File

@ -60,4 +60,16 @@
#define PK_DEPRECATED __attribute__((deprecated)) #define PK_DEPRECATED __attribute__((deprecated))
#else #else
#define PK_DEPRECATED #define PK_DEPRECATED
#endif #endif
#ifdef NDEBUG
#if defined(__GNUC__)
#define PK_INLINE __attribute__((always_inline)) inline
#elif defined(_MSC_VER)
#define PK_INLINE __forceinline
#else
#define PK_INLINE inline
#endif
#else
#define PK_INLINE
#endif

View File

@ -92,7 +92,6 @@ bool pk__parse_int_slice(py_Ref slice,
bool pk__normalize_index(int* index, int length); bool pk__normalize_index(int* index, int length);
bool pk__object_new(int argc, py_Ref argv); bool pk__object_new(int argc, py_Ref argv);
py_TypeInfo* pk_typeinfo(py_Type type);
bool pk_wrapper__self(int argc, py_Ref argv); bool pk_wrapper__self(int argc, py_Ref argv);

View File

@ -10,17 +10,17 @@ py_ItemRef pk_tpfindname(py_TypeInfo* ti, py_Name name) {
return NULL; return NULL;
} }
py_ItemRef py_tpfindname(py_Type type, py_Name name) { PK_INLINE py_ItemRef py_tpfindname(py_Type type, py_Name name) {
py_TypeInfo* ti = pk_typeinfo(type); py_TypeInfo* ti = pk_typeinfo(type);
return pk_tpfindname(ti, name); return pk_tpfindname(ti, name);
} }
py_Ref py_tpfindmagic(py_Type t, py_Name name) { PK_INLINE py_Ref py_tpfindmagic(py_Type t, py_Name name) {
// assert(py_ismagicname(name)); // assert(py_ismagicname(name));
return py_tpfindname(t, name); return py_tpfindname(t, name);
} }
py_Type py_tpbase(py_Type t) { PK_INLINE py_Type py_tpbase(py_Type t) {
assert(t); assert(t);
py_TypeInfo* ti = pk_typeinfo(t); py_TypeInfo* ti = pk_typeinfo(t);
return ti->base; return ti->base;
@ -44,7 +44,7 @@ const char* py_tpname(py_Type type) {
return py_name2str(name); return py_name2str(name);
} }
py_TypeInfo* pk_typeinfo(py_Type type) { PK_INLINE py_TypeInfo* pk_typeinfo(py_Type type) {
#ifndef NDEBUG #ifndef NDEBUG
int length = pk_current_vm->types.length; int length = pk_current_vm->types.length;
if(type < 0 || type >= length) { if(type < 0 || type >= length) {

View File

@ -6,6 +6,18 @@
#include <assert.h> #include <assert.h>
#include <stdlib.h> #include <stdlib.h>
#define HASH_PROBE_0(__k, ok, i) \
ok = false; \
i = (uintptr_t)(__k)&self->mask; \
do { \
if(self->items[i].key == (__k)) { \
ok = true; \
break; \
} \
if(self->items[i].key == NULL) break; \
i = (5 * i + 1) & self->mask; \
} while(true);
#define HASH_PROBE_1(__k, ok, i) \ #define HASH_PROBE_1(__k, ok, i) \
ok = false; \ ok = false; \
i = (uintptr_t)(__k)&self->mask; \ i = (uintptr_t)(__k)&self->mask; \
@ -17,8 +29,6 @@
i = (5 * i + 1) & self->mask; \ i = (5 * i + 1) & self->mask; \
} }
#define HASH_PROBE_0 HASH_PROBE_1
static void NameDict__set_capacity_and_alloc_items(NameDict* self, int val) { static void NameDict__set_capacity_and_alloc_items(NameDict* self, int val) {
self->capacity = val; self->capacity = val;
self->critical_size = val * self->load_factor; self->critical_size = val * self->load_factor;

View File

@ -2,14 +2,16 @@
#include "pocketpy/pocketpy.h" #include "pocketpy/pocketpy.h"
#include <assert.h> #include <assert.h>
void* PyObject__userdata(PyObject* self) { return self->flex + PK_OBJ_SLOTS_SIZE(self->slots); } PK_INLINE void* PyObject__userdata(PyObject* self) {
return self->flex + PK_OBJ_SLOTS_SIZE(self->slots);
}
NameDict* PyObject__dict(PyObject* self) { PK_INLINE NameDict* PyObject__dict(PyObject* self) {
assert(self->slots == -1); assert(self->slots == -1);
return (NameDict*)(self->flex); return (NameDict*)(self->flex);
} }
py_TValue* PyObject__slots(PyObject* self) { PK_INLINE py_TValue* PyObject__slots(PyObject* self) {
assert(self->slots >= 0); assert(self->slots >= 0);
return (py_TValue*)(self->flex); return (py_TValue*)(self->flex);
} }

View File

@ -178,7 +178,7 @@ bool py_vectorcall(uint16_t argc, uint16_t kwargc) {
return VM__vectorcall(pk_current_vm, argc, kwargc, false) != RES_ERROR; return VM__vectorcall(pk_current_vm, argc, kwargc, false) != RES_ERROR;
} }
py_Ref py_retval() { return &pk_current_vm->last_retval; } PK_INLINE py_Ref py_retval() { return &pk_current_vm->last_retval; }
bool py_pushmethod(py_Name name) { bool py_pushmethod(py_Name name) {
bool ok = pk_loadmethod(py_peek(-1), name); bool ok = pk_loadmethod(py_peek(-1), name);

View File

@ -8,12 +8,12 @@ py_Ref py_getreg(int i) { return pk_current_vm->reg + i; }
void py_setreg(int i, py_Ref val) { pk_current_vm->reg[i] = *val; } void py_setreg(int i, py_Ref val) { pk_current_vm->reg[i] = *val; }
py_Ref py_getdict(py_Ref self, py_Name name) { PK_INLINE py_Ref py_getdict(py_Ref self, py_Name name) {
assert(self && self->is_ptr); assert(self && self->is_ptr);
return NameDict__try_get(PyObject__dict(self->_obj), name); return NameDict__try_get(PyObject__dict(self->_obj), name);
} }
void py_setdict(py_Ref self, py_Name name, py_Ref val) { PK_INLINE void py_setdict(py_Ref self, py_Name name, py_Ref val) {
assert(self && self->is_ptr); assert(self && self->is_ptr);
NameDict__set(PyObject__dict(self->_obj), name, val); NameDict__set(PyObject__dict(self->_obj), name, val);
} }

View File

@ -4,7 +4,7 @@
#include "pocketpy/objects/object.h" #include "pocketpy/objects/object.h"
#include "pocketpy/interpreter/vm.h" #include "pocketpy/interpreter/vm.h"
bool py_istype(py_Ref self, py_Type type) { return self->type == type; } PK_INLINE bool py_istype(py_Ref self, py_Type type) { return self->type == type; }
bool py_checktype(py_Ref self, py_Type type) { bool py_checktype(py_Ref self, py_Type type) {
if(self->type == type) return true; if(self->type == type) return true;