Skip to content

Branch Prediction

Spicy — senior dev territoryBackend

ELI5 - The Vibe Check

Modern CPUs don't wait around to find out if an if statement is true, they guess the answer and start executing down that path before they even know for sure, because waiting would waste dozens of idle cycles. Guess right and you got a free head start, guess wrong and the CPU has to throw away all that work and start over, which is why sorting an array before a loop full of conditionals can genuinely make it faster.

Real Talk

Branch prediction is a CPU technique that guesses the outcome of a conditional branch, like an if statement, before it is evaluated, allowing the processor to speculatively execute instructions down the predicted path instead of stalling the pipeline. A misprediction forces the CPU to discard the speculative work and flush the pipeline, which can cost 10 to 20 cycles on modern hardware, making predictable branch patterns meaningfully faster than data-dependent ones.

Show Me The Code

// Sorting data first can speed up a branch-heavy loop
// because the predictor learns the pattern
data.sort();
for (const x of data) {
  if (x > threshold) sum += x; // now highly predictable
}

When You'll Hear This

"Sorting the array first made the branch predictor's job trivial and doubled our speed." / "That famous benchmark where sorted data runs faster is all branch prediction."

Made with passive-aggressive love by manoga.digital. Powered by Claude.