Learn the vocabulary of a CPU guessing a branch's outcome and speculatively executing down that guessed path.
0 / 5 completed
1 / 5
At standup, a dev mentions that a CPU guesses which way an upcoming conditional branch will go and starts speculatively executing instructions down that guessed path before the branch's actual condition is even fully evaluated. What is this CPU technique called?
Branch prediction is exactly this: a CPU guesses which way an upcoming conditional branch will go, based on patterns it has observed, and starts speculatively executing instructions down that guessed path before the branch's actual condition is fully evaluated, keeping its execution pipeline full instead of stalling. A hash collision is an unrelated hash-table concept about two keys sharing a bucket. This guess-and-speculatively-execute behavior is exactly why a modern CPU can run predictable, well-behaved branching code close to full speed instead of pausing at every conditional.
2 / 5
During a design review, the team notices a hot loop's branch condition follows a highly predictable, repeating pattern, which lets the CPU's branch predictor guess correctly almost every time. Which capability does this predictability provide?
A highly predictable branch condition here provides sustained high throughput through the loop, since the branch predictor guesses correctly almost every time, letting the CPU keep speculatively executing down the correct path and keeping its execution pipeline full instead of stalling to wait for the branch's actual outcome to be confirmed. A completely unpredictable, random branch condition would cause the predictor to guess wrong roughly half the time, forcing the CPU to discard the speculatively executed work and restart down the correct path, which costs real cycles. This predictability-driven throughput is exactly why branch prediction rewards code whose conditionals follow consistent, learnable patterns.
3 / 5
In a code review, a dev notices a hot loop's branch condition depends on effectively random, unsorted input data, making the branch outcome unpredictable from one iteration to the next, and no attention was given to how this affects the CPU's branch predictor. What does this represent?
This is a missed branch-prediction consideration, since an effectively random, unpredictable branch condition causes the CPU's branch predictor to guess wrong far more often, triggering a misprediction penalty on each wrong guess as speculatively executed work is discarded and the pipeline restarts down the correct path, when sorting the input data or restructuring the branch could make the outcome more predictable. A cache eviction policy is an unrelated concept about discarded cache entries. This ignore-predictability pattern is exactly the kind of overlooked performance detail a reviewer flags in a genuinely hot loop.
4 / 5
An incident report shows a hot loop's throughput was far lower than expected on a performance-critical path, because its branch condition depended on effectively random, unsorted input data, causing frequent branch mispredictions that repeatedly stalled the CPU's pipeline. What practice would prevent this?
Restructuring the data, such as sorting it so the branch condition follows a more consistent, learnable pattern, lets the CPU's branch predictor guess correctly far more often, directly reducing the pipeline-stalling mispredictions described in this incident. Continuing to rely on effectively random, unsorted input data for the branch condition regardless of the misprediction cost is exactly what caused the throughput to suffer. This data-restructuring approach is a well-known fix for hot-loop performance once profiling traces the slowdown back to a poorly predictable branch condition.
5 / 5
During a PR review, a teammate asks why sorting the input data before a hot loop noticeably speeds up that loop, even though sorting itself adds extra work up front. What is the reasoning?
Sorting the input data makes the loop's branch condition follow a consistent, learnable pattern instead of jumping unpredictably between outcomes, which lets the CPU's branch predictor guess correctly far more often and avoid the pipeline-stalling cost of a misprediction on nearly every iteration. Since a misprediction penalty can cost many CPU cycles, the cumulative savings from far fewer mispredictions across a large loop can easily outweigh the one-time cost of sorting the data beforehand. This is exactly why branch-heavy hot loops sometimes benefit surprisingly from restructuring their data purely to make branch outcomes more predictable.