Skip to content

Commit 01c838b

Browse files
authored
Make begin() to return end() on empty tables. (#297)
Eliminate performance overhead in constructing begin() iterators on empty hash tables.
1 parent 8442f1c commit 01c838b

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

examples/bench.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,21 @@ Timer _lookup(vector<T> &v, HT &hash, size_t &num_present)
337337
return timer;
338338
}
339339

340+
// --------------------------------------------------------------------------
341+
template <class HT>
342+
Timer _traverse_empty(int64_t capacity, HT& hash)
343+
{
344+
hash.clear();
345+
hash.reserve(capacity);
346+
Timer timer(true);
347+
348+
for (auto& pair : hash)
349+
if (pair.second)
350+
fprintf(stderr, "should not be here");
351+
352+
return timer;
353+
}
354+
340355
// --------------------------------------------------------------------------
341356
template <class T, class HT>
342357
Timer _delete(vector<T> &v, HT &hash)
@@ -441,6 +456,10 @@ int main(int argc, char ** argv)
441456
timer = _lookup(v, hash, num_present);
442457
//fprintf(stderr, "found %zu\n", num_present);
443458
}
459+
else if (!strcmp(bench_name, "traverse_empty"))
460+
{
461+
timer = _traverse_empty(num_keys, hash);
462+
}
444463
else if(!strcmp(bench_name, "delete"))
445464
{
446465
vector<int64_t> v(num_keys);

parallel_hashmap/phmap.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,6 +1245,7 @@ class raw_hash_set
12451245
~raw_hash_set() { destroy_slots(); }
12461246

12471247
iterator begin() {
1248+
if (empty()) return end();
12481249
auto it = iterator_at(0);
12491250
it.skip_empty_or_deleted();
12501251
return it;

0 commit comments

Comments
 (0)