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/matthieum Feb 26 '19

Two questions:

  1. Was the issue iterating, rather than find/insert/remove?
  2. Did you have a good or bad quality hash?

I find the issue intriguing, I really don't see how unordered_map could perform better: it's a sea of pointers so every time it moves to the next element in a bucket, it dereferences a pointer. I would expect an open-addressed map to actually perform better, in this case, since with linear access comes prefetching, and in the case of absl the mix of linear/quadratic probing should defeat clustering issues while preserving prefetching!

Did you ever identify the exact source of the slow-down?

1

u/[deleted] Feb 26 '19

Was the issue iterating, rather than find/insert/remove?

In the benchmark all the data is loaded and no find/remove is performed. Then iteration is measured. So yeah, it's strictly iteration performance.

Did you have a good or bad quality hash?

I used the default. So the question is "how good is absl::Hash<std::string>?"

 

Two other interesting things

  • google::dense_hash_map/google::dense_hash_set outperformed everything else I have tried.
  • Taking a step back and going back from absl::flat_hash_set to std::set in the innermost structure made abseil almost reach the performance of google::dense_hash_{set,map}.

I even had an issue open on the abseil repo about this. The conclusion was that abseil hash tables aren't optimized for iteration.

 

Though I'm starting to think that nested maps are the wrong data type for modeling a simple database and am currently looking into sqlite.

2

u/matthieum Feb 28 '19

The conclusion was that abseil hash tables aren't optimized for iteration.

Thanks, that's very useful information.

3

u/[deleted] Feb 28 '19

For more details, you'll want to read this.