Make begin() to return end() on empty tables. (#297)

Eliminate performance overhead in constructing begin() iterators on empty hash tables.
This commit is contained in:
wind
2026-04-08 13:28:28 +08:00
committed by GitHub
parent 8442f1c82c
commit 01c838b76a
2 changed files with 20 additions and 0 deletions
+19
View File
@@ -337,6 +337,21 @@ Timer _lookup(vector<T> &v, HT &hash, size_t &num_present)
return timer; return timer;
} }
// --------------------------------------------------------------------------
template <class HT>
Timer _traverse_empty(int64_t capacity, HT& hash)
{
hash.clear();
hash.reserve(capacity);
Timer timer(true);
for (auto& pair : hash)
if (pair.second)
fprintf(stderr, "should not be here");
return timer;
}
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
template <class T, class HT> template <class T, class HT>
Timer _delete(vector<T> &v, HT &hash) Timer _delete(vector<T> &v, HT &hash)
@@ -441,6 +456,10 @@ int main(int argc, char ** argv)
timer = _lookup(v, hash, num_present); timer = _lookup(v, hash, num_present);
//fprintf(stderr, "found %zu\n", num_present); //fprintf(stderr, "found %zu\n", num_present);
} }
else if (!strcmp(bench_name, "traverse_empty"))
{
timer = _traverse_empty(num_keys, hash);
}
else if(!strcmp(bench_name, "delete")) else if(!strcmp(bench_name, "delete"))
{ {
vector<int64_t> v(num_keys); vector<int64_t> v(num_keys);
+1
View File
@@ -1245,6 +1245,7 @@ public:
~raw_hash_set() { destroy_slots(); } ~raw_hash_set() { destroy_slots(); }
iterator begin() { iterator begin() {
if (empty()) return end();
auto it = iterator_at(0); auto it = iterator_at(0);
it.skip_empty_or_deleted(); it.skip_empty_or_deleted();
return it; return it;