mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
improve dict perf
This commit is contained in:
parent
d1ebff587c
commit
4cdbc426d5
@ -99,6 +99,7 @@ static bool Dict__try_get(Dict* self, py_TValue* key, DictEntry** out) {
|
||||
int idx2 = self->indices[idx]._[i];
|
||||
if(idx2 == -1) continue;
|
||||
DictEntry* entry = c11__at(DictEntry, &self->entries, idx2);
|
||||
if(entry->hash == (uint64_t)hash) {
|
||||
int res = py_equal(&entry->key, key);
|
||||
if(res == 1) {
|
||||
*out = entry;
|
||||
@ -106,6 +107,7 @@ static bool Dict__try_get(Dict* self, py_TValue* key, DictEntry** out) {
|
||||
}
|
||||
if(res == -1) return false; // error
|
||||
}
|
||||
}
|
||||
*out = NULL;
|
||||
return true;
|
||||
}
|
||||
@ -195,6 +197,9 @@ static bool Dict__set(Dict* self, py_TValue* key, py_TValue* val) {
|
||||
}
|
||||
// update existing entry
|
||||
DictEntry* entry = c11__at(DictEntry, &self->entries, idx2);
|
||||
// check if they have the same hash
|
||||
if(entry->hash == (uint64_t)hash) {
|
||||
// check if they are equal
|
||||
int res = py_equal(&entry->key, key);
|
||||
if(res == 1) {
|
||||
entry->val = *val;
|
||||
@ -202,6 +207,7 @@ static bool Dict__set(Dict* self, py_TValue* key, py_TValue* val) {
|
||||
}
|
||||
if(res == -1) return false; // error
|
||||
}
|
||||
}
|
||||
// no empty slot found
|
||||
if(self->capacity >= (uint32_t)self->entries.length * 10) {
|
||||
// raise error if we reach the minimum load factor (10%)
|
||||
@ -224,6 +230,7 @@ static int Dict__pop(Dict* self, py_Ref key) {
|
||||
int idx2 = self->indices[idx]._[i];
|
||||
if(idx2 == -1) continue;
|
||||
DictEntry* entry = c11__at(DictEntry, &self->entries, idx2);
|
||||
if(entry->hash == (uint64_t)hash) {
|
||||
int res = py_equal(&entry->key, key);
|
||||
if(res == 1) {
|
||||
*py_retval() = entry->val;
|
||||
@ -235,6 +242,7 @@ static int Dict__pop(Dict* self, py_Ref key) {
|
||||
}
|
||||
if(res == -1) return -1; // error
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -382,12 +390,16 @@ static bool dict__eq__(int argc, py_Ref argv) {
|
||||
py_newbool(py_retval(), false);
|
||||
return true;
|
||||
}
|
||||
int res = py_equal(&entry->val, &other_entry->val);
|
||||
if(res == -1) return false;
|
||||
if(!res) {
|
||||
if(entry->hash != other_entry->hash) {
|
||||
py_newbool(py_retval(), false);
|
||||
return true;
|
||||
}
|
||||
int res = py_equal(&entry->val, &other_entry->val);
|
||||
if(res == 0) {
|
||||
py_newbool(py_retval(), false);
|
||||
return true;
|
||||
}
|
||||
if(res == -1) return false; // error
|
||||
}
|
||||
py_newbool(py_retval(), true);
|
||||
return true;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user