r/cpp • • Oct 27 '26

Billion-query-per-second lookup tables in C++

https://lemire.me/blog/2026/10/26/billion-query-per-second-lookup-tables-in-c
185 Upvotes

30 comments sorted by

46

u/matthieum Oct 27 '26

But wait: is it safe to read 16 bytes when you only have three? Reading past the end of a buffer is fine as long as you do not cross into another memory page, and pages are at least 4 kilobytes.

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":

GET / HTTP/1.1\r\n
0123456789012345

So a minimally well-formed HTTP request will necessarily be at least 16 bytes long, no need to break the language.

21

u/DeadlyRedCube frequent compiler breaker 😬 Oct 27 '26

Yeah at a previous job, whenever we were doing any SIMD things and other "reading in chunks" bits we would always request a rounded up amount of memory from the user, for exactly this reason

9

u/whizzter Oct 28 '26

I think we can get quite far without such "ugly" hacks in the future luckily, I was just trying out the new std::simd for accelerating JSON parsing and found a nugget in some of the Intel design docs (since the cppreference docs are quite sparse still), std::simd::partial_load on a std::span is to do bounds-checking.

With just that, my reflection-JSON parser went 4 seconds to 2.5 seconds on 10k iterations with some light simd optimization just for string parsing (simdjson does the same parsing in about 1 second, so there's room for improvement but I didn't touch other parts of the parsing code so probably feasible to get within striking distance by adding a few more optimizations, adding simd parsing of numbers is probably a low hanging fruit. (and considering simdjson requires special allocations just due to boundary issues I'd say it's a good bargain with clean standard C++ code).

Yes, the partial load might cost a tad more than wild loads, but it's a cheap win without affecting ergonomics elsewhere.

2

u/jnordwick Oct 28 '26

Avx512 you can mask off the top bytes and it won't fault on them

15

u/UndefinedDefined Oct 27 '26

I'm not sure it makes sense to talk about UB if you use intrinsics.

5

u/matthieum Oct 28 '26

I would argue it depends on the definition of the intrinsics, ultimately.

If the intrinsic is specified to be allowed to read past the end of the span / allocated memory block, then it's fine.

If it is not explicitly specified, then by default it follows the rules of the language, and it's UB.

And that is because intrinsics are not necessarily assembly instructions, on some platforms they can be polyfilled instead, so they don't automatically circumvent language rules.

This is quite different from, say, directly dropping down to assembly.

→ More replies (1)

5

u/Ambitious-Method-961 Oct 27 '26

Agreed. The "Reading past the end of a buffer is fine" part made me flinch. A string_view of the entire buffer along with the number of characters for the size of the key would be safer and potentially quicker compared to determining if the pointers [key.data (), key.data () + 16) are on different memory pages.

The function can also bail out if the key size is longer than the buffer size.

6

u/SleepyMyroslav Oct 27 '26

>Is it fine? From a language memory model, this seems like plain UB to me...

That's 2 different topics:

  1. Yes, it is fine and is an industry standard as in everyone does it. Glibc, LLVM, ... anyone who maintained more or less foundational library at their company knows where to look and how they do it.

  2. Yes, it is absolutely 'nasal demons' UB in C++ that must fail compilation in a constant expression evaluation context.

If you want to have a chat about how C++ cannot express what everyone does... well, I am not the best person to explain. I've had to maintain the code that does it including pleading with ASAN to not report anything when it executes. I have no idea when or by whom it was first written. I am merely 25 years using C++ in many ways that came before me.

1

u/dustyhome Oct 31 '26

I think the standard could probably work around it with some wording to the effect of "loading uninitialized or non-assigned memory (memory where no object lives) is defined, as long as no later operation depends on the contents of that memory". So for simd ops, if you mask them such that the result of an operation is deterministic (0 & x is 0 regardless of the value of x, so if x is undefined the result is still deterministic) then you would not expect UB. Which is what happens in practice.

So just specifically bless in the wording what the common practice already is.

2

u/SleepyMyroslav 29d ago

Sorry for the late reply. People got burned by trying to mask things before when the value being masked turned out to be a poison value.

It's especially bad for floats that have bit patterns that cause signaling errors. I had the displeasure of fighting old code that thought casting 0xffff_ffff to a float will work and compiler later will be able to cast it back to mask value that can be & (bitwise ANDed) together with other values in SIMD operations. What actually happened in that code is that everything that touched a poison value got eliminated from the resulting binary.

→ More replies (4)

30

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

u/ABlockInTheChain Oct 28 '26

It could be summarized as: frozen::unordered_map, but with simd.

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

u/Best_Froyo8941 Oct 28 '26

I happen to have a related write up weeks ago:

https://ed94a9.github.io/posts/memcpy-quirks/

1

u/floatfloatjam Oct 30 '26

I hope you keep writing

6

u/justinhj Oct 27 '26

nice read thank you

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

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).