Follow

Highly recommended read if you ever optimized a or loop, or ever used __builtin_expect().

blog.cloudflare.com/branch-pre

My key take away is that not-taken conditional branches are essentially free, and are not polluting the branch predictor, i.e.

if (__builtin_expect (DEBUG, 0))
log (...);

@timj I never write C/C++ but I have been fascinated by branchless programming for a while now.

@finlaydag33k
The findings can potentially be interesting as well for languages with JITs (e.g. Java, Javascript) that have good heuristics. E.g. one heuristic often used is "branch for early exits is unlikely", so the following should result in 2 not-taken conditionals (happy path) in JS:

if (input < 0 || input > 65536)
return;

@timj Indeed, though I mainly use PHP.
I do wonder whether it'll make a noticable different there.

Sign in to participate in the conversation
Mastodon

The social network of the future: No ads, no corporate surveillance, ethical design, and decentralization! Own your data with Mastodon!