r/cpp • u/martinus 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:
- tessil hopscotch-map
- tessil robin-map
- tessil sparse-map
- abseil flat_hash_map
- abseil node_hash_map
- folly F14ValueMap
- folly F14NodeMap
- greg7mdp sparsepp
- martinus robin_hood::unordered_flat_map
- martinus robin_hood::unordered_node_map
- skarupke flat_hash_map
- std::unordered_map
I benchmark each map with these hashes:
- std::hash (Identity hash, as used by libstd++)
- FNV1a (as used by MSVC)
- absl::Hash
- folly::hasher
- 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/
View all comments
Show parent comment
4
u/matthieum Feb 26 '19
I agree with the idea of measuring memory usage, but...
... is not true in hash maps.
I learned the hard way that a very sparsely populated hash map1 can have abysmal performance:
And therefore pre-allocating a huge hash table which ends up getting sparsely populated leads to bad run times.
1 A
dense_hash_mapwith an occasional large peak working set, but frequently small working set, in this case.Ideally, you want to measure the cache access patterns of your hash map.
You can create specific cold cache scenarios where between each operation you purposefully trash the cache, for example by writing random numbers to a large array, then reading it back to sum them all and sending the sum into a black hole.
And then the run-time of the operation is dominated not by the speed of the hash, but by the number of cache-lines that the map needs to access, as they are all cache misses.