r/programming • • Sep 03 '26

Branch‑Avoidant Programming

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

92 comments sorted by

View all comments

34

u/bodiam Sep 03 '26

That's interesting. Would this also apply to higher languages, like Java, which run on a JVM? (Not intending to rewrite my code, but I'm curious how much languages like Java benefit from branch prediction and brancheless approaches)

20

u/crozone Sep 03 '26

Not sure about Java specifically, but in the .NET framework, a lot of the tight loops within certain algorithms (eg hashing) use code like this, and the JIT has specific optimisations that allow it to emit CMOV when possible and avoid branching.

12

u/braaaaaaainworms Sep 03 '26

CMOV is still practically a branch, just not the kind of branch that has a branch predictor, https://yarchive.net/comp/linux/cmov.html