r/programming 1d ago

Branch‑Avoidant Programming

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

87 comments sorted by

View all comments

33

u/bodiam 1d 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)

1

u/dacjames 16h ago edited 16h ago

In practice, it applies to any language that generates machine code, AOT or JIT.

In an interpreted language like Python, this optimization is much less effective. Bounds checking adds at least one hidden branch to every write but there's usually a lot more than that.

In my quick test of this example in Python, the branchless version is slightly slower, likely due to the extra writes.