Branch prediction forecasts a conditional branch so the processor can execute likely-next instructions speculatively and preserve instruction-level parallelism. A predictor might observe a condition succeeding 99 of 100 times; when it is wrong, the processor flushes its pipeline, imposing a 10–20-cycle penalty. section 12.3
A common misconception is that sequential Go statements necessarily execute as an indivisible sequence:
- The increment, condition check, and following increment can overlap when their dependencies permit it. section 12.3
- A conditional instruction creates a control hazard when a later instruction depends on its outcome; prediction permits speculative execution rather than waiting. section 12.3
- When diagnosing performance, distinguish a control hazard from a data hazard: changing dependencies can expose parallelism, but prediction addresses the branch decision itself. section 12.3
const n = 1_000_000
func add(s [2]int64) [2]int64 {
for i := 0; i < n; i++ { #1
s[0]++ #2
if s[0]%2 == 0 { #3
s[1]++
}
}
return s
}
Use this loop to inspect a conditional increment whose repeated outcome gives branch prediction an exploitable pattern. section 12.3

