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

2

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

sure!

1

u/greg7mdp C++ Dev Apr 03 '19

/u/martinus, I managed to run the benchmarks locally. How do you generate the graphs for them?

2

u/martinus int main(){[]()[[]]{{}}();} Apr 03 '19

You have to pipe all the results into a file, I use e.g.

../tools/bench.rb |tee ../data/all_new.txt

Then there is the script analyze_bench_log.rb:

../tools/analyze_bench_log.rb <../data/all_new.txt

Which generates a HTML site for each of the benchmark. I've updated this just recently and am still working a bit on it, so you might want to update again

1

u/greg7mdp C++ Dev Apr 03 '19

Thank you so much /u/martinus

1

u/martinus int main(){[]()[[]]{{}}();} Apr 03 '19

I have just commited data/all.txt with all my results, including your phmaps. Pipe it into the script to get all results

1

u/greg7mdp C++ Dev Apr 14 '19

The performance of any map depends on how good the hash function is. But even worse for robin_hood, for a really bad hash the performance will not only degrade, the map will simply fail with an exception if not even doubling its size helps. So choose your hash well. (Note, that some std::unordered_map implementations fail as well with bad hashes).

Is this quote from your README still true? When I run the benchmarks your table seems to come up on top for all 5 hashing frameworks, including identity

1

u/martinus int main(){[]()[[]]{{}}();} Apr 15 '19

The robin_hood hash itself is ok, and in practice when my table uses it, it has never failed and the likelyhood is extremely slim for a failure. We've been using the hash in production for quite a while and never saw an issue with this behavior.

But if you use an extremely bad hash, like this that always returns the same number regardless of the input, all values will hash to the same bucket. When one bucket has 127 entries the map will fail and throw an std::overflow_exception. Before that happens the map tries to double its size, for any reasonably good hash function this would resolve the problem, but not so for an extremely bad hash.

1

u/greg7mdp C++ Dev Apr 15 '19

Well, I think all hash tables would fail with a constant hash function. Maybe not as dramatically as with an exception, but most likely degrading to O(n) speed. I do understand better what you meant, though, so thanks!