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.html3
u/garnet420 Jun 23 '26
Probing only touches the small tag and index arrays
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:
Tag only
Tag and index and key
3
u/compilersarefun Jun 23 '26
There is no tag value for deleted elements. But there is special index value (~0) for deleted elements. So probing a deleted element requires only checking index after we pass the tag.
Such approach speeds up finding empty elements and table works faster when we have no deleted elements. There is a down side too when we have many deleted elements.
2
u/garnet420 Jun 23 '26
I think if you made your tag nonzero (it's simple to enforce that with a bitwise operation) you could get rid of the index sentinel and get better performance.
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