This commit is contained in:
blueloveTH 2023-05-21 12:51:14 +08:00
parent 54f376c189
commit 581a21e407
5 changed files with 22 additions and 26 deletions

View File

@ -513,6 +513,7 @@ __NEXT_STEP:;
if(s.empty() || s[0] == '_') continue; if(s.empty() || s[0] == '_') continue;
frame->f_globals().set(name, value); frame->f_globals().set(name, value);
} }
frame->f_globals()._try_perfect_rehash();
DISPATCH(); DISPATCH();
/*****************************************/ /*****************************************/
TARGET(UNPACK_SEQUENCE) TARGET(UNPACK_SEQUENCE)

View File

@ -317,7 +317,7 @@ struct C99ReflType final: ReflType{
C99ReflType& self = _CAST(C99ReflType&, obj); C99ReflType& self = _CAST(C99ReflType&, obj);
const Str& name = CAST(Str&, key); const Str& name = CAST(Str&, key);
auto it = std::lower_bound(self.fields.begin(), self.fields.end(), name.sv()); auto it = std::lower_bound(self.fields.begin(), self.fields.end(), name.sv());
if(it == self.fields.end()){ if(it == self.fields.end() || it->name != name.sv()){
vm->KeyError(key); vm->KeyError(key);
return vm->None; return vm->None;
} }

View File

@ -1,6 +1,5 @@
#pragma once #pragma once
#include <cmath>
#include "cffi.h" #include "cffi.h"
namespace pkpy{ namespace pkpy{

View File

@ -6,7 +6,7 @@
namespace pkpy{ namespace pkpy{
const std::vector<uint16_t> kHashSeeds = {9629, 43049, 13267, 59509, 39251, 1249, 35803, 54469, 27689, 9719, 34897, 18973, 30661, 19913, 27919, 32143}; const std::vector<uint16_t> kHashSeeds = {9629, 43049, 13267, 59509, 39251, 1249, 27689, 9719, 19913};
#define _hash(key, mask, hash_seed) ( ( (key).index * (hash_seed) >> 8 ) & (mask) ) #define _hash(key, mask, hash_seed) ( ( (key).index * (hash_seed) >> 8 ) & (mask) )
@ -42,32 +42,35 @@ struct NameDictImpl {
uint16_t _mask; uint16_t _mask;
Item* _items; Item* _items;
void _alloc(int cap){ #define HASH_PROBE(key, ok, i) \
_items = (Item*)pool128.alloc(cap * sizeof(Item)); ok = false; \
memset(_items, 0, cap * sizeof(Item)); i = _hash(key, _mask, _hash_seed); \
} while(!_items[i].first.empty()) { \
if(_items[i].first == (key)) { ok = true; break; } \
i = (i + 1) & _mask; \
}
#define NAMEDICT_ALLOC() \
_items = (Item*)pool128.alloc(_capacity * sizeof(Item)); \
memset(_items, 0, _capacity * sizeof(Item)); \
NameDictImpl(float load_factor=0.67f): NameDictImpl(float load_factor=0.67f):
_load_factor(load_factor), _capacity(__Capacity), _size(0), _load_factor(load_factor), _capacity(__Capacity), _size(0),
_hash_seed(kHashSeeds[0]), _mask(__Capacity-1) { _hash_seed(kHashSeeds[0]), _mask(__Capacity-1) {
_alloc(__Capacity); NAMEDICT_ALLOC()
} }
NameDictImpl(const NameDictImpl& other) { NameDictImpl(const NameDictImpl& other) {
memcpy(this, &other, sizeof(NameDictImpl)); memcpy(this, &other, sizeof(NameDictImpl));
_alloc(_capacity); NAMEDICT_ALLOC()
for(int i=0; i<_capacity; i++){ for(int i=0; i<_capacity; i++) _items[i] = other._items[i];
_items[i] = other._items[i];
}
} }
NameDictImpl& operator=(const NameDictImpl& other) { NameDictImpl& operator=(const NameDictImpl& other) {
pool128.dealloc(_items); pool128.dealloc(_items);
memcpy(this, &other, sizeof(NameDictImpl)); memcpy(this, &other, sizeof(NameDictImpl));
_alloc(_capacity); NAMEDICT_ALLOC()
for(int i=0; i<_capacity; i++){ for(int i=0; i<_capacity; i++) _items[i] = other._items[i];
_items[i] = other._items[i];
}
return *this; return *this;
} }
@ -77,14 +80,6 @@ struct NameDictImpl {
NameDictImpl& operator=(NameDictImpl&&) = delete; NameDictImpl& operator=(NameDictImpl&&) = delete;
uint16_t size() const { return _size; } uint16_t size() const { return _size; }
#define HASH_PROBE(key, ok, i) \
ok = false; \
i = _hash(key, _mask, _hash_seed); \
while(!_items[i].first.empty()) { \
if(_items[i].first == (key)) { ok = true; break; } \
i = (i + 1) & _mask; \
}
T operator[](StrName key) const { T operator[](StrName key) const {
bool ok; uint16_t i; bool ok; uint16_t i;
HASH_PROBE(key, ok, i); HASH_PROBE(key, ok, i);
@ -113,7 +108,7 @@ while(!_items[i].first.empty()) { \
_capacity *= 2; _capacity *= 2;
_mask = _capacity - 1; _mask = _capacity - 1;
} }
_alloc(_capacity); NAMEDICT_ALLOC()
for(uint16_t i=0; i<old_capacity; i++){ for(uint16_t i=0; i<old_capacity; i++){
if(old_items[i].first.empty()) continue; if(old_items[i].first.empty()) continue;
bool ok; uint16_t j; bool ok; uint16_t j;
@ -196,6 +191,7 @@ while(!_items[i].first.empty()) { \
_size = 0; _size = 0;
} }
#undef HASH_PROBE #undef HASH_PROBE
#undef NAMEDICT_ALLOC
#undef _hash #undef _hash
}; };