FlatHashTable: optimize find.

This commit is contained in:
levlam 2022-03-11 14:10:10 +03:00
parent 2a44a0f357
commit 3eda5b17ac

View File

@ -8,8 +8,6 @@
#include "td/utils/common.h" #include "td/utils/common.h"
#include "td/utils/HashTableUtils.h" #include "td/utils/HashTableUtils.h"
#include "td/utils/MapNode.h"
#include "td/utils/SetNode.h"
#include <cstddef> #include <cstddef>
#include <initializer_list> #include <initializer_list>
@ -203,20 +201,7 @@ class FlatHashTable {
} }
Iterator find(const KeyT &key) { Iterator find(const KeyT &key) {
if (unlikely(nodes_ == nullptr) || is_hash_table_key_empty(key)) { return create_iterator(find_impl(key));
return end();
}
auto bucket = calc_bucket(key);
while (true) {
auto &node = nodes_[bucket];
if (EqT()(node.key(), key)) {
return create_iterator(&node);
}
if (node.empty()) {
return end();
}
next_bucket(bucket);
}
} }
ConstIterator find(const KeyT &key) const { ConstIterator find(const KeyT &key) const {
@ -232,16 +217,7 @@ class FlatHashTable {
} }
Iterator begin() { Iterator begin() {
if (empty()) { return create_iterator(begin_impl());
return end();
}
if (begin_bucket_ == INVALID_BUCKET) {
begin_bucket_ = detail::get_random_flat_hash_table_bucket(bucket_count_mask_);
while (nodes_[begin_bucket_].empty()) {
next_bucket(begin_bucket_);
}
}
return create_iterator(nodes_ + begin_bucket_);
} }
Iterator end() { Iterator end() {
return create_iterator(nullptr); return create_iterator(nullptr);
@ -301,16 +277,17 @@ class FlatHashTable {
} }
size_t erase(const KeyT &key) { size_t erase(const KeyT &key) {
auto it = find(key); auto *node = find_impl(key);
if (it == end()) { if (node == nullptr) {
return 0; return 0;
} }
erase(it); erase_node(node);
try_shrink();
return 1; return 1;
} }
size_t count(const KeyT &key) const { size_t count(const KeyT &key) const {
return find(key) != end(); return const_cast<FlatHashTable *>(this)->find_impl(key) != nullptr;
} }
void clear() { void clear() {
@ -333,7 +310,7 @@ class FlatHashTable {
return; return;
} }
auto it = begin().get(); auto it = begin_impl();
auto end = nodes_ + bucket_count(); auto end = nodes_ + bucket_count();
while (it != end && !it->empty()) { while (it != end && !it->empty()) {
++it; ++it;
@ -395,6 +372,36 @@ class FlatHashTable {
used_node_count_ = other.used_node_count_; used_node_count_ = other.used_node_count_;
} }
NodeT *begin_impl() {
if (empty()) {
return nullptr;
}
if (begin_bucket_ == INVALID_BUCKET) {
begin_bucket_ = detail::get_random_flat_hash_table_bucket(bucket_count_mask_);
while (nodes_[begin_bucket_].empty()) {
next_bucket(begin_bucket_);
}
}
return nodes_ + begin_bucket_;
}
NodeT *find_impl(const KeyT &key) {
if (unlikely(nodes_ == nullptr) || is_hash_table_key_empty(key)) {
return nullptr;
}
auto bucket = calc_bucket(key);
while (true) {
auto &node = nodes_[bucket];
if (EqT()(node.key(), key)) {
return &node;
}
if (node.empty()) {
return nullptr;
}
next_bucket(bucket);
}
}
void try_grow() { void try_grow() {
if (unlikely(nodes_ == nullptr)) { if (unlikely(nodes_ == nullptr)) {
resize(8); resize(8);