From 92e4416c4eb8959297f73eb09b38584d1acd1074 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Fri, 13 Oct 2023 12:46:41 +0800 Subject: [PATCH] some refactor --- include/pocketpy/cffi.h | 1 - include/pocketpy/namedict.h | 41 ++++++++----------------------------- src/ceval.cpp | 1 - src/namedict.cpp | 19 ----------------- src/vm.cpp | 5 ----- tests/80_linalg.py | 4 ++-- 6 files changed, 10 insertions(+), 61 deletions(-) diff --git a/include/pocketpy/cffi.h b/include/pocketpy/cffi.h index 8fdb6dff..38b04987 100644 --- a/include/pocketpy/cffi.h +++ b/include/pocketpy/cffi.h @@ -23,7 +23,6 @@ namespace pkpy { } \ PyObject* type = vm->new_type_object(mod, #name, base); \ T::_register(vm, mod, type); \ - type->attr()._try_perfect_rehash(); \ return type; \ } diff --git a/include/pocketpy/namedict.h b/include/pocketpy/namedict.h index f042d327..e7db76d4 100644 --- a/include/pocketpy/namedict.h +++ b/include/pocketpy/namedict.h @@ -96,14 +96,6 @@ struct SmallNameDict{ uint16_t capacity() const { return PK_SMALL_NAME_DICT_CAPACITY; } }; -inline const uint16_t kHashSeeds[] = {9629, 43049, 13267, 59509, 39251, 1249, 27689, 9719, 19913}; - -inline uint16_t _hash(StrName key, uint16_t mask, uint16_t hash_seed){ - return ( (key).index * (hash_seed) >> 8 ) & (mask); -} - -uint16_t _find_perfect_hash_seed(uint16_t capacity, const std::vector& keys); - template struct LargeNameDict { PK_ALWAYS_PASS_BY_POINTER(LargeNameDict) @@ -115,7 +107,6 @@ struct LargeNameDict { bool _is_small; float _load_factor; uint16_t _size; - uint16_t _hash_seed; uint16_t _capacity; uint16_t _critical_size; @@ -125,7 +116,7 @@ struct LargeNameDict { #define HASH_PROBE_1(key, ok, i) \ ok = false; \ -i = _hash(key, _mask, _hash_seed); \ +i = key.index & _mask; \ while(!_items[i].first.empty()) { \ if(_items[i].first == (key)) { ok = true; break; } \ i = (i + 1) & _mask; \ @@ -133,9 +124,8 @@ while(!_items[i].first.empty()) { \ #define HASH_PROBE_0 HASH_PROBE_1 - LargeNameDict(float load_factor=kInstAttrLoadFactor): _is_small(false), _load_factor(load_factor), _size(0), _hash_seed(kHashSeeds[0]) { - _set_capacity(kInitialCapacity); - _alloc_items(); + LargeNameDict(float load_factor=kInstAttrLoadFactor): _is_small(false), _load_factor(load_factor), _size(0) { + _set_capacity_and_alloc_items(kInitialCapacity); } ~LargeNameDict(){ free(_items); } @@ -143,13 +133,11 @@ while(!_items[i].first.empty()) { \ uint16_t size() const { return _size; } uint16_t capacity() const { return _capacity; } - void _set_capacity(uint16_t val){ + void _set_capacity_and_alloc_items(uint16_t val){ _capacity = val; _critical_size = val * _load_factor; _mask = val - 1; - } - void _alloc_items(){ _items = (Item*)malloc(_capacity * sizeof(Item)); memset(_items, 0, _capacity * sizeof(Item)); } @@ -160,7 +148,7 @@ while(!_items[i].first.empty()) { \ if(!ok) { _size++; if(_size > _critical_size){ - _rehash(true); + _rehash_2x(); HASH_PROBE_1(key, ok, i); } _items[i].first = key; @@ -168,13 +156,10 @@ while(!_items[i].first.empty()) { \ _items[i].second = val; } - void _rehash(bool resize){ + void _rehash_2x(){ Item* old_items = _items; uint16_t old_capacity = _capacity; - if(resize){ - _set_capacity(_capacity * 2); - } - _alloc_items(); + _set_capacity_and_alloc_items(_capacity * 2); for(uint16_t i=0; i keys() const{ std::vector v; apply([&](StrName key, V val){ diff --git a/src/ceval.cpp b/src/ceval.cpp index 05d86e98..25b61a9e 100644 --- a/src/ceval.cpp +++ b/src/ceval.cpp @@ -660,7 +660,6 @@ __NEXT_STEP:; DISPATCH(); TARGET(END_CLASS) _0 = POPX(); - _0->attr()._try_perfect_rehash(); DISPATCH(); TARGET(STORE_CLASS_ATTR){ _name = StrName(byte.arg); diff --git a/src/namedict.cpp b/src/namedict.cpp index bdbf72ff..f5198793 100644 --- a/src/namedict.cpp +++ b/src/namedict.cpp @@ -1,23 +1,4 @@ #include "pocketpy/namedict.h" namespace pkpy{ - -uint16_t _find_perfect_hash_seed(uint16_t capacity, const std::vector& keys){ - if(keys.empty()) return kHashSeeds[0]; - static std::set indices; - indices.clear(); - std::pair best_score = {kHashSeeds[0], 0.0f}; - const int kHashSeedsSize = sizeof(kHashSeeds) / sizeof(kHashSeeds[0]); - for(int i=0; i best_score.second) best_score = {kHashSeeds[i], score}; - } - return best_score.first; -} - } // namespace pkpy \ No newline at end of file diff --git a/src/vm.cpp b/src/vm.cpp index e884dc8a..e54ec1ef 100644 --- a/src/vm.cpp +++ b/src/vm.cpp @@ -295,7 +295,6 @@ namespace pkpy{ path_cpnts.pop_back(); PyObject* new_mod = new_module(name_cpnt, f_join(path_cpnts)); _exec(code, new_mod); - new_mod->attr()._try_perfect_rehash(); return new_mod; } @@ -693,10 +692,6 @@ void VM::init_builtin_types(){ builtins->attr().set("slice", _t(tp_slice)); post_init(); - for(int i=0; i<_all_types.size(); i++){ - _all_types[i].obj->attr()._try_perfect_rehash(); - } - for(auto [k, v]: _modules.items()) v->attr()._try_perfect_rehash(); this->_main = new_module("__main__"); } diff --git a/tests/80_linalg.py b/tests/80_linalg.py index 5f1b61ed..2f129985 100644 --- a/tests/80_linalg.py +++ b/tests/80_linalg.py @@ -83,14 +83,14 @@ assert str(static_test_vec4_float) == 'vec4(3.1887, -1.0984e+06, 9, 4.5654e+12)' assert str(static_test_vec4_int) == 'vec4(278, -1.39197e+13, 1.36422e+15, -37)' # test __getnewargs__ -element_name_list = [e for e in dir(test_vec4) if e in 'x,y,z,w'] +element_name_list = ['x', 'y', 'z', 'w'] element_value_list = [getattr(test_vec4, attr) for attr in element_name_list] _0 = tuple(element_value_list) _1 = test_vec4.__getnewargs__() assert (_0 == _1), (_0, _1) # test copy -element_name_list = [e for e in dir(test_vec4) if e in 'x,y,z,w'] +element_name_list = ['x', 'y', 'z', 'w'] element_value_list = [getattr(test_vec4, attr) for attr in element_name_list] copy_element_value_list = [getattr(test_vec4.copy(), attr) for attr in element_name_list] assert element_value_list == copy_element_value_list