Skip to content

Commit 0717136

Browse files
committed
Merge pull request #106353 from YYF233333/hashmap_zst
Optimize `HashMap` size for zero-sized Allocators
2 parents 4cd27d7 + 0babb2a commit 0717136

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

core/templates/hash_map.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,13 @@ template <typename TKey, typename TValue,
6666
typename Hasher = HashMapHasherDefault,
6767
typename Comparator = HashMapComparatorDefault<TKey>,
6868
typename Allocator = DefaultTypedAllocator<HashMapElement<TKey, TValue>>>
69-
class HashMap {
69+
class HashMap : private Allocator {
7070
public:
7171
static constexpr uint32_t MIN_CAPACITY_INDEX = 2; // Use a prime.
7272
static constexpr float MAX_OCCUPANCY = 0.75;
7373
static constexpr uint32_t EMPTY_HASH = 0;
7474

7575
private:
76-
Allocator element_alloc;
7776
HashMapElement<TKey, TValue> **elements = nullptr;
7877
uint32_t *hashes = nullptr;
7978
HashMapElement<TKey, TValue> *head_element = nullptr;
@@ -214,7 +213,7 @@ class HashMap {
214213
_resize_and_rehash(capacity_index + 1);
215214
}
216215

217-
HashMapElement<TKey, TValue> *elem = element_alloc.new_allocation(HashMapElement<TKey, TValue>(p_key, p_value));
216+
HashMapElement<TKey, TValue> *elem = Allocator::new_allocation(HashMapElement<TKey, TValue>(p_key, p_value));
218217

219218
if (tail_element == nullptr) {
220219
head_element = elem;
@@ -254,7 +253,7 @@ class HashMap {
254253
}
255254

256255
hashes[i] = EMPTY_HASH;
257-
element_alloc.delete_allocation(elements[i]);
256+
Allocator::delete_allocation(elements[i]);
258257
elements[i] = nullptr;
259258
}
260259

@@ -379,7 +378,7 @@ class HashMap {
379378
elements[pos]->next->prev = elements[pos]->prev;
380379
}
381380

382-
element_alloc.delete_allocation(elements[pos]);
381+
Allocator::delete_allocation(elements[pos]);
383382
elements[pos] = nullptr;
384383

385384
num_elements--;

0 commit comments

Comments
 (0)