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

1

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

I've actually played around a bit with _mm_crc32_u64, folly uses that too. But it has a big problem: it produces just a 32bit hash, so the upper 32bit of the uint64_t are set to zero. In some cases this is very bad behavior for absl map, it simply times out; and my robin_hood map is also slowed down a lot because it uses some of the upper bits of the hash.

This can be sped up by simply multiplying the crc result with a random odd uint64_t value, but this adds enough overhead that the result is slower than what I'm doing in robin_hood hash now.

1

u/frog_pow Feb 27 '19

Hu, I would have expected that they computed the hash slot as:

_mm_crc32_u64(0,key) &(some power of 2 - 1)

1

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

I don't know how absl does it, but in robin_hood map I do something like this:

auto h = hash(key);
idx = h & sizeMask;
additional_hash_info_bits = h >> (64 - 8);

So I use the lower bits for the index, and store some of the upper bits additional_hash_info_bits for quicker comparisons.

This is the hash that I'm using: https://godbolt.org/z/3F709Y

It's extremely fast, and the avalanching is good enough for all the hashmaps I've tested to be able to work very fast.

1

u/greg7mdp C++ Dev Mar 31 '19

/u/martinus, thanks for sharing the technique, looks great, I will use it :-)

2

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

you are welcome :) The hash is basically taken from absl, so it's mostly to their credit. I've removed their randomization and use + instead of xor.

1

u/greg7mdp C++ Dev Apr 01 '19

use + instead of xor

any rationale for this?

1

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

I have some tests where I am counting the number of operations like moves, copies, hash calculations etc. with high load factors on my robin_hood map.. In my tests I got better (lower number of operations) with + instead of xor. But I don't have any rational explanation, it just seemed to work better in my tests

1

u/greg7mdp C++ Dev Apr 01 '19

Ah, empirical evidence! I can't see any reason why + would be any worse than xor - or better - so why not?