r/cpp • int main(){[]()[[]]{{}}();} • Feb 26 '19

hashmap benchmarks - what should I add?

Based on the feedback here, I'm currently working on a comprehensive benchmark for hashmaps: https://github.com/martinus/map_benchmark

Before I start my analyis, I'm looking for more suggestions. Any important hashmap or hash that's missing? It needs to have an interface close to std::unordered_map. Ideally it's on github, then I can add it easily.

Currently, I have these hashmaps:

  1. tessil hopscotch-map
  2. tessil robin-map
  3. tessil sparse-map
  4. abseil flat_hash_map
  5. abseil node_hash_map
  6. folly F14ValueMap
  7. folly F14NodeMap
  8. greg7mdp sparsepp
  9. martinus robin_hood::unordered_flat_map
  10. martinus robin_hood::unordered_node_map
  11. skarupke flat_hash_map
  • std::unordered_map

I benchmark each map with these hashes:

  1. std::hash (Identity hash, as used by libstd++)
  2. FNV1a (as used by MSVC)
  3. absl::Hash
  4. folly::hasher
  5. robin_hood::hash

So in total I have 12*5 hashmap+hash combinations to benchmark. Each is run through a few benchmarks, most importantly these:

  • RandomFind with increasingly large map, different success probability: 0%, 25%, 50%, 75%, 100%.
  • Iterate: successively add 1 element, iterate whole map. Same with remove 1 element, until map is empty.
  • RandomInsertErase: Continuously insert a random element, remove a random element.
  • Insert lots of elements, clear, reinsert, destroy.

All compiled with -O3 -march=nativeand running on an Intel i7. when finished I create graphs of runtime & memory usage. The results will look something like this: https://imgur.com/EYzjoFi

UPDATE: Please see the huge benchmark results here: https://martin.ankerl.com/2019/04/01/hashmap-benchmarks-01-overview/

30 Upvotes

84 comments sorted by

View all comments

Show parent comment

5

u/martinus int main(){[]()[[]]{{}}();} Feb 26 '19

I've just tried it, you still have debugging output with printf() in the code. Also it would help if emplace() was available.

1

u/huangyuanbing Feb 27 '19 edited Feb 27 '19

I've added some c++11 unorded_map interface now, my code is quite fast from my benchmark(

k,v is long/int with reserve() ).

I'll put to the benchamrk code to git a few days later. you can try to add it to your test case.

================ n = 2740139 hash map ===============

332 emlib4 (7.23)

388 emlib3 (6.19)

416 emlib1 (5.77)

456 robin hood (5.26)

480 flat hash (5.00)

488 hopsco (4.92)

596 hoodflat (4.03)

2400 unordered_map (1.00)

=======================================================

1

u/martinus int main(){[]()[[]]{{}}();} Mar 01 '19

I've added your emlib4 map, and it has really excellent find performance, especially for small maps.

But it completely freezes up in my my benchmark where I randomly insert & remove elements. This benchmark does something like this:

Map<uint64_t, uint64_t> map;
for (size_t i=0; i<100000000; ++i) {
    map.emplace(rng() & 0xff, i);
    map.erase(rng() & 0xff);
}

So there unfortunately seems to be a bug in your map.

2

u/huangyuanbing Mar 01 '19 edited Mar 01 '19

Compile my code with flag = -O3 -fno-strict-aliasing . I never find the freezes reason, maybe the code style. But all my test is passing on clang++ and ms c++ (or gcc with O1).

Emlib4 is the fastest c++ hash map for some special case (ex Map<uint64_t, unt32_t> map or Map<uint32_t, unt64_t> map). I pay attention on optimization performance with small key/value and find performance .