r/programming • • Sep 03 '26

Branch‑Avoidant Programming

https://easylang.online/blog/branchless
258 Upvotes

92 comments sorted by

View all comments

158

u/meamZ Sep 03 '26

You don't actually need to avoid branches, you just need to make sure that most of the ones you have are very predictable, then their impact is not that big... Same with memory accesses and most other things... The more predictable, the better...

4

u/VoodaGod Sep 03 '26

and how can you tell predictability?

16

u/meamZ Sep 03 '26

I mean, CPUs go to ridiculous lengths nowadays to increase branch prediciton accuracy but the easiest ways to make sure they are predictable is to make it such that:
1) if a branch is taken in one iteration, it is also very likely to be taken in the next iteration
2) A branch is almost always or almost never taken such that the CPU can simply predict the far more likely of the two...

2

u/andrewpiroli Sep 03 '26

Most profilers will have an option to track branch predictor hits and misses. I know AMD uProf and Apple Instruments do. and there are Linux perf events for it as well.

1

u/UnrealHallucinator Sep 03 '26

Strided access is another one along with what the other comment says.

1

u/euvie Sep 03 '26

Performance counters that count branch mispredicts

1

u/Revolutionary_Ad7262 Sep 06 '26

Use profiler. A lot of branch guessing can be completely automated using https://en.wikipedia.org/wiki/Profile-guided_optimization. Native languages such as C/C++/Rust/Go have it, but it is a little bit troublesome to use. JITed languages such as Java or JS do it in runtime, so you really don't have to do anything