r/cpp • u/martinus int main(){[]()[[]]{{}}();} • Sep 04 '26
Which C++ Hashmap Should You Actually Use?
https://martin.ankerl.com/which-hash-map/I recently did a big update to my ankerl::unordered_dense::map which now features several optimizations, bug fixes, and most importantly an SSE optimized lookup which brings the lookup speed quite close to the fastest competitor which is boost::unordered_flat_map. Get it here: https://github.com/martinus/unordered_dense
Since it is not easy to decide what map is best for one's usecase, I have redone and improved the benchmarks I did a while ago, crunched some numbers and tried to compress everything into a quiz that helps you figure out what to use.
While doing the numbers, a few things surprised me:
- My random access benchmark was not random enough. The CPU was able to optimize the branch predictor in a way I did not think was possible. I have fixed this for the new numbers. Clever girl.
- Even though the absl maps are very similar in concept to the boost maps, they perform badly if you expect lots of misses. 1.6 times slower than boost.
- I didn't think SSE optimization of my
unordered_densemap would be any benefit. Thanks to AI, I found a way to speed up random access a lot, it is now very close toboost::unordered_flat_map. - Heavy insert & erase on flat maps require rehashing every once in a while due to the accumulation of tombstones. That means some insert/erase operation will have very high latency. This does not happen for a robin-hood hashmap like my
unordered_densemap.
12
u/joaquintides Boost author Sep 04 '26
Excellent tool and work! Are you going to publish some writeup on your latest updates to udm?
44
u/instantly-invoked Sep 04 '26 edited Sep 04 '26
I just wanted to say thanks as I do use your map most of the time! I've also used it to make a sharded locked concurrent map because of its excellent memory usage properties. Seriously, thanks for all your hard work on this.
edit: The quiz is great! I answered with how I typically use unordered maps and handle dependencies and got yours first with STL coming in second, which is honestly how I'd rank them (if I don't need speed I'm just gonna use what's already there).
5
u/NilacTheGrim Sep 04 '26
I love your map man. Much improved over your previous work on robin_hood. Excellent job!
4
u/SamG101_ Sep 04 '26 edited Sep 04 '26
been using ur hashmap for a while now! also regarding 4.11, the SSE optimiation crashes with module exporting due to an alignment-propagation bug in module BMIs (on gcc) - 127193 β Alignment problem when using modules - would it be possible that this optimization is gated behind a macro /
-Dcmake option? thanks!25
u/mark_99 Sep 04 '26
Yep, this is why I roll my eyes when someone claims "the fastest hashmap", it absolutely depends on what you are doing.
Re perf, fancy hashmaps are problematic in low latency environments, and while Robin Hood isn't as pathological as tombstones there is still a variable amount of reshuffling on insert. What would be good is histograms (or at least percentiles) rather than averages for the various operations.
4
u/rdtsc Sep 04 '26
The quiz redirects you to the node-based if you need stable pointers to values. But that's not the only way. The abseil docs recommend
flat_hash_map<K, std::unique_ptr<V>>in that case, and the node-based map only if pointer stability of keys is needed.4
u/SirClueless Sep 05 '26
Worth mentioning also the restrictions on hashing? Abseil functions atrociously if the hash is not high-quality and has a unique and bespoke way of overriding the hashing of user types. Personally the number one reason I've seen for lock-in. A lot of codebases paid the upfront costs of implementing Abseil's hash conventions for all their vocabulary types back when it offered a night-and-day difference from
std::unordered_map, and now even thoughboosthas a much saner and easier-to-adopt hash mechanism (for example, works fine with user types that overridestd::hash) the win from switching is not as clear-cut.Another big constraint I think is Google's hash randomization, which is declaration by fiat that depending hash stability is always a mistake, and that you should expect a different iteration order when the same program does the same sequence of insertions and deletions.
1
u/g_0g Sep 04 '26
Great work on the updates! Always nice to read from one of the pioneers.
You mentioned slow downs with misses and tombstones accumulation.
Boost is indeed way better for the first, but Abseil design allows to greatly limit tombstone creation on erase.
Boost might also hides a higher latency in the context of micro-benchmark, as it needs to access a sizeable lookup table on each find (quickly pushed in L1 cache on repeated calls). I think Google tried to mainly benchmark theirs maps in production contexts by comparison.
Anyway, you might be interested in checking 2 of my own competitive experiments:
one tombstone-free map (inspired by Boost) and one speed-oriented (inspired by Abseil)
https://github.com/gaujay/indivi_collection/tree/main/bench/flat_unordered
2
u/Fabulous-Meaning-966 Sep 04 '26
Hey, just wondering if you've checked out bidirectional linear probing as an alternative to Robin Hood? There's a Java integer hash set benchmark here that shows it's consistently superior:
https://github.com/senderista/hashtable-benchmarks
1
u/Just_Government3790 Sep 11 '26
Same-machine interleaved A/B or the ordering means nothing. And the losses go in the same table or the win is uninterpretable.
My table runs 1.185x/1.199x/1.482x/1.811x losses next to 0.68x/0.83x wins, whole-loop timer, no pinning, limits stated. A "random" pattern the branch predictor learns is the classic microbenchmark bug. Validate the access trace first.