remove vm

This commit is contained in:
blueloveTH 2024-06-16 22:17:36 +08:00
parent 4d5e6c6b59
commit ea269686b5
5 changed files with 53 additions and 46 deletions

View File

@ -74,11 +74,13 @@ PK_INLINE bool PyVar__IS_OP(const PyVar* a, const PyVar* b){
#define pkpy_Var__is_null(self) ((self)->type == 0) #define pkpy_Var__is_null(self) ((self)->type == 0)
#define pkpy_Var__set_null(self) do { (self)->type = 0; } while(0) #define pkpy_Var__set_null(self) do { (self)->type = 0; } while(0)
bool pkpy_Var__eq__(void *vm, PyVar a, PyVar b);
int64_t pkpy_Var__hash__(void *vm, PyVar a);
extern PyVar pkpy_NULL, pkpy_OP_CALL, pkpy_OP_YIELD; extern PyVar pkpy_NULL, pkpy_OP_CALL, pkpy_OP_YIELD;
bool py_eq(const PyVar*, const PyVar*);
bool py_le(const PyVar*, const PyVar*);
int64_t py_hash(const PyVar*);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -49,7 +49,7 @@ pkpy_Dict pkpy_Dict__copy(const pkpy_Dict* self);
* @param val value to set * @param val value to set
* @return `true` if the key is newly added, `false` if the key already exists * @return `true` if the key is newly added, `false` if the key already exists
*/ */
bool pkpy_Dict__set(pkpy_Dict* self, void* vm, PyVar key, PyVar val); bool pkpy_Dict__set(pkpy_Dict* self, PyVar key, PyVar val);
/** /**
* @brief Check if a key exists in the `pkpy_Dict` * @brief Check if a key exists in the `pkpy_Dict`
@ -58,7 +58,7 @@ bool pkpy_Dict__set(pkpy_Dict* self, void* vm, PyVar key, PyVar val);
* @param key key to check * @param key key to check
* @return `true` if the key exists, `false` otherwise * @return `true` if the key exists, `false` otherwise
*/ */
bool pkpy_Dict__contains(const pkpy_Dict* self, void* vm, PyVar key); bool pkpy_Dict__contains(const pkpy_Dict* self, PyVar key);
/** /**
* @brief Remove a key from the `pkpy_Dict` * @brief Remove a key from the `pkpy_Dict`
@ -67,7 +67,7 @@ bool pkpy_Dict__contains(const pkpy_Dict* self, void* vm, PyVar key);
* @param key key to remove * @param key key to remove
* @return `true` if the key was found and removed, `false` if the key doesn't exist * @return `true` if the key was found and removed, `false` if the key doesn't exist
*/ */
bool pkpy_Dict__del(pkpy_Dict* self, void* vm, PyVar key); bool pkpy_Dict__del(pkpy_Dict* self, PyVar key);
/** /**
* @brief Try to get a value from the `pkpy_Dict` * @brief Try to get a value from the `pkpy_Dict`
@ -76,7 +76,7 @@ bool pkpy_Dict__del(pkpy_Dict* self, void* vm, PyVar key);
* @param key key to get * @param key key to get
* @return the value associated with the key, `NULL` if the key doesn't exist * @return the value associated with the key, `NULL` if the key doesn't exist
*/ */
const PyVar* pkpy_Dict__try_get(const pkpy_Dict* self, void* vm, PyVar key); const PyVar* pkpy_Dict__try_get(const pkpy_Dict* self, PyVar key);
/** /**
* @brief Update the `pkpy_Dict` with another one * @brief Update the `pkpy_Dict` with another one
@ -84,7 +84,7 @@ const PyVar* pkpy_Dict__try_get(const pkpy_Dict* self, void* vm, PyVar key);
* @param vm __eq__ and __hash__ context * @param vm __eq__ and __hash__ context
* @param other `pkpy_Dict` instance to update with * @param other `pkpy_Dict` instance to update with
*/ */
void pkpy_Dict__update(pkpy_Dict* self, void *vm, const pkpy_Dict* other); void pkpy_Dict__update(pkpy_Dict* self, const pkpy_Dict* other);
/** /**
* @brief Clear the `pkpy_Dict` * @brief Clear the `pkpy_Dict`

View File

@ -28,25 +28,25 @@ struct Dict : private pkpy_Dict {
int size() const { return count; } int size() const { return count; }
void set(VM* vm, PyVar key, PyVar val) { void set(VM* vm, PyVar key, PyVar val) {
pkpy_Dict__set(this, vm, *(::PyVar*)(&key), *(::PyVar*)(&val)); pkpy_Dict__set(this, *(::PyVar*)(&key), *(::PyVar*)(&val));
} }
PyVar try_get(VM* vm, PyVar key) const { PyVar try_get(VM* vm, PyVar key) const {
auto res = pkpy_Dict__try_get(this, vm, *(::PyVar*)(&key)); auto res = pkpy_Dict__try_get(this, *(::PyVar*)(&key));
if (!res) return nullptr; if (!res) return nullptr;
return *(const PyVar*)(res); return *(const PyVar*)(res);
} }
bool contains(VM* vm, PyVar key) const { bool contains(VM* vm, PyVar key) const {
return pkpy_Dict__contains(this, vm, *(::PyVar*)(&key)); return pkpy_Dict__contains(this, *(::PyVar*)(&key));
} }
bool del(VM* vm, PyVar key) { bool del(VM* vm, PyVar key) {
return pkpy_Dict__del(this, vm, *(::PyVar*)(&key)); return pkpy_Dict__del(this, *(::PyVar*)(&key));
} }
void update(VM* vm, const Dict& other) { void update(VM* vm, const Dict& other) {
pkpy_Dict__update(this, vm, &other); pkpy_Dict__update(this, &other);
} }
template <typename __Func> template <typename __Func>

View File

@ -73,7 +73,7 @@ static void pkpy_Dict__htset(pkpy_Dict* self, int h, int v) {
#endif #endif
} }
static int pkpy_Dict__probe0(const pkpy_Dict* self, void* vm, PyVar key, int hash) { static int pkpy_Dict__probe0(const pkpy_Dict* self, PyVar key, int hash) {
const int null = pkpy_Dict__idx_null(self); const int null = pkpy_Dict__idx_null(self);
const int mask = self->_htcap - 1; const int mask = self->_htcap - 1;
for(int h = hash & mask;; h = DICT_HASH_NEXT(h) & mask) { for(int h = hash & mask;; h = DICT_HASH_NEXT(h) & mask) {
@ -86,7 +86,7 @@ static int pkpy_Dict__probe0(const pkpy_Dict* self, void* vm, PyVar key, int has
PK_UNREACHABLE(); PK_UNREACHABLE();
} }
static int pkpy_Dict__probe1(const pkpy_Dict* self, void* vm, PyVar key, int hash) { static int pkpy_Dict__probe1(const pkpy_Dict* self, PyVar key, int hash) {
const int null = pkpy_Dict__idx_null(self); const int null = pkpy_Dict__idx_null(self);
const int mask = self->_htcap - 1; const int mask = self->_htcap - 1;
for(int h = hash & mask;; h = DICT_HASH_NEXT(h) & mask) { for(int h = hash & mask;; h = DICT_HASH_NEXT(h) & mask) {
@ -95,12 +95,12 @@ static int pkpy_Dict__probe1(const pkpy_Dict* self, void* vm, PyVar key, int has
struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx); struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx);
if(pkpy_Var__is_null(&entry->key)) continue; if(pkpy_Var__is_null(&entry->key)) continue;
if(pkpy_Var__eq__(vm, entry->key, key)) return h; if(py_eq(&entry->key, &key)) return h;
} }
PK_UNREACHABLE(); PK_UNREACHABLE();
} }
static void pkpy_Dict__extendht(pkpy_Dict* self, void* vm) { static void pkpy_Dict__extendht(pkpy_Dict* self) {
free(self->_hashtable); free(self->_hashtable);
self->_htcap *= 2; self->_htcap *= 2;
self->_hashtable = malloc(pkpy_Dict__ht_byte_size(self)); self->_hashtable = malloc(pkpy_Dict__ht_byte_size(self));
@ -110,15 +110,15 @@ static void pkpy_Dict__extendht(pkpy_Dict* self, void* vm) {
struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, i); struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, i);
if(pkpy_Var__is_null(&entry->key)) continue; if(pkpy_Var__is_null(&entry->key)) continue;
int rhash = DICT_HASH_TRANS(pkpy_Var__hash__(vm, entry->key)); int rhash = DICT_HASH_TRANS(py_hash(&entry->key));
int h = pkpy_Dict__probe0(self, vm, entry->key, rhash); int h = pkpy_Dict__probe0(self, entry->key, rhash);
pkpy_Dict__htset(self, h, i); pkpy_Dict__htset(self, h, i);
} }
} }
bool pkpy_Dict__set(pkpy_Dict* self, void* vm, PyVar key, PyVar val) { bool pkpy_Dict__set(pkpy_Dict* self, PyVar key, PyVar val) {
int hash = DICT_HASH_TRANS(pkpy_Var__hash__(vm, key)); int hash = DICT_HASH_TRANS(py_hash(&key));
int h = pkpy_Dict__probe1(self, vm, key, hash); int h = pkpy_Dict__probe1(self, key, hash);
int idx = pkpy_Dict__htget(self, h); int idx = pkpy_Dict__htget(self, h);
if(idx == pkpy_Dict__idx_null(self)) { if(idx == pkpy_Dict__idx_null(self)) {
@ -129,20 +129,20 @@ bool pkpy_Dict__set(pkpy_Dict* self, void* vm, PyVar key, PyVar val) {
.key = key, .key = key,
.val = val, .val = val,
})); }));
h = pkpy_Dict__probe0(self, vm, key, hash); h = pkpy_Dict__probe0(self, key, hash);
pkpy_Dict__htset(self, h, idx); pkpy_Dict__htset(self, h, idx);
self->count += 1; self->count += 1;
if(self->count >= self->_htcap * DICT_MAX_LOAD) pkpy_Dict__extendht(self, vm); if(self->count >= self->_htcap * DICT_MAX_LOAD) pkpy_Dict__extendht(self);
return true; return true;
} }
struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx); struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx);
if(pkpy_Var__eq__(vm, entry->key, key)) { if(py_eq(&entry->key, &key)) {
entry->val = val; entry->val = val;
} else { } else {
self->count += 1; self->count += 1;
h = pkpy_Dict__probe0(self, vm, key, hash); h = pkpy_Dict__probe0(self, key, hash);
idx = pkpy_Dict__htget(self, h); idx = pkpy_Dict__htget(self, h);
struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx); struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx);
entry->key = key; entry->key = key;
@ -151,16 +151,16 @@ bool pkpy_Dict__set(pkpy_Dict* self, void* vm, PyVar key, PyVar val) {
return false; return false;
} }
bool pkpy_Dict__contains(const pkpy_Dict* self, void* vm, PyVar key) { bool pkpy_Dict__contains(const pkpy_Dict* self, PyVar key) {
int hash = DICT_HASH_TRANS(pkpy_Var__hash__(vm, key)); int hash = DICT_HASH_TRANS(py_hash(&key));
int h = pkpy_Dict__probe1(self, vm, key, hash); int h = pkpy_Dict__probe1(self, key, hash);
int idx = pkpy_Dict__htget(self, h); int idx = pkpy_Dict__htget(self, h);
if(idx == pkpy_Dict__idx_null(self)) return false; if(idx == pkpy_Dict__idx_null(self)) return false;
return true; return true;
} }
static bool pkpy_Dict__refactor(pkpy_Dict* self, void* vm) { static bool pkpy_Dict__refactor(pkpy_Dict* self) {
int deleted_slots = self->_entries.count - self->count; int deleted_slots = self->_entries.count - self->count;
if(deleted_slots <= 8 || deleted_slots < self->_entries.count * (1 - DICT_MAX_LOAD)) return false; if(deleted_slots <= 8 || deleted_slots < self->_entries.count * (1 - DICT_MAX_LOAD)) return false;
@ -184,29 +184,29 @@ static bool pkpy_Dict__refactor(pkpy_Dict* self, void* vm) {
struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, i); struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, i);
if(pkpy_Var__is_null(&entry->key)) continue; if(pkpy_Var__is_null(&entry->key)) continue;
int rhash = DICT_HASH_TRANS(pkpy_Var__hash__(vm, entry->key)); int rhash = DICT_HASH_TRANS(py_hash(&entry->key));
int h = pkpy_Dict__probe0(self, vm, entry->key, rhash); int h = pkpy_Dict__probe0(self, entry->key, rhash);
pkpy_Dict__htset(self, h, i); pkpy_Dict__htset(self, h, i);
} }
return true; return true;
} }
bool pkpy_Dict__del(pkpy_Dict* self, void* vm, PyVar key) { bool pkpy_Dict__del(pkpy_Dict* self, PyVar key) {
int hash = DICT_HASH_TRANS(pkpy_Var__hash__(vm, key)); int hash = DICT_HASH_TRANS(py_hash(&key));
int h = pkpy_Dict__probe1(self, vm, key, hash); int h = pkpy_Dict__probe1(self, key, hash);
int idx = pkpy_Dict__htget(self, h), null = pkpy_Dict__idx_null(self); int idx = pkpy_Dict__htget(self, h), null = pkpy_Dict__idx_null(self);
if(idx == null) return false; if(idx == null) return false;
struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx); struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx);
pkpy_Var__set_null(&entry->key); pkpy_Var__set_null(&entry->key);
self->count -= 1; self->count -= 1;
pkpy_Dict__refactor(self, vm); pkpy_Dict__refactor(self);
return true; return true;
} }
const PyVar *pkpy_Dict__try_get(const pkpy_Dict* self, void* vm, PyVar key) { const PyVar *pkpy_Dict__try_get(const pkpy_Dict* self, PyVar key) {
int hash = DICT_HASH_TRANS(pkpy_Var__hash__(vm, key)); int hash = DICT_HASH_TRANS(py_hash(&key));
int h = pkpy_Dict__probe1(self, vm, key, hash); int h = pkpy_Dict__probe1(self, key, hash);
int idx = pkpy_Dict__htget(self, h); int idx = pkpy_Dict__htget(self, h);
if(idx == pkpy_Dict__idx_null(self)) return NULL; if(idx == pkpy_Dict__idx_null(self)) return NULL;
@ -215,11 +215,11 @@ const PyVar *pkpy_Dict__try_get(const pkpy_Dict* self, void* vm, PyVar key) {
return &entry->val; return &entry->val;
} }
void pkpy_Dict__update(pkpy_Dict *self, void *vm, const pkpy_Dict *other) { void pkpy_Dict__update(pkpy_Dict *self, const pkpy_Dict *other) {
for(int i = 0; i < other->_entries.count; i++) { for(int i = 0; i < other->_entries.count; i++) {
struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &other->_entries, i); struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &other->_entries, i);
if(pkpy_Var__is_null(&entry->key)) continue; if(pkpy_Var__is_null(&entry->key)) continue;
pkpy_Dict__set(self, vm, entry->key, entry->val); pkpy_Dict__set(self, entry->key, entry->val);
} }
} }

View File

@ -3,14 +3,19 @@
extern "C" { extern "C" {
bool pkpy_Var__eq__(void* vm_, PyVar a, PyVar b) { bool py_eq(const PyVar* a, const PyVar* b){
auto vm = (pkpy::VM*)(vm_); auto vm = (pkpy::VM*)pkpy_g.vm;
return vm->py_eq(*(PyVar*)(&a), *(PyVar*)(&b)); return vm->py_eq(*a, *b);
} }
int64_t pkpy_Var__hash__(void* vm_, PyVar a) { bool py_le(const PyVar* a, const PyVar* b){
auto vm = (pkpy::VM*)(vm_); auto vm = (pkpy::VM*)pkpy_g.vm;
return vm->py_hash(*(PyVar*)(&a)); return vm->py_le(*a, *b);
}
int64_t py_hash(const PyVar* a){
auto vm = (pkpy::VM*)pkpy_g.vm;
return vm->py_hash(*a);
} }
} }