Menu

Branchless Rust: Making a Filter 4x Faster by Removing an if | Serhii Potapov (greyblake)
📰

Branchless Rust: Making a Filter 4x Faster by Removing an if | Serhii Potapov (greyblake)

Hacker News·3 months ago
#2suXmxqd
Reading 0:00
15s threshold

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

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

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

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.