r/programming 1d ago

Branch‑Avoidant Programming

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

87 comments sorted by

View all comments

-1

u/hacksoncode 22h ago

Trying to figure out what will be optimal for a particular processor family is a job for the compiler/optimizer.

Super Geniuses that try to optimize this stuff themselves are just annoying to the people that have to maintain the code.

That's not to say there aren't some very specific circumstances where hand-optimization like this can make a big improvement on a problem that's actually limited by loop efficiency in a way that optimizers can't figure out, but normally it's a bad thing to think about.

8

u/nukethebees 17h ago

Trying to figure out what will be optimal for a particular processor family is a job for the compiler/optimizer.

Which are not perfect. We can help them by writing code that is easy to optimise.

Super Geniuses that try to optimize this stuff themselves are just annoying to the people that have to maintain the code.

The example is changing an if to a conditional expression. It's not exactly complicated.

In general, if you take the "premature optimisation is evil" route, your architecture may be hamstrung from the very beginning. There's not a whole lot you can do if your code is a pointer soup of virtual functions and pointer-based data structures.