Skip to content

Branch Prediction and Speculative Execution

Modern processors execute instructions speculatively before knowing branch outcomes; mispredictions waste work and stall pipelines. Predictable code patterns run faster than unpredictable ones, even when performing the same logical operations.

Tension Addressed

Predictability vs. Flexibility

Recognizable As

Sorting data before processing improves performance, branchless algorithms, lookup tables replacing conditionals, performance cliffs from data-dependent branches.

Canonical Sources

Patterson & Hennessy, Computer Organization and Design; Fog, The microarchitecture of Intel, AMD and VIA CPUs

Transfers To

Compiler optimization, database query execution, virtual machine design

The famous "sorted array" benchmark: processing a sorted array can be dramatically faster than an unsorted one because branch outcomes become predictable. The same logical operations, different performance.