diff --git a/tdutils/td/utils/FlatHashTable.h b/tdutils/td/utils/FlatHashTable.h index 1d50585bc..101d12dc0 100644 --- a/tdutils/td/utils/FlatHashTable.h +++ b/tdutils/td/utils/FlatHashTable.h @@ -308,8 +308,11 @@ class FlatHashTable { template std::pair 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(args)...); + } + invalidate_iterators(); + node.emplace(std::move(key), std::forward(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)) {