Compare commits

..

4 Commits

Author SHA1 Message Date
blueloveTH
4bb0ae3035 some optimize 2025-06-20 18:24:30 +08:00
blueloveTH
4684e4386d some fix 2025-06-20 17:36:21 +08:00
blueloveTH
5666d2c580 replace BinTree with CachedNames 2025-06-20 17:32:53 +08:00
blueloveTH
5e65567c28 clear macros 2025-06-20 17:13:31 +08:00
11 changed files with 65 additions and 24 deletions

View File

@ -1,6 +1,5 @@
#pragma once #pragma once
#include "pocketpy/export.h"
#include "pocketpy/common/vector.h" #include "pocketpy/common/vector.h"
typedef struct c11_chunkedvector_chunk { typedef struct c11_chunkedvector_chunk {
int length; int length;

View File

@ -12,7 +12,7 @@ typedef struct py_TypeInfo {
struct py_TypeInfo* base_ti; struct py_TypeInfo* base_ti;
py_TValue self; py_TValue self;
py_TValue module; // the module where the type is defined py_GlobalRef module;
bool is_python; // is it a python class? (not derived from c object) bool is_python; // is it a python class? (not derived from c object)
bool is_sealed; // can it be subclassed? bool is_sealed; // can it be subclassed?

View File

@ -4,6 +4,7 @@
#include "pocketpy/common/name.h" #include "pocketpy/common/name.h"
#include "pocketpy/objects/codeobject.h" #include "pocketpy/objects/codeobject.h"
#include "pocketpy/objects/bintree.h" #include "pocketpy/objects/bintree.h"
#include "pocketpy/objects/container.h"
#include "pocketpy/pocketpy.h" #include "pocketpy/pocketpy.h"
#include "pocketpy/interpreter/heap.h" #include "pocketpy/interpreter/heap.h"
#include "pocketpy/interpreter/frame.h" #include "pocketpy/interpreter/frame.h"
@ -50,7 +51,7 @@ typedef struct VM {
py_TValue reg[8]; // users' registers py_TValue reg[8]; // users' registers
void* ctx; // user-defined context void* ctx; // user-defined context
BinTree cached_names; CachedNames cached_names;
NameDict compile_time_funcs; NameDict compile_time_funcs;
py_StackRef curr_class; py_StackRef curr_class;

View File

@ -0,0 +1,13 @@
#pragma once
#include "pocketpy/objects/base.h"
#include "pocketpy/common/chunkedvector.h"
#include "pocketpy/config.h"
#include <stdint.h>
#define FIXEDHASH_T__HEADER
#define K py_Name
#define V py_TValue
#define NAME CachedNames
#include "pocketpy/xmacros/fixedhash.h"
#undef FIXEDHASH_T__HEADER

View File

@ -1,9 +1,9 @@
#if !defined(FIXEDHASH_T__HEADER) && !defined(FIXEDHASH_T__SOURCE)
#include "pocketpy/common/chunkedvector.h" #include "pocketpy/common/chunkedvector.h"
#include "pocketpy/config.h" #include "pocketpy/config.h"
#include <stdint.h> #include <stdint.h>
#if !defined(FIXEDHASH_T__HEADER) && !defined(FIXEDHASH_T__SOURCE)
#define FIXEDHASH_T__HEADER #define FIXEDHASH_T__HEADER
#define FIXEDHASH_T__SOURCE #define FIXEDHASH_T__SOURCE
/* Input */ /* Input */
@ -48,6 +48,7 @@ NAME* METHOD(new)();
void METHOD(delete)(NAME* self); void METHOD(delete)(NAME* self);
void METHOD(set)(NAME* self, K key, V* value); void METHOD(set)(NAME* self, K key, V* value);
V* METHOD(try_get)(NAME* self, K key); V* METHOD(try_get)(NAME* self, K key);
bool METHOD(contains)(NAME* self, K key);
#endif #endif
@ -104,4 +105,22 @@ V* METHOD(try_get)(NAME* self, K key) {
return NULL; return NULL;
} }
bool METHOD(contains)(NAME* self, K key) {
V* value = METHOD(try_get)(self, key);
return value != NULL;
}
#endif #endif
/* Undefine all macros */
#undef KV
#undef METHOD
#undef CONCAT
#undef CONCAT_
#undef K
#undef V
#undef NAME
#undef less
#undef partial_less
#undef equal

View File

@ -1,5 +1,7 @@
#if !defined(SMALLMAP_T__HEADER) && !defined(SMALLMAP_T__SOURCE) #if !defined(SMALLMAP_T__HEADER) && !defined(SMALLMAP_T__SOURCE)
#include "pocketpy/common/vector.h" #include "pocketpy/common/vector.h"
#include "pocketpy/common/utils.h"
#include "pocketpy/config.h"
#define SMALLMAP_T__HEADER #define SMALLMAP_T__HEADER
#define SMALLMAP_T__SOURCE #define SMALLMAP_T__SOURCE

View File

@ -44,7 +44,7 @@ static void py_TypeInfo__ctor(py_TypeInfo* self,
py_Type index, py_Type index,
py_Type base, py_Type base,
py_TypeInfo* base_ti, py_TypeInfo* base_ti,
py_TValue module) { py_GlobalRef module) {
memset(self, 0, sizeof(py_TypeInfo)); memset(self, 0, sizeof(py_TypeInfo));
self->name = name; self->name = name;
@ -111,11 +111,7 @@ void VM__ctor(VM* self) {
ManagedHeap__ctor(&self->heap); ManagedHeap__ctor(&self->heap);
ValueStack__ctor(&self->stack); ValueStack__ctor(&self->stack);
const static BinTreeConfig cached_names_config = { CachedNames__ctor(&self->cached_names);
.f_cmp = BinTree__cmp_voidp,
.need_free_key = false,
};
BinTree__ctor(&self->cached_names, NULL, py_NIL(), &cached_names_config);
NameDict__ctor(&self->compile_time_funcs, PK_TYPE_ATTR_LOAD_FACTOR); NameDict__ctor(&self->compile_time_funcs, PK_TYPE_ATTR_LOAD_FACTOR);
/* Init Builtin Types */ /* Init Builtin Types */
@ -294,7 +290,7 @@ void VM__dtor(VM* self) {
TypeList__dtor(&self->types); TypeList__dtor(&self->types);
FixedMemoryPool__dtor(&self->pool_frame); FixedMemoryPool__dtor(&self->pool_frame);
ValueStack__dtor(&self->stack); ValueStack__dtor(&self->stack);
BinTree__dtor(&self->cached_names); CachedNames__dtor(&self->cached_names);
NameDict__dtor(&self->compile_time_funcs); NameDict__dtor(&self->compile_time_funcs);
} }
@ -407,7 +403,7 @@ py_Type pk_newtype(const char* name,
if(base_ti && base_ti->is_sealed) { if(base_ti && base_ti->is_sealed) {
c11__abort("type '%s' is not an acceptable base type", py_name2str(base_ti->name)); c11__abort("type '%s' is not an acceptable base type", py_name2str(base_ti->name));
} }
py_TypeInfo__ctor(ti, py_name(name), index, base, base_ti, module ? *module : *py_NIL()); py_TypeInfo__ctor(ti, py_name(name), index, base, base_ti, module ? module : py_NIL());
if(!dtor && base) dtor = base_ti->dtor; if(!dtor && base) dtor = base_ti->dtor;
ti->dtor = dtor; ti->dtor = dtor;
ti->is_python = is_python; ti->is_python = is_python;
@ -674,7 +670,10 @@ void ManagedHeap__mark(ManagedHeap* self) {
// mark modules // mark modules
BinTree__apply_mark(&vm->modules, p_stack); BinTree__apply_mark(&vm->modules, p_stack);
// mark cached names // mark cached names
BinTree__apply_mark(&vm->cached_names, p_stack); for(int i = 0; i < vm->cached_names.entries.length; i++) {
CachedNames_KV* kv = c11_chunkedvector__at(&vm->cached_names.entries, i);
pk__mark_value(&kv->val);
}
// mark compile time functions // mark compile time functions
for(int i = 0; i < vm->compile_time_funcs.capacity; i++) { for(int i = 0; i < vm->compile_time_funcs.capacity; i++) {
NameDict_KV* kv = &vm->compile_time_funcs.items[i]; NameDict_KV* kv = &vm->compile_time_funcs.items[i];
@ -876,13 +875,13 @@ int py_replinput(char* buf, int max_size) {
py_Ref py_name2ref(py_Name name) { py_Ref py_name2ref(py_Name name) {
assert(name != NULL); assert(name != NULL);
BinTree* d = &pk_current_vm->cached_names; CachedNames* d = &pk_current_vm->cached_names;
py_Ref res = BinTree__try_get(d, name); py_Ref res = CachedNames__try_get(d, name);
if(res != NULL) return res; if(res != NULL) return res;
// not found, create a new one // not found, create a new one
py_StackRef tmp = py_pushtmp(); py_StackRef tmp = py_pushtmp();
py_newstrv(tmp, py_name2sv(name)); py_newstrv(tmp, py_name2sv(name));
BinTree__set(d, name, tmp); CachedNames__set(d, name, tmp);
py_pop(); py_pop();
return BinTree__try_get(d, name); return CachedNames__try_get(d, name);
} }

View File

@ -62,11 +62,11 @@ static void PickleObject__write_bytes(PickleObject* buf, const void* data, int s
static void c11_sbuf__write_type_path(c11_sbuf* path_buf, py_Type type) { static void c11_sbuf__write_type_path(c11_sbuf* path_buf, py_Type type) {
py_TypeInfo* ti = pk__type_info(type); py_TypeInfo* ti = pk__type_info(type);
if(py_isnil(&ti->module)) { if(py_isnil(ti->module)) {
c11_sbuf__write_cstr(path_buf, py_name2str(ti->name)); c11_sbuf__write_cstr(path_buf, py_name2str(ti->name));
return; return;
} }
const char* mod_path = py_tostr(py_getdict(&ti->module, __path__)); const char* mod_path = py_tostr(py_getdict(ti->module, __path__));
c11_sbuf__write_cstr(path_buf, mod_path); c11_sbuf__write_cstr(path_buf, mod_path);
c11_sbuf__write_char(path_buf, '.'); c11_sbuf__write_char(path_buf, '.');
c11_sbuf__write_cstr(path_buf, py_name2str(ti->name)); c11_sbuf__write_cstr(path_buf, py_name2str(ti->name));

8
src/objects/container.c Normal file
View File

@ -0,0 +1,8 @@
#include "pocketpy/objects/container.h"
#define FIXEDHASH_T__SOURCE
#define K py_Name
#define V py_TValue
#define NAME CachedNames
#include "pocketpy/xmacros/fixedhash.h"
#undef FIXEDHASH_T__SOURCE

View File

@ -265,7 +265,7 @@ py_Ref py_tpfindname(py_Type t, py_Name name) {
py_Type py_tpbase(py_Type t) { py_Type py_tpbase(py_Type t) {
assert(t); assert(t);
py_TypeInfo* ti = pk__type_info(t); py_TypeInfo* ti = pk__type_info(t);
return py_totype(&ti->base_ti->self); return ti->base;
} }
PK_DEPRECATED py_Ref py_tpgetmagic(py_Type type, py_Name name) { PK_DEPRECATED py_Ref py_tpgetmagic(py_Type type, py_Name name) {

View File

@ -101,10 +101,10 @@ static bool type__or__(int argc, py_Ref argv) {
static bool type__module__(int argc, py_Ref argv) { static bool type__module__(int argc, py_Ref argv) {
PY_CHECK_ARGC(1); PY_CHECK_ARGC(1);
py_TypeInfo* ti = pk__type_info(py_totype(argv)); py_TypeInfo* ti = pk__type_info(py_totype(argv));
if(py_isnil(&ti->module)) { if(py_isnil(ti->module)) {
py_newnone(py_retval()); py_newnone(py_retval());
} else { } else {
py_Ref path = py_getdict(&ti->module, __path__); py_Ref path = py_getdict(ti->module, __path__);
py_assign(py_retval(), path); py_assign(py_retval(), path);
} }
return true; return true;