r/programming 1d ago

Branch‑Avoidant Programming

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

87 comments sorted by

View all comments

139

u/meamZ 23h ago

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 22h ago

and how can you tell predictability?

15

u/meamZ 21h ago

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...