Branch Prediction and Hot Loops
Help the CPU predict branches and write loops the compiler can optimize.
The Branch Predictor
Modern CPUs predict the next instruction before the previous one finishes. When the prediction is wrong, the pipeline stalls — costing 10-20 cycles.
Predictable Branches
Branches that almost always go one way (or follow a clear pattern) are well predicted. Random branches are catastrophic.
All lessons in this course
- Cache-Friendly Data Layouts
- Branch Prediction and Hot Loops
- Profiling with perf vtune and Sanitizers
- Micro-benchmarking with Google Benchmark