Skip to content
Tech News
← Back to articles

Branchless Rust: Making a Filter 4x Faster by Removing an If

read original more articles

August 02, 2026 #rust #branchless #optimization Serhii PotapovAugust 02, 2026

Most of my career I spent in the domain world programming, where correctness matters much more than performance. Using Rust already made things fast enough. Avoid the N+1 SQL queries problem and usually we are good.

But recently I found myself in a situation where I actually had to optimize a hot path. This is how I discovered the branchless programming technique, and its results blew my mind. Let me share it with you on a small example.

The problem

Let's keep things simple. We need to filter a slice of numbers and return the elements that are greater than a given threshold (a typical problem that database engines solve all day long). Normally I would write the following code:

pub fn filter_iter (input : & [ f64 ], threshold : f64 ) -> Vec < f64 > { input . iter () . copied () . filter ( |& x | x > threshold) . collect () }

Easy to read, idiomatic, correct. Usually I would not touch it ever again. But what if this beast happens to be on a hot path? Let's benchmark it!

The input is one million random f64 values uniformly spread over 0.0..100.0 . Instead of one threshold we will try several, chosen so that the filter keeps 1%, 25%, 50%, 75% or 99% of the elements. For example, the threshold 50.0 keeps about a half.

The benchmarks are made with criterion and live in the branchless-rust-benchmarks repo, so you can reproduce everything on your own machine.

Puzzling results

... continue reading