From 04134084d60301a08af3c2833d2ab835e82fc205 Mon Sep 17 00:00:00 2001 From: Max Bernstein Date: Fri, 24 Feb 2023 11:36:19 -0500 Subject: [PATCH] Use arrayset instead of std::set This does not require a RB-tree due to the constant (and small) size of the array. Linear search is faster. This is a 38% speedup (as measured by Callgrind instruction count). --- src/namedict.h | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/namedict.h b/src/namedict.h index 7bcbec82..142aea8a 100644 --- a/src/namedict.h +++ b/src/namedict.h @@ -38,6 +38,34 @@ struct DictArrayPool { } }; +template +class arrayset { + public: + arrayset(size_t capacity) { + elements.reserve(capacity); + } + + void insert(const T& elem) { + for (size_t i = 0; i < elements.size(); i++) { + if (elements[i] == elem) { + return; + } + } + elements.push_back(elem); + } + + void clear() { + elements.clear(); + } + + size_t size() { + return elements.size(); + } + + private: + std::vector elements; +}; + namespace pkpy{ const std::vector kHashSeeds = {9629, 43049, 13267, 59509, 39251, 1249, 35803, 54469, 27689, 9719, 34897, 18973, 30661, 19913, 27919, 32143, 3467, 28019, 1051, 39419, 1361, 28547, 48197, 2609, 24317, 22861, 41467, 17623, 52837, 59053, 33589, 32117}; static DictArrayPool<32> _dict_pool; @@ -52,7 +80,7 @@ namespace pkpy{ uint16_t find_perfect_hash_seed(uint16_t capacity, const std::vector& keys){ if(keys.empty()) return kHashSeeds[0]; - std::set indices; + arrayset indices(kHashSeeds.size()); std::pair best_score = {kHashSeeds[0], 0}; for(int i=0; i