Don't try to increase hash table if no new element inserted.
This commit is contained in:
parent
a91bed2996
commit
e8c3792776
@ -308,8 +308,11 @@ class FlatHashTable {
|
|||||||
|
|
||||||
template <class... ArgsT>
|
template <class... ArgsT>
|
||||||
std::pair<NodePointer, bool> emplace(KeyT key, ArgsT &&...args) {
|
std::pair<NodePointer, bool> emplace(KeyT key, ArgsT &&...args) {
|
||||||
try_grow();
|
|
||||||
CHECK(!is_hash_table_key_empty(key));
|
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);
|
auto bucket = calc_bucket(key);
|
||||||
while (true) {
|
while (true) {
|
||||||
auto &node = nodes_[bucket];
|
auto &node = nodes_[bucket];
|
||||||
@ -317,6 +320,13 @@ class FlatHashTable {
|
|||||||
return {NodePointer(&node), false};
|
return {NodePointer(&node), false};
|
||||||
}
|
}
|
||||||
if (node.empty()) {
|
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)...);
|
node.emplace(std::move(key), std::forward<ArgsT>(args)...);
|
||||||
used_node_count_++;
|
used_node_count_++;
|
||||||
return {NodePointer(&node), true};
|
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() {
|
void try_shrink() {
|
||||||
DCHECK(nodes_ != nullptr);
|
DCHECK(nodes_ != nullptr);
|
||||||
if (unlikely(used_node_count_ * 10 < bucket_count_mask_ && bucket_count_mask_ > 7)) {
|
if (unlikely(used_node_count_ * 10 < bucket_count_mask_ && bucket_count_mask_ > 7)) {
|
||||||
|
Loading…
Reference in New Issue
Block a user