Branch predictor: How many "if"s are too many? Including x86 and M1 benchmarks!
May 6, 2021Marek Majkowski, David Wragg

Summary
This post delves into the performance implications of branch prediction, specifically analyzing the cost of numerous conditional and unconditional branches within performance-critical code. It provides theoretical background on branch predictors (BPUs) and the Branch Target Buffer (BTB), and presents experimental results on AMD EPYC and Apple M1 processors. The findings highlight that the number of branches and their density significantly impact performance, with a noticeable degradation beyond a certain threshold (around 4096 branches) due to BTB limitations and potential cache misses. This contributes to the understanding of low-level hardware performance optimization.
More from the Hardware Infrastructure & Performance Optimization trail
- How we reduced core unit boot time from hours to minutes
- A one-line Kubernetes fix that saved 600 hours a year
- Launching Cloudflare’s Gen 13 servers- trading cache for cores for 2x edge compute performance
- Inside Gen 13- how we built our most powerful server yet
- So long, and thanks for all the fish- how to escape the Linux networking stack