r/C_Programming • u/jacksaccountonreddit • May 29 '24
An Extensive Benchmark of C and C++ Hash Tables
https://jacksonallan.github.io/c_cpp_hash_tables_benchmark
130
Upvotes
r/C_Programming • u/jacksaccountonreddit • May 29 '24
56
u/jacksaccountonreddit May 29 '24 edited May 29 '24
Hello r/C_Programming :)
This article is the product of over a year of intermittent research into, and experimentation with, hash-table designs. It compares the performance of 15 libraries (eight in C and seven in C++), two of which I authored and previously shared on this subreddit, across seven benchmarks and three different table configurations. It delves into the implementation details of each table tested, makes some general observations about hash-table designs (namely separate-chaining tables, classic linear- and quadratic probing open-addressing tables, Robin Hood tables, SIMD-accelerated tables, and hybrid open-addressing/separate chaining tables), and offers some advice about which libraries readers should consider using.
Hope someone finds it interesting!
3
u/kansetsupanikku May 30 '24
Wonderful analysis, thanks for sharing!
Do you perhaps have some estimates of how it might compare to
hsearch_rLinux/glibc extension? It's easily available on that platform and should be memory efficient, but I wonder how far it might lag behind the state of the art performance you are delving into.3
u/8d8n4mbo28026ulk May 30 '24
Interesting results.
I had found that even in open-addressing linear/quadratic probing tables, which are very susceptible to bad hash functions, sometimes it's actually faster to use the identity function anyway (for integer keys, ofcourse).
In my testing on Sandy Bridge (so no AVX2 and/or BMI), a simple open-addressing with quadratic probing table (like
khash, but more optimized) is as fast asabsl's table on (maybe existing-)insertions and lookups (for 32-bit integers as key/value) up to ~1 million keys, with a load factor of0.5.It'd be interesting to also benchmark a variant of the 16-char c-string key, 64-bit case, where you manually cache the computed hash in the key type. It would eliminate the overhead of rehashing keys and speed up equality checks. Curious how well this favors the tables which don't store hash fragments and/or tables which rehash semi-frequently.
I also wonder how well the node-based containters would fair when paired with a pool allocator (in particular, for the chains). I'd expect some speedup.
5
u/HaydnH May 29 '24
Glib2's hash table isn't included? Or am I being blind?It's my usual go to just because I know it (although the whole gint-to-pointer back and forth seems a little... verbose?), it would be good to see what I use Vs what's out there...
8
u/quirktheory May 29 '24
Such a lovely, thorough analysis. Thank you.
7
u/9Boxy33 May 29 '24
This looks to be very useful!
10
u/EpochVanquisher May 29 '24
Damn, kudos, that’s a very thorough analysis.
4
u/RafBenson May 29 '24
A great and very informative analysis. Thank you for sharing.
1
u/commandersaki Jun 02 '24
Thank you so much. I've been going through an interview for a C++ position at a trading company and I was quizzed twice in two separate interviews about
std::unordered_map. I realise it has performance penalties by being mandated to use separate chaining (to enforce reference stability and iterator stability), but I didn't know how quantifiable that is.It's also really nice to see the
boost::unordered_mapis quite useful.I also love that you've shown a few implementation of hash tables available for C. I was familiar with uthash but I found it pretty difficult to integrate with because it is intrusive.
On that note, has anyone seen a hash table that makes use of
hcreate/hsearch? I've never understood how to use it.1
u/Timzhy0 May 30 '24 edited May 30 '24
With all due respect for the work and effort, isn't it somewhat obvious that different designs have different trade-offs? IMO the goal should not be to find the best versatile hashtable, because no matter what, you can pick a more specific data structure for the task (e.g. say, if I know I am going to loop over a lot, most likely I'd want key value pairs stored in a separate backing array). In some other cases, maybe I don't care about deletion or my deletion pattern happens in batch (say a compiler symbol table, when entering/exiting local scopes). I can give more examples but hopefully the point is clear: "custom" trumps "versatile". And even if we insist on having an ergonomic and generic HashMap<K,V> in my opinion, if performance is valued, we should really at least have a layer comptime dispatch it to more appropriate implementations at least based on the types (and their sizes, e.g. string with variadic len clearly need auxiliary backing storage, and possibly benefit from storing a hash in the slot, but integer keys do not).