---
title: "Boosting Rust Hot Loops: Slashing Branch Mispredictions"
description: "Low-level optimization in Rust, focusing on minimizing branch mispredictions in performance-critical loops"
url: "https://mayorana.ch/en/blog/branch-prediction-rust"
language: "en"
date: "2025-10-23"
author: "Mohamed Bennekrouf"
tags:
  - "rust"
  - "optimization"
  - "advanced"
fr_version: "https://mayorana.ch/fr/blog/branch-prediction-rust.md"
---

# Boosting Rust Hot Loops: Slashing Branch Mispredictions

Branch mispredictions occur when the CPU’s branch predictor incorrectly guesses whether a conditional jump (e.g., from an `if`) is taken, causing pipeline stalls and costing cycles (10-20 cycles per misprediction on modern CPUs). In a performance-critical hot loop in Rust, I’d restructure the code to minimize or eliminate branches, leveraging Rust’s features, and use profiling tools to confirm measurable improvements in CPU pipeline efficiency.

<div class="svg-container" style="margin:2rem 0;">
<svg class="lo4-fig" viewBox="0 0 800 260" width="100%" style="height:auto;max-width:780px;display:block;margin:0 auto;" role="img" aria-label="Pipeline diagram contrasting a branched if statement that causes a misprediction flush with a branchless arithmetic version that keeps the pipeline full">
<style>
.lo4-fig{--bg:#f8fafc;--box:#ffffff;--tx:#1e293b;--mut:#64748b;--ln:#cbd5e1;--ac:#FF6B00}
:root.dark .lo4-fig,[data-theme="dark"] .lo4-fig{--bg:#0f172a;--box:#1e293b;--tx:#f8fafc;--mut:#94a3b8;--ln:#475569}
.lo4-fig .box{fill:var(--box);stroke:var(--ln);stroke-width:1.5}
.lo4-fig .warn{fill:var(--box);stroke:var(--ac);stroke-width:2}
.lo4-fig .fin{fill:var(--box);stroke:var(--ac);stroke-width:2}
.lo4-fig .tx{fill:var(--tx);font:600 12px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
.lo4-fig .mut{fill:var(--mut);font:500 11px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
.lo4-fig .ac{fill:var(--ac);font:700 12px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
.lo4-fig line{stroke:var(--ln);stroke-width:1.5}
</style>
<defs>
<marker id="lo4-arrow" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M0,0 L10,5 L0,10 z" fill="var(--ln)"/>
</marker>
</defs>
<!-- row 1: branched -->
<text x="20" y="22" class="tx" text-anchor="start">Branched: if x &gt; 0 { sum += x }</text>
<rect x="20" y="35" width="140" height="50" rx="6" class="box"/>
<text x="90" y="65" class="tx">Fetch / Decode</text>
<line x1="160" y1="60" x2="178" y2="60" marker-end="url(#lo4-arrow)"/>
<rect x="180" y="35" width="140" height="50" rx="6" class="box"/>
<text x="250" y="65" class="tx">Predict taken</text>
<line x1="320" y1="60" x2="338" y2="60" marker-end="url(#lo4-arrow)"/>
<rect x="340" y="35" width="140" height="50" rx="6" class="warn"/>
<text x="410" y="65" class="ac">Mispredict!</text>
<line x1="480" y1="60" x2="498" y2="60" marker-end="url(#lo4-arrow)"/>
<rect x="500" y="35" width="280" height="50" rx="6" class="warn"/>
<text x="640" y="65" class="ac">Flush + refetch: ~15 cycles lost</text>
<!-- row 2: branchless -->
<text x="20" y="122" class="tx" text-anchor="start">Branchless: sum += (x &gt; 0) as i32 * x</text>
<rect x="20" y="135" width="140" height="50" rx="6" class="box"/>
<text x="90" y="165" class="tx">Fetch / Decode</text>
<line x1="160" y1="160" x2="178" y2="160" marker-end="url(#lo4-arrow)"/>
<rect x="180" y="135" width="280" height="50" rx="6" class="box"/>
<text x="320" y="160" class="tx">Execute mask + multiply</text>
<text x="320" y="176" class="mut">no conditional jump</text>
<line x1="460" y1="160" x2="478" y2="160" marker-end="url(#lo4-arrow)"/>
<rect x="480" y="135" width="300" height="50" rx="6" class="fin"/>
<text x="630" y="165" class="ac">Retire: steady throughput</text>
<text x="400" y="225" class="mut">Same result, no pipeline flush — predictable for any input distribution</text>
</svg>
</div>

## Techniques to Reduce Branch Mispredictions

### 1. Branch Elimination with Arithmetic
Replace `if` statements with branchless operations to avoid conditional jumps.

**Before** (branched):
```rust
let mut sum = 0;
for x in data {
    if x > 0 { sum += x; } // Branch: taken or not?
}
```

**After** (branchless):
```rust
let mut sum = 0;
for x in data {
    sum += (x > 0) as i32 * x; // No branch: true=1, false=0
}
```

The comparison generates a mask (`1` for `true`, `0` for `false`), and multiplication avoids a jump. Rust’s type system ensures this is safe and explicit.

### 2. Data Sorting for Predictable Patterns
If branches depend on input data, sort it to group similar outcomes, making branch prediction easier.

**Before**:
```rust
for x in data {
    if x < threshold { process_a(x); } else { process_b(x); }
}
```

**After**:
```rust
data.sort_by(|a, b| a.partial_cmp(b).unwrap());
for x in data {
    if x < threshold { process_a(x); } else { process_b(x); }
}
```

Sorting with `sort_by` creates long runs of “taken” or “not taken” branches, improving predictor accuracy.

<div class="svg-container" style="margin:2rem 0;">
<svg class="lo4b-fig" viewBox="0 0 800 275" width="100%" style="height:auto;max-width:780px;display:block;margin:0 auto;" role="img" aria-label="Strip of sixteen branch outcomes showing that unsorted data alternates taken and not-taken and mispredicts often, while sorted data forms two long runs with a single misprediction at the boundary">
<style>
.lo4b-fig{--bg:#f8fafc;--box:#ffffff;--tx:#1e293b;--mut:#64748b;--ln:#cbd5e1;--ac:#FF6B00}
:root.dark .lo4b-fig,[data-theme="dark"] .lo4b-fig{--bg:#0f172a;--box:#1e293b;--tx:#f8fafc;--mut:#94a3b8;--ln:#475569}
.lo4b-fig .cell{fill:var(--box);stroke:var(--ln);stroke-width:1.5}
.lo4b-fig .cellt{fill:var(--bg);stroke:var(--mut);stroke-width:1.5}
.lo4b-fig .box{fill:var(--box);stroke:var(--ln);stroke-width:1.5}
.lo4b-fig .miss{fill:var(--ac)}
.lo4b-fig .ti{fill:var(--tx);font:700 13px ui-sans-serif,system-ui,sans-serif}
.lo4b-fig .tx{fill:var(--tx);font:600 12px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
.lo4b-fig .mut{fill:var(--mut);font:500 11px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
.lo4b-fig .mutl{fill:var(--mut);font:500 11px ui-sans-serif,system-ui,sans-serif}
.lo4b-fig .ac{fill:var(--ac);font:700 12px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
</style>
<!-- row 1 title -->
<text x="20" y="22" class="ti">Unsorted data — x &lt; threshold flips constantly</text>
<!-- row 1: 16 branch outcomes -->
<rect x="40" y="34" width="30" height="32" rx="4" class="cellt"/>
<text x="55" y="55" class="tx">T</text>
<rect x="74" y="34" width="30" height="32" rx="4" class="cell"/>
<text x="89" y="55" class="tx">N</text>
<circle cx="89" cy="80" r="4" class="miss"/>
<rect x="108" y="34" width="30" height="32" rx="4" class="cellt"/>
<text x="123" y="55" class="tx">T</text>
<circle cx="123" cy="80" r="4" class="miss"/>
<rect x="142" y="34" width="30" height="32" rx="4" class="cellt"/>
<text x="157" y="55" class="tx">T</text>
<rect x="176" y="34" width="30" height="32" rx="4" class="cell"/>
<text x="191" y="55" class="tx">N</text>
<circle cx="191" cy="80" r="4" class="miss"/>
<rect x="210" y="34" width="30" height="32" rx="4" class="cellt"/>
<text x="225" y="55" class="tx">T</text>
<circle cx="225" cy="80" r="4" class="miss"/>
<rect x="244" y="34" width="30" height="32" rx="4" class="cell"/>
<text x="259" y="55" class="tx">N</text>
<circle cx="259" cy="80" r="4" class="miss"/>
<rect x="278" y="34" width="30" height="32" rx="4" class="cell"/>
<text x="293" y="55" class="tx">N</text>
<rect x="312" y="34" width="30" height="32" rx="4" class="cellt"/>
<text x="327" y="55" class="tx">T</text>
<circle cx="327" cy="80" r="4" class="miss"/>
<rect x="346" y="34" width="30" height="32" rx="4" class="cellt"/>
<text x="361" y="55" class="tx">T</text>
<rect x="380" y="34" width="30" height="32" rx="4" class="cell"/>
<text x="395" y="55" class="tx">N</text>
<circle cx="395" cy="80" r="4" class="miss"/>
<rect x="414" y="34" width="30" height="32" rx="4" class="cellt"/>
<text x="429" y="55" class="tx">T</text>
<circle cx="429" cy="80" r="4" class="miss"/>
<rect x="448" y="34" width="30" height="32" rx="4" class="cell"/>
<text x="463" y="55" class="tx">N</text>
<circle cx="463" cy="80" r="4" class="miss"/>
<rect x="482" y="34" width="30" height="32" rx="4" class="cell"/>
<text x="497" y="55" class="tx">N</text>
<rect x="516" y="34" width="30" height="32" rx="4" class="cellt"/>
<text x="531" y="55" class="tx">T</text>
<circle cx="531" cy="80" r="4" class="miss"/>
<rect x="550" y="34" width="30" height="32" rx="4" class="cellt"/>
<text x="565" y="55" class="tx">T</text>
<text x="690" y="47" class="ac">10 of 16 mispredicted</text>
<text x="690" y="66" class="mut">~150 cycles thrown away</text>
<text x="690" y="84" class="mut">predictor learns nothing</text>
<!-- row 2 title -->
<text x="20" y="118" class="ti">After sort_by — two long runs</text>
<!-- row 2: 16 branch outcomes -->
<rect x="40" y="130" width="30" height="32" rx="4" class="cell"/>
<text x="55" y="151" class="tx">N</text>
<rect x="74" y="130" width="30" height="32" rx="4" class="cell"/>
<text x="89" y="151" class="tx">N</text>
<rect x="108" y="130" width="30" height="32" rx="4" class="cell"/>
<text x="123" y="151" class="tx">N</text>
<rect x="142" y="130" width="30" height="32" rx="4" class="cell"/>
<text x="157" y="151" class="tx">N</text>
<rect x="176" y="130" width="30" height="32" rx="4" class="cell"/>
<text x="191" y="151" class="tx">N</text>
<rect x="210" y="130" width="30" height="32" rx="4" class="cell"/>
<text x="225" y="151" class="tx">N</text>
<rect x="244" y="130" width="30" height="32" rx="4" class="cell"/>
<text x="259" y="151" class="tx">N</text>
<rect x="278" y="130" width="30" height="32" rx="4" class="cell"/>
<text x="293" y="151" class="tx">N</text>
<rect x="312" y="130" width="30" height="32" rx="4" class="cellt"/>
<text x="327" y="151" class="tx">T</text>
<circle cx="327" cy="176" r="4" class="miss"/>
<rect x="346" y="130" width="30" height="32" rx="4" class="cellt"/>
<text x="361" y="151" class="tx">T</text>
<rect x="380" y="130" width="30" height="32" rx="4" class="cellt"/>
<text x="395" y="151" class="tx">T</text>
<rect x="414" y="130" width="30" height="32" rx="4" class="cellt"/>
<text x="429" y="151" class="tx">T</text>
<rect x="448" y="130" width="30" height="32" rx="4" class="cellt"/>
<text x="463" y="151" class="tx">T</text>
<rect x="482" y="130" width="30" height="32" rx="4" class="cellt"/>
<text x="497" y="151" class="tx">T</text>
<rect x="516" y="130" width="30" height="32" rx="4" class="cellt"/>
<text x="531" y="151" class="tx">T</text>
<rect x="550" y="130" width="30" height="32" rx="4" class="cellt"/>
<text x="565" y="151" class="tx">T</text>
<text x="690" y="143" class="ac">1 of 16 mispredicted</text>
<text x="690" y="162" class="mut">only at the run boundary</text>
<text x="690" y="180" class="mut">predictor saturates</text>
<!-- footer -->
<rect x="120" y="200" width="560" height="60" rx="6" class="box"/>
<text x="400" y="224" class="tx">T = branch taken · N = not taken · orange dot = pipeline flush</text>
<text x="400" y="244" class="mut">Pay the sort once when the loop body runs many times per element</text>
</svg>
</div>

### 3. Conditional Moves with Pattern Matching
Use Rust’s enums and `match` to structure code for potential branchless optimization.

**Before**:
```rust
let result = if flag { compute_a() } else { compute_b() };
```

**After**:
```rust
enum Op { A, B }
let op = if flag { Op::A } else { Op::B };
let result = match op {
    Op::A => compute_a(),
    Op::B => compute_b(),
};
```

For simple `compute_a` and `compute_b`, the compiler may optimize this into a conditional move (`cmov` on x86), avoiding jumps.

### 4. Loop Unrolling
Unroll small loops to reduce the frequency of loop-end branches.

**Before**:
```rust
for i in 0..4 {
    if data[i] > 0 { out[i] = data[i]; }
}
```

**After**:
```rust
out[0] = (data[0] > 0) as i32 * data[0];
out[1] = (data[1] > 0) as i32 * data[1];
out[2] = (data[2] > 0) as i32 * data[2];
out[3] = (data[3] > 0) as i32 * data[3];
```

Fewer loop-end branches improve pipeline flow.

## Leveraging Rust’s Features

Rust’s ownership model and [zero-cost abstractions](https://mayorana.ch/en/blog/zero-cost-abstractions-rust) (e.g., iterator fusion) reduce implicit branches. Iterators like `filter` can be inlined and optimized (see previous answers on iterator chains), and the type system encourages clean, optimizable patterns without unsafe code.

## Profiling Tools and Verification

To measure and confirm reductions in branch mispredictions, I’d use:

- **Linux `perf`**:
  - **Command**: `perf stat -e branches,branch-misses ./target/release/myapp`
  - **Metrics**: Monitor `branch-misses` as a percentage of `branches`. A drop from 10% to 2% indicates success.
  - **Example Output (Before)**:
    ```
    10,000,000 branches
    1,000,000 branch-misses (10.00%)
    ```
  - **After Optimization**:
    ```
    8,000,000 branches
    160,000 branch-misses (2.00%)
    ```

- **Valgrind with Callgrind**:
  - **Command**: `valgrind --tool=callgrind ./target
