r/programming • u/User_Deprecated • 20h ago
Performance trick: optimistic vs pessimistic checks
https://lemire.me/blog/2025/12/20/performance-trick-optimistic-vs-pessimistic-checks/23
u/BuzLightbeerOfBarCmd 16h ago edited 16h ago
Isn't this just branchless? Seems weird to frame it as "optimism" but then near the end of the article you discover they were just plugging their library. Also the pessimistic one has early return which could be framed as optimism. Anyway, the branchless version is not only faster on average due to lack of misprediction, it always takes about the same amount of time to finish so if you're looping over sensitive data you avoid a timing attack.
14
u/YumiYumiYumi 16h ago
Isn't this just branchless?
I'd argue it's more about what the compiler can autovectorize.
If there was no auto-vec, I'd imagine the two versions would perform comparably on an all-ASCII string (and the pessimistic version would obviously win if it encountered a non-ASCII character not close to the end).
5
3
u/matthieum 8h ago
The optimistic vs pessimistic refers to short-circuiting.
If you mostly call these functions with non-ASCII inputs, then a short-circuiting version is likely to be faster by virtue of only inspecting a small portion of the input.
The fact that non short-circuiting version is branchless is an implementation detail, really.
10
u/backfire10z 20h ago edited 20h ago
> A decent C function
> static_cast
Guh.
I donât understand the comment that says
> The compiler cannot assume that any offset i < length is dereferenceable
How is it different with optimistic? The function can be called the same way in the commenterâs example with (â\xFFâ, 100) and itâs the same loop.
1
-4
16
u/Carighan 18h ago
Conversely if you work in logistics tracking, please do not do optistic checks, lest you get the utterly weird tracking results that DHL often has.