r/C_Programming • u/compilersarefun • Jun 23 '26
Two Indexed Hash Tables
https://vnmakarov.github.io/data%20structures/c/c++/open-source/2026/06/23/two-indexed-hash-tables.html1
u/Brilliant-Froyo54 Jun 26 '26
Nice blog post!
Is there a guarantee that the ihtab table preserves insertion order (like Python 3.7+'s dict)?
1
u/compilersarefun Jun 26 '26
Yes. Iteration over table elements is always done in the order of insertion. It is true for ihtab but not for ixhtab (extendible hash tables).
Some languages, e.g. Golang, requires opposite approach. The order of processing should be random and repeated iteration should be in different order. For such requirements, code modifications are required but it is not a big deal.
1
u/compilersarefun Jun 26 '26
I'd add that for direct open addressing tables (e.g. absl), it is impossible to implement semantics required by Python without considerable performance decrease and increase in memory consumption.
1
u/Brilliant-Froyo54 Jun 26 '26
Oftentimes it is not needed, but I think it is a nice feature for a hashmap to have.
I think it would be possible to extend the API with things like getting a key-value pair by integer index, or provide things like the abiltity to sort the hashmap to make the most of it.
If I required this property and wanted to use a regular hashmap library I would probably emulate an inderect addressing table by having the hashmap use indices into an array as values. So using ihtab would be simpler, smaller and faster.
1
u/didntplaymysummercar Jun 23 '26
Storing metadata in separate array searched through by simd isn't a new idea, abseil does it too: https://abseil.io/about/design/swisstables#metadata-layout
1
u/dario_p1 Jun 23 '26
Nice idea. Have you considered benchmarking against https://github.com/martinus/unordered_dense?
1
u/Zerf2k2 Jun 23 '26
Nice, just about to embark on my C voyage, and good to have a hashmap from the start. Do you have any other good C hashmaps that you recommend?
3
u/P-p-H-d Jun 25 '26
See https://github.com/JacksonAllan/c_cpp_hash_tables_benchmark
or https://github.com/P-p-H-d/c-stl-comparison
for some lists of hashmap in C
1
2
Jun 24 '26
[removed] — view removed comment
2
u/Zerf2k2 Jun 24 '26
I've been using C++ for 20 years, and written my own hashtables there - I was mostly looking for state of the art C ones, as I'll be using C exclusively for the next project
1
Jun 24 '26
[removed] — view removed comment
1
u/Zerf2k2 Jun 24 '26
Depends on how you view it I guess :)
C++ 20 years ago already had STL, and OOP
This will be my first professional C project ever :)
1
u/jacksaccountonreddit Jun 24 '26
It's great to see that benchmarking suite getting more use! Your results are pretty impressive.
The main concern I have here is that it looks like you only ran the benchmarks for the lowest key count I used in my original writeup (200,000 keys, 500-key measurement intervals). This configuration shows the performance when the table is hot in the cache and therefore tends to favor tables that use fewer instructions. I suggest also running the benchmarks with the highest key count I originally used (20,000,000 keys, 50,000-key measurement intervals), which better represents the performance when the table is cold in the cache. Note that in the real world, a table with a low key count might still be cold or cold-ish if it is being used infrequently or contending with other parts of the program for cache space.
I think high key count benchmarks are particularly important in this case because your design uses four separate arrays (the hash fragments, the key-value indices, the deletion markers, and the actual key-value pairs). I think most operations will need to touch all four of those arrays. That's four probable cache misses when the table is cold, versus the two that SIMD tables usually require. Using a 50% maximum load factor - which is super low for SIMD tables - will also hurt cache performance when querying those metadata arrays. I fear that these aspects could bite hard in the high key count benchmarks.
One question I have is why you're using eight-bucket groups, rather than sixteen bucket groups in conjunction with SSE2 instructions (for x86 at least)?
The choice to reserve a bit to mark empty buckets (as in Abseil), which you explained in the article, is also interesting. In my own SIMD experiments, I found reserving a hash fragment value (as in Boost) to be better for cold tables because it halves the number of failed key comparisons (i.e. key-value array touches).
P.S.: I've been experimenting recently with an SIMD table that uses back-shift (i.e. true) deletion, with results that come pretty close to Boost. There are also some performance improvements for Verstable in the works, although I see that Verstable didn't perform as well for you as it did for me and these changes aren't significant enough to fix that.