How many branches can your CPU predict?

Modern CPUs use branch prediction to boost performance, but their capacity varies significantly. A recent test compares AMD Zen 5, Apple M4, and Intel Emerald Rapids to see how many branches they can 'learn' perfectly.
Modern processors have the ability to execute many instructions per cycle, on a single core. To be able to execute many instructions per cycle in practice, processors predict branches. I have made the point over the years that modern CPUs have an incredible ability to predict branches.
It makes benchmarking difficult because if you test on small datasets, you can get surprising results that might not work on real data.
My go-to benchmark is a function like so:
while (howmany != 0) {
val = generate_random_value()
if(val is odd) write to buffer
decrement howmany
}
The processor tries to predict the branch (if clause). Because we use random values, the processor should mispredict one time out of two.
However, if we repeat multiple times the benchmark, always using the same random values, the processor learns the branches. How many can processors learn? I test using three recent processors.
- The AMD Zen 5 processor can predict perfectly 30,000 branches.
- The Apple M4 processor can predict perfectly 10,000 branches.
- Intel Emerald Rapids can predict perfectly 5,000 branches.
Once more I am disappointed by Intel. AMD is doing wonderfully well on this benchmark.
The Rust guy at work said that Rust’s borrow checker makes the Rust assembly more predictable which makes it easier for the CPU to do branch prediction and that’s why Rust is so much faster than less-predictable languages like C and C++. Maybe you could get even higher numbers if you rewrote your benchmark using Rust.
No. My tests have nothing to do with the programming language.
This is not the case, rust is LLVM backed same as C++. There is no magic here with rust being able to hint to the branch predictor. What your co worker probably conflated is a feature where certain rust code can be optimized away since llvm knows certain facts about the code at certain points. This is most often used to describe how rust often eliminates its bounds checking since llvm can infer the size of things making some checks redundant. If you wanted to hint the branch predictor you could use a keyword like likely or unlikely.
Source: Hacker News










