Branch Prediction
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."
Related Terms
Cache Line
Your CPU never fetches just the one byte you asked for, it grabs a whole chunk around it, usually 64 bytes, called a cache line, on the theory that you'll...
Latency
Latency is the delay before data starts moving — the time it takes for a request to go from your device to the server and back.
Perf Counters
Perf counters are the CPU's own built-in scorekeepers, tiny hardware registers that silently count things like cache misses, branch mispredictions, and ins...
SIMD (Single Instruction, Multiple Data)
SIMD is a CPU trick where instead of adding two numbers at a time, the processor adds eight, or sixteen, or thirty-two pairs of numbers in the exact same i...