AK: Tune HashTable load factor

Double the capacity when used+deleted buckets crosses 60% of capacity.
This appears to be a sweet spot for performance based on some ad-hoc
testing with test-js. :^)
This commit is contained in:
Andreas Kling 2020-10-16 08:32:35 +02:00
parent afd52e2576
commit 7ad8bb5be6
Notes: sideshowbarker 2024-07-19 01:53:23 +09:00

View file

@ -74,6 +74,8 @@ private:
template<typename T, typename TraitsForT>
class HashTable {
static constexpr size_t load_factor_in_percent = 60;
struct Bucket {
bool used;
bool deleted;
@ -348,8 +350,8 @@ private:
return *const_cast<Bucket*>(bucket_for_reading);
}
if ((used_bucket_count() + 1) >= m_capacity)
rehash((size() + 1) * 2);
if (should_grow())
rehash(capacity() * 2);
else if (usable_bucket_for_writing)
return *usable_bucket_for_writing;
@ -365,6 +367,7 @@ private:
}
size_t used_bucket_count() const { return m_size + m_deleted_count; }
bool should_grow() const { return ((used_bucket_count() + 1) * 100) >= (m_capacity * load_factor_in_percent); }
Bucket* m_buckets { nullptr };
size_t m_size { 0 };