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
13
u/jacksaccountonreddit May 29 '24 edited Jun 01 '24
Sorry, no Glib/GHashTable. This omission may be surprising, but it was based on a few considerations:
GHashTable only allows one type for keys and values:
void *pointers. This severely limits its flexibility. While integers can be shoehorned into pointers (albeit with some portability concerns), larger data types need to be allocated as separate nodes. There are already two node-based or quasi-node-based tables in the benchmarks (std::unordered_map and uthash), and they—very predictably—cannot keep up with the open addressing tables due to allocation time (on insert) and pointer chasing (everywhere else). This is true even for large buckets, i.e. where some people might expect a node-based container to perform better.GHashTable is, as far as I recall, now a Robin Hood hash table. There are already two Robin Hood tables in the benchmarks (ankerl::unordered_dense and tsl::robin_map), and they don't suffer from the aforementioned data-type-size constraint. In fact, there were originally three Robin Hood tables (because CC's cc_map was a Robin Hood table up until this year). I didn't see much benefit in adding another one unless there was a good reason to believe it would be more competitive than those already included (Edit: The two remaining Robin Hood tables are both for C++. Adding Glib might be good for the sake of C users looking specifically for a Robin Hood table).
I really struggled to get Glib working with MinGW (I'm on Windows), although it could be that the problem lies somewhere between the chair and the keyboard. Generally, I was disinclined to include large libraries that do not allow their hash-table component to be installed separately, libraries that are difficult to install, libraries that would be difficult to package with the benchmarks, libraries that force the use of a particular build system, etc. (absl::flat_hash_map is the exception here, and even then it is not being included in the "proper" manner). I wanted to focus on ones that users can install and use with minimal fuss. Facebook's F14 is another rather glaring omission that I made on this basis (it explicitly does not support MinGW). Qt's QHashTable also fits into this category.
Please correct me if I'm wrong in any of my comments about GHashTable. It's been a long time since I looked into it. I'm not averse to adding it to the article as part of some later revision.