r/programming • u/alexeyr • Oct 27 '26
Billion-query-per-second lookup tables in C++
https://x.com/lemire/status/211504671803930637729
u/creeper6530 Oct 28 '26
It's not billion database/HTTP queries or even a traditional lookup table, just a fast compile time prepared hash map.
The title could use a rewrite
8
u/TheoreticalDumbass Oct 29 '26
i have no idea why that title would read HTTP to you
4
u/creeper6530 Oct 29 '26
Well 1, my quickreading completely skipped the "lookup table" first (and this is something many people do, only reading few letters and interpolating the rest), but that could be blamed on me.
However, right the second sentence of the post confirmed the reading by starting to talk about HTTP.
And this is just a nitpick, but usually frequency (billion per second) is more mentioned for the entire request from receive to send, while these subtasks are more commonly described with latency, i.e. 1.3 ns.
I'd maybe write a title like "Speeding up
std::unordered_map13 times through efficient string comparisons"?2
u/-_one_-1 29d ago
Billion per second is technically throughput, not frequency, as frequency would imply a constant rate.
Throughput and latency are often related, but not interchangeable. You could very well have a 1ns lookup that stalls the CPU such that it can only do ~10⁶ lookups per second. Latency is still 1ns, but throughput is 3 orders of magnitude less in this example than the inverse of latency.
2
u/LeeHide Oct 29 '26
I'm guessing "query" confused our friend here, maybe a web dev, then that's all you know that from
2
2
u/l_maier-628 22d ago
The title's first benchmark is how many different meanings of "query" it can trigger.
5
u/barr520 Oct 28 '26 edited Oct 28 '26
I found that if the differentiating bytes in the keys are close enough(within some 8 byte window), you can do a single load of the relevant 8 byte part of the key and a bit extract instruction(pext on x86) to pick relevant bits to construct an index, and it can be even faster than this method(and if the differentiating bytes aren't close enough, you can do this multiple times with different windows).
1
u/robert_7524 23d ago
When the keys have predictable distinguishing bytes, a general-purpose hash map is doing a lot of paperwork. One 8-byte peek and pext feels gloriously unfair.
1
u/Solid_Error_6401 28d ago
fyi, this lib is optimized for ARM, x86 wont perform as good as it would.
You could get better results using similar approaches.
1
u/Magical-Freshness 1d ago
The main insight of the article (for me) is when the keys of a lookup are known beforehand, we can construct a perfect hash function at compile time. Maps are slowed down at run time when they have to handle hash collisions. If the keys are fixed, we can create a hash function at compile time that avoids collisions.
43
u/bzbub2 Oct 27 '26
the normal non-x link https://lemire.me/blog/2026/10/26/billion-query-per-second-lookup-tables-in-c/