r/cpp • 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.html
34
Upvotes
r/cpp • u/compilersarefun • Jun 23 '26
7
u/g_0g Jun 23 '26 edited Jun 23 '26
That's an interesting design, thanks for sharing.
Here are a few notes if you're interested:
- Benchmark images for Jackson Allan don't show (can be found on repo though)
- You should instrument your code to mesure real average/max probe length and comparison ops (for find hit/miss in particular)
- You should benchmark with default hash functions too, as good map design allow for using poorer but faster hashers
- Allowing high load factor is a positive thing, it means being able to retain good perf while maximizing memory usage. Max load factor can usually be reduced to improve access speed at the cost of extra memory.
- Swiss tables have some of the best iterations speed, only slightly beaten by contiguous-value-storing maps
- You can use quadratic probing with open addressing, it's just usually not worth it (a bit slower, less locality)
- Your map does not recycle deleted items as it goes, leading to rebuilding your arrays more often and at unexpected times. Maybe at least use an intrusive free list to try reusing slots.
Now the interesting part:
Abseil use the same 7-bits hash tags in separated array as ihtab, but don't need an extra indirection + cache miss to follow an index. Same for Boost, even if hash tags are a bit different.
Yet the benchmarks show that ihtab is as fast/faster (e.g. for find hit/miss). This is surprising, how do you explain these results?
Loading the index array in memory should trash L1 cache lines and occur a penalty for big maps that don't fit. It should also cause extra costly cache misses on cold maps.
See Boost blog for details about Swiss tables design choices and performance profiles:
https://bannalia.blogspot.com/2022/11/inside-boostunorderedflatmap.html
3
u/garnet420 Jun 23 '26
I'm a little confused by this. When does probing touch the index array but not the key? My impression was that you'd have two cases: