From 581a21e407dc2a65736e04604432fbe99bd4a31e Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sun, 21 May 2023 12:51:14 +0800 Subject: [PATCH] ... --- src/ceval.h | 1 + src/cffi.h | 2 +- src/codeobject.h | 2 +- src/easing.h | 1 - src/namedict.h | 42 +++++++++++++++++++----------------------- 5 files changed, 22 insertions(+), 26 deletions(-) diff --git a/src/ceval.h b/src/ceval.h index 98656040..549a1c16 100644 --- a/src/ceval.h +++ b/src/ceval.h @@ -513,6 +513,7 @@ __NEXT_STEP:; if(s.empty() || s[0] == '_') continue; frame->f_globals().set(name, value); } + frame->f_globals()._try_perfect_rehash(); DISPATCH(); /*****************************************/ TARGET(UNPACK_SEQUENCE) diff --git a/src/cffi.h b/src/cffi.h index e843d0a1..479b72a5 100644 --- a/src/cffi.h +++ b/src/cffi.h @@ -317,7 +317,7 @@ struct C99ReflType final: ReflType{ C99ReflType& self = _CAST(C99ReflType&, obj); const Str& name = CAST(Str&, key); 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); return vm->None; } diff --git a/src/codeobject.h b/src/codeobject.h index 86b73f25..1123bfca 100644 --- a/src/codeobject.h +++ b/src/codeobject.h @@ -33,7 +33,7 @@ enum CodeBlockType { TRY_EXCEPT, }; -#define BC_NOARG -1 +#define BC_NOARG -1 #define BC_KEEPLINE -1 struct CodeBlock { diff --git a/src/easing.h b/src/easing.h index f1f468e6..de364950 100644 --- a/src/easing.h +++ b/src/easing.h @@ -1,6 +1,5 @@ #pragma once -#include #include "cffi.h" namespace pkpy{ diff --git a/src/namedict.h b/src/namedict.h index b814d33f..b7e644f5 100644 --- a/src/namedict.h +++ b/src/namedict.h @@ -6,7 +6,7 @@ namespace pkpy{ -const std::vector kHashSeeds = {9629, 43049, 13267, 59509, 39251, 1249, 35803, 54469, 27689, 9719, 34897, 18973, 30661, 19913, 27919, 32143}; +const std::vector kHashSeeds = {9629, 43049, 13267, 59509, 39251, 1249, 27689, 9719, 19913}; #define _hash(key, mask, hash_seed) ( ( (key).index * (hash_seed) >> 8 ) & (mask) ) @@ -42,32 +42,35 @@ struct NameDictImpl { uint16_t _mask; Item* _items; - void _alloc(int cap){ - _items = (Item*)pool128.alloc(cap * sizeof(Item)); - memset(_items, 0, cap * sizeof(Item)); - } +#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; \ +} + +#define NAMEDICT_ALLOC() \ + _items = (Item*)pool128.alloc(_capacity * sizeof(Item)); \ + memset(_items, 0, _capacity * sizeof(Item)); \ NameDictImpl(float load_factor=0.67f): _load_factor(load_factor), _capacity(__Capacity), _size(0), _hash_seed(kHashSeeds[0]), _mask(__Capacity-1) { - _alloc(__Capacity); + NAMEDICT_ALLOC() } NameDictImpl(const NameDictImpl& other) { memcpy(this, &other, sizeof(NameDictImpl)); - _alloc(_capacity); - for(int i=0; i<_capacity; i++){ - _items[i] = other._items[i]; - } + NAMEDICT_ALLOC() + for(int i=0; i<_capacity; i++) _items[i] = other._items[i]; } NameDictImpl& operator=(const NameDictImpl& other) { pool128.dealloc(_items); memcpy(this, &other, sizeof(NameDictImpl)); - _alloc(_capacity); - for(int i=0; i<_capacity; i++){ - _items[i] = other._items[i]; - } + NAMEDICT_ALLOC() + for(int i=0; i<_capacity; i++) _items[i] = other._items[i]; return *this; } @@ -77,14 +80,6 @@ struct NameDictImpl { NameDictImpl& operator=(NameDictImpl&&) = delete; 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 { bool ok; uint16_t i; HASH_PROBE(key, ok, i); @@ -113,7 +108,7 @@ while(!_items[i].first.empty()) { \ _capacity *= 2; _mask = _capacity - 1; } - _alloc(_capacity); + NAMEDICT_ALLOC() for(uint16_t i=0; i