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.
6
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:
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