Don't try to increase hash table if no new element inserted.

This commit is contained in:
levlam 2022-04-18 21:27:38 +03:00
parent a91bed2996
commit e8c3792776

View File

@ -308,8 +308,11 @@ class FlatHashTable {
template <class... ArgsT>
std::pair<NodePointer, bool> emplace(KeyT key, ArgsT &&...args) {
try_grow();
CHECK(!is_hash_table_key_empty(key));
if (unlikely(bucket_count_mask_ == 0)) {
CHECK(used_node_count_ == 0);
resize(8);
}
auto bucket = calc_bucket(key);
while (true) {
auto &node = nodes_[bucket];
@ -317,6 +320,13 @@ class FlatHashTable {
return {NodePointer(&node), false};
}
if (node.empty()) {
if (unlikely(used_node_count_ * 5 >= bucket_count_mask_ * 3)) {
resize(2 * bucket_count_);
CHECK(used_node_count_ * 5 < bucket_count_mask_ * 3);
return emplace(std::move(key), std::forward<ArgsT>(args)...);
}
invalidate_iterators();
node.emplace(std::move(key), std::forward<ArgsT>(args)...);
used_node_count_++;
return {NodePointer(&node), true};
@ -477,13 +487,6 @@ class FlatHashTable {
}
}
void try_grow() {
if (unlikely(used_node_count_ * 5 >= bucket_count_mask_ * 3)) {
resize(2 * bucket_count_mask_ + 2 + 6 * (bucket_count_mask_ == 0));
}
invalidate_iterators();
}
void try_shrink() {
DCHECK(nodes_ != nullptr);
if (unlikely(used_node_count_ * 10 < bucket_count_mask_ && bucket_count_mask_ > 7)) {