← Retour au blog
tech 6 August 2026

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

Discover how branchless optimization in Rust can transform a filtering operation into a version four times faster by simply removing a condition.

Article inspired by the original source
Branchless Rust: Making a Filter 4x Faster by Removing an If ↗ www.greyblake.com

Introduction

Software optimization is often a quest for marginal gains. But what if you could make your code four times faster with a simple change? This is exactly what branchless optimization in Rust offers, a technique worth your attention if you aim to maximize the performance of your applications.

The Initial Problem

We have a classic programming problem: filtering a list of numbers. In Rust, this is typically done with a straightforward function using filter(). For example:

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

This approach is clear and idiomatic. However, in a critical path, every millisecond counts. Benchmarks on a million random values showed surprising results. The case where 50% of elements are kept is the slowest, even though less data is copied than when keeping 99% of elements. Why?

Understanding Modern CPUs

To understand these results, it's essential to consider how modern CPUs work. They use deep pipelines that prepare several instructions to execute. Conditional branches, like if, can break this flow by introducing "jumps" that the CPU must handle, creating "branch mispredictions" that are costly in terms of performance.

Branchless Optimization

The idea is to eliminate these branches. How? By using arithmetic and logical operations that can be executed without condition. For example, converting a condition into a mathematical operation:

``rust pub fn filter_branchless(input: &[f64], threshold: f64) -> Vec<f64> { let mut out = Vec::with_capacity(input.len()); for &x in input { let mask = (x > threshold) as u64; let value = x * mask as f64; out.push(value); } out.retain(|&x| x != 0.0); out } ``

The Results

By removing the conditional branch, the same benchmark reveals performance up to four times faster in some cases. This optimization leverages the CPU's ability to execute instructions sequentially without interruption.

When to Use This Technique?

This approach is not always necessary. It is mostly useful in "hot paths," where even small improvements can have a significant impact. For developers working on high-performance systems or real-time applications, this can be game-changing.

Conclusion

Branchless optimization in Rust demonstrates how a deep understanding of hardware architecture can lead to significant performance gains. By exploring these techniques, you can make your code not only faster but also more elegant.

Let's discuss your project in 15 minutes.

Rust branchless optimization performance CPU architecture
Deepthix newsletter · 100% AI · every Monday 8am

An AI agent reads tech for you.

Our AI agent scans ~200 sources per week and ships the best articles to your inbox Monday 8am. Free. One click to unsubscribe.

Visit the newsletter page →

Want to automate your operations?

Let's talk about your project in 15 minutes.

Book a call