From f6ef78c0f1f1b624eda001c3e8fc8988e0c87e73 Mon Sep 17 00:00:00 2001 From: Nikita Taranov Date: Sat, 25 Jun 2022 00:55:58 +0200 Subject: [PATCH] stash --- src/Common/HashTable/HashTable.h | 20 +++++++++++++++++--- src/Common/HashTable/StringHashTable.h | 6 +++++- src/Common/HashTable/TwoLevelHashTable.h | 1 + 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/Common/HashTable/HashTable.h b/src/Common/HashTable/HashTable.h index 7db693ddcec..78fdcf8a281 100644 --- a/src/Common/HashTable/HashTable.h +++ b/src/Common/HashTable/HashTable.h @@ -228,11 +228,22 @@ void insertSetMapped(MappedType & dest, const ValueType & src) { dest = src.seco /** Determines the size of the hash table, and when and how much it should be resized. */ template -struct HashTableGrower +class HashTableGrower { /// The state of this structure is enough to get the buffer size of the hash table. UInt8 size_degree = initial_size_degree; + size_t cached_mask = (1ULL << initial_size_degree) - 1; + size_t cached_max_fill = 1ULL << (initial_size_degree - 1); + +protected: + void updateSizeDegree() + { + cached_mask = (1ULL << size_degree) - 1; + cached_max_fill = 1ULL << (size_degree - 1); + } + +public: static constexpr auto initial_count = 1ULL << initial_size_degree; /// If collision resolution chains are contiguous, we can implement erase operation by moving the elements. @@ -241,8 +252,8 @@ struct HashTableGrower /// The size of the hash table in the cells. size_t bufSize() const { return 1ULL << size_degree; } - size_t maxFill() const { return 1ULL << (size_degree - 1); } - size_t mask() const { return bufSize() - 1; } + size_t maxFill() const { return cached_max_fill; } + size_t mask() const { return cached_mask; } /// From the hash value, get the cell number in the hash table. size_t place(size_t x) const { return x & mask(); } @@ -257,6 +268,7 @@ struct HashTableGrower void increaseSize() { size_degree += size_degree >= 23 ? 1 : 2; + updateMask(); } /// Set the buffer size by the number of elements in the hash table. Used when deserializing a hash table. @@ -267,11 +279,13 @@ struct HashTableGrower : ((initial_size_degree > static_cast(log2(num_elems - 1)) + 2) ? initial_size_degree : (static_cast(log2(num_elems - 1)) + 2)); + updateMask(); } void setBufSize(size_t buf_size_) { size_degree = static_cast(log2(buf_size_ - 1) + 1); + updateMask(); } }; diff --git a/src/Common/HashTable/StringHashTable.h b/src/Common/HashTable/StringHashTable.h index 6a8bdc06218..e18429688ed 100644 --- a/src/Common/HashTable/StringHashTable.h +++ b/src/Common/HashTable/StringHashTable.h @@ -153,7 +153,11 @@ template struct StringHashTableGrower : public HashTableGrower { // Smooth growing for string maps - void increaseSize() { this->size_degree += 1; } + void increaseSize() + { + this->size_degree += 1; + HashTableGrower::updateMask(); + } }; template diff --git a/src/Common/HashTable/TwoLevelHashTable.h b/src/Common/HashTable/TwoLevelHashTable.h index 35c224c53f8..71c62a34f33 100644 --- a/src/Common/HashTable/TwoLevelHashTable.h +++ b/src/Common/HashTable/TwoLevelHashTable.h @@ -21,6 +21,7 @@ struct TwoLevelHashTableGrower : public HashTableGrower void increaseSize() { this->size_degree += this->size_degree >= 15 ? 1 : 2; + HashTableGrower::updateMask(); } };