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:
parent
afd52e2576
commit
7ad8bb5be6
Notes:
sideshowbarker
2024-07-19 01:53:23 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/7ad8bb5be6b
1 changed files with 5 additions and 2 deletions
|
@ -74,6 +74,8 @@ private:
|
||||||
|
|
||||||
template<typename T, typename TraitsForT>
|
template<typename T, typename TraitsForT>
|
||||||
class HashTable {
|
class HashTable {
|
||||||
|
static constexpr size_t load_factor_in_percent = 60;
|
||||||
|
|
||||||
struct Bucket {
|
struct Bucket {
|
||||||
bool used;
|
bool used;
|
||||||
bool deleted;
|
bool deleted;
|
||||||
|
@ -348,8 +350,8 @@ private:
|
||||||
return *const_cast<Bucket*>(bucket_for_reading);
|
return *const_cast<Bucket*>(bucket_for_reading);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((used_bucket_count() + 1) >= m_capacity)
|
if (should_grow())
|
||||||
rehash((size() + 1) * 2);
|
rehash(capacity() * 2);
|
||||||
else if (usable_bucket_for_writing)
|
else if (usable_bucket_for_writing)
|
||||||
return *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; }
|
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 };
|
Bucket* m_buckets { nullptr };
|
||||||
size_t m_size { 0 };
|
size_t m_size { 0 };
|
||||||
|
|
Loading…
Add table
Reference in a new issue