r/programming 1d ago

Branch‑Avoidant Programming

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

87 comments sorted by

View all comments

0

u/hacksoncode 21h 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.

3

u/bwainfweeze 19h ago

To be fair, we are in the somewhat historically unique situation of AWS providing one exact architecture of server to run your same cluster for five, eight years at a time with zero substitutions. Anywhere else in software history, except supercomputers, it would be insane to optimize for a single processor.

Honestly am surprised they don’t end up retiring the old instance types sooner.