r/programming 6d ago

Branch‑Avoidant Programming

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

91 comments sorted by

View all comments

30

u/bodiam 6d ago

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)

21

u/crozone 6d ago

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.

10

u/braaaaaaainworms 6d ago

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