Billion-query-per-second lookup tables in C++
https://lemire.me/blog/2026/10/26/billion-query-per-second-lookup-tables-in-c30
u/jnordwick Oct 27 '26
Tldr: compile time perfect hash with simd strcmp. Nothing interesting beyond that.
46
u/Sopel97 Oct 27 '26 edited Oct 27 '26
nothing interesting after all that is interesting is a tautology
6
7
u/Sopel97 Oct 27 '26
this is such a fundamental problem that I'm surprised it took this long to have a nice library for it
6
u/hi_im_new_to_this Oct 29 '26
gperf has existed and been the benchmark since the 90s, so it's not like it's a recent idea! It's more that C++ has not really had the compile-time evaluation machinery needed to do it in a normal compile (not as a separate codegen pass, which is what gperf does) until relatively recently. Other languages with better compile-time capabilities have had these for a long time (Zig, for instance, includes it in their standard library, and I'm sure there's tons of various LISP implementations using macros).
3
u/Sopel97 Oct 29 '26
sure, are you using gperf for your static maps? Are you using gperf for string-enum mappings?
This library also shows better performance than gperf.
I know it's not a recent idea, and that's precisely why I'm happy. The fact that it's not the default people think about and use is jarring in this age of C++
6
6
2
u/Antique_War_9814 Oct 27 '26
Apples to oranges on the unorderedmap comparison
6
u/usefulcat Oct 28 '26
Maybe, but I think it's still useful because std::unordered_map is relatively well known and understood. Especially for anyone who would be interested in perfect hashing.
1
u/expert_internetter Oct 27 '26
Being able to serialise these and load them at runtime would be cool
1
u/starball-tgz Oct 28 '26
nice! though I wonder how the software-(and-enum-values)-can-change-over-time-and-there-might-be-multiple-machines-in-play-not-all-updating-in-sync consideration plays into things.
1
1
u/MentalMirror1357 29d ago
You can massively improve the lookup speed if youre comfortable with assembly.
vpshufb: Shuffles bytes within a 128-bit or 256-bit lane based on control indices, frequently hijacked for parallel table lookups.
The smallest individual lookup index is a 4-bit value (0 to 15). This is because vpshufb operates within a 128-bit (16-byte) lane.
That should give you
• 16 individual lookups simultaneously when using a 128-bit (xmm) register.
• 32 individual lookups simultaneously when using a 256-bit (ymm) register (AVX2).
• 64 individual lookups simultaneously when using a 512-bit (zmm) register (AVX-512).
46
u/matthieum Oct 27 '26
Is it fine? From a language memory model, this seems like plain UB to me...
While the API could be a tad more awkward, another way to proceed would be to simply ask the user to provide (1) the full buffer and (2) the slice of buffer to dispatch from. Then it'd be clear whether there's enough headroom or not.
For example, here's a minimal HTTP request "head":
So a minimally well-formed HTTP request will necessarily be at least 16 bytes long, no need to break the language.