From 7ad8bb5be6b34dba19825a83938f09c4cf197e2d Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 16 Oct 2020 08:32:35 +0200 Subject: [PATCH] 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. :^) --- AK/HashTable.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/AK/HashTable.h b/AK/HashTable.h index 8d3bf7cd2a..cd53fb202b 100644 --- a/AK/HashTable.h +++ b/AK/HashTable.h @@ -74,6 +74,8 @@ private: template 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_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 };