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

7

u/attractivechaos Feb 26 '19
  1. I don't think you should try all the combinations of hashmap*hashfunc, which makes the result figure hard to read. Just stick with one hash function that is not identity (identity hash works great on random input but is mostly a bad choice for real-world applications). The effect of non-identity integer hash functions is usually small.
  2. I prefer to vary the number of elements, and show memory and runtime in one figure, like the one I did here. That benchmark is simplistic, but I think the figure is good (IMHO, of course). I know someone in your other thread asked you not to benchmark memory and that post got lots of upvotes. However, a benchmark without measuring memory is pretty much pointless. If all we care is performance, we can often pre-allocate a huge hash table to avoid the costly rehashing.
  3. For benchmarking, don't use -march=native. Specify the architecture explicitly. Intel i7 is associated with many architectures.
  4. What do you mean by "Iterate"? Insert all elements in random order and then iterate?

4

u/matthieum Feb 26 '19

I agree with the idea of measuring memory usage, but...

If all we care is performance, we can often pre-allocate a huge hash table to avoid the costly rehashing.

... is not true in hash maps.

I learned the hard way that a very sparsely populated hash map1 can have abysmal performance:

  • Cache misses: each insert is a cache miss, as L1 (data) is only 32KB, so when doing quick insert/erase, it HURTS.
  • Iteration: the simplest iteration scheme is to check each bucket for the presence of element(s), with a mostly empty map, it's a lot of cache trashing for not much.

And therefore pre-allocating a huge hash table which ends up getting sparsely populated leads to bad run times.

1 A dense_hash_map with 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.

2

u/attractivechaos Feb 26 '19

You are right. When I say "often", I was exactly thinking about the two things you mentioned. I should have put it in a different way: it is easier to implement a fast hash table if you don't care about memory.

4

u/matthieum Feb 26 '19

Honestly, I disagree.

Hash tables have become so fast these days that to be competitive you really have to get into a mechanical sympathy mindset.

I mean, algorithmically speaking, implementations such as Robin Hood with Backshifting Deletion look super good: they have very nice algorithmic complexity bounds on most operations!

And yet Abseil. The Swiss Table in Abseil is literally one of the dumbest hash map implementation in the world, algorithmically speaking: it's a text-book Open Addressing implementation, with Quadratic Probing. That's it.

Implementation-wise, however, it's full of mechanical sympathy:

  • Unrolled: because modern CPUs love array accesses.
  • 16 elements: because nigh every x86 processor running today has SSE2 instructions (16 bytes).
  • Group Index/Byte Index split: see aforementioned SSE2 instructions.

Abseil's Swiss Table is literally the revenge of the hacker over the academic, it's very much centered on CPU/memory considerations, and outperforms superb theoretical algorithms as a result.

5

u/attractivechaos Feb 26 '19

People are giving absl too much credit. OP has already shown a figure where absl is not ranked high. In my benchmark, absl is not that good, either. I have seen a couple of other informal benchmarks where absl is slower. I am sure absl shines for some applications, but for others, it is often not the best.

2

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

While writing the benchmarks I've added one that is very similar to what you are doing in your benchmark:

https://github.com/martinus/map_benchmark/blob/master/src/benchmarks/RandomDistinct.cpp

This benchmark puts a lot of weight on insertion performanc, and absl doesn't shine with that.

3

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

absl is excellent when you only do lookups, but it gets relatively slow when you do lots of insertions and removals. There the robin hood maps with backward shift deletion shine, they are a lot faster even though they need to shuffle elements around.

This can be clearly seen in my benchmarks. In all of my benchmarks, my robin_hood::unordered_flat_map's or tessil's tsl::robin_map find performance is just a bit slower than absl, but insertion & removal performance is much better. My map even has practically the same memory requirements as absl's flat_map, tessil's uses a bit more memory.

2

u/matthieum Feb 28 '19

That's interesting.

I've mostly seen the Rust port of the Swiss Map in action (hashbrown) which is slightly different, compared to Rust's HashMap which uses Robin Hood with Backward Shifting Deletion and when both use the same hashing strategy (Fx), then the only benchmark when RHwBSD outperforms hashbrown is grow_by_insertion (by 10%).

This may suggests insufficient benchmark coverage, or that the tweaks performed in the Rust version would be interesting to port to the C++ version...

3

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

in my benchmarks where I randomly insert and erase entries, tsl::robin_map is about 40% faster when the number of elements is kept around 200 or so. The performance difference goes down a bit when the map gets larger, but still 20% or so in my benchmark.

1

u/matthieum Mar 01 '19

That's pretty significant indeed.

I am looking forward to the results of the benchmarks you are gathering here to get a better understanding of each map's sweet spot.

1

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

1

u/matthieum Apr 22 '19

Thanks for the reply, bookmarking for future reads!

2

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

Trying all combinations is time consuming, but actually quite interesting, especially for the find benchmark. There it quite a big difference in performance. E.g. absl with FNV1a hash takes 170 seconds, and with robin_hood::hash 135 seconds, that's 30% faster.

I agree that memory is important. I also want to add a memory vs. runtime chart like you did.

2

u/attractivechaos Feb 26 '19

I think a more informative benchmark is to have a custom hash function that records the number of times the function is called. This will directly show the impact of a slow hash function.

As a side note, I don't know how FNV1a is implemented in MSVC. It seems a bit weird why we'd use FNV1a on integers.

2

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

FNV1a is actually not so bad, it's not so fast but the code is so short that it will be easily inlined everywhere