Branchless programming is a technique that eliminates unpredictable CPU branch mispredictions by replacing conditional control flow with arithmetic. Using a filter-over-floats benchmark in Rust, the author demonstrates why a standard iterator-based filter is slowest at 50% selectivity on shuffled data — the branch predictor is essentially coin-flipping, causing ~500K pipeline flushes. By rewriting the filter to always write each element and use the boolean comparison result as a numeric index increment, the branch is converted from a control dependency to a data dependency. This makes the worst case nearly 4x faster and produces consistent timing regardless of data distribution. The trade-off: the best case (highly skewed data) gets slightly slower due to unconditional writes. The technique is recommended only for measured hot paths with unpredictable branches.

8m read timeFrom greyblake.com
Post cover image
Table of contents
The problemPuzzling resultsFirst instinct: preallocateWhat CPUs do behind our backThe smoking gunWelcome branchless programmingShould you go branchless?ConclusionsLinks
975 Impressions