News for hackers. Stuff that matters.
Re:Push ifs up and fors down by flashback2199 on Wednesday November 15, 2023 @05:16PM (#38283372)
Don't compilers, cpu branch prediction, etc all fix the performance issues behind the scenes for the most part?
Re:Push ifs up and fors down by jchw on Wednesday November 15, 2023 @05:29PM (#38283524)
The short answer is absolutely not, even when you are sure that it should. Even something as simple as a naive byteswap function might wind up generating surprisingly suboptimal code depending on the compiler. If you really want to be sure, you're just going to have to check. (And if you want to check, a good tool is, of course, Compiler Explorer.)
Re:Push ifs up and fors down by bee_rider on Wednesday November 15, 2023 @05:27PM (#38283506)
Some of the moves seemed to change what an individual function might do. For example they suggested pulling an if from a function to the calling function.

Could the compiler figure it out? My gut says maybe; maybe if it started by inlining the callee? But inlining happens based on some heuristics usually, this seems like an unreliable strategy if it would even work at all.

Re:Push ifs up and fors down by roywashere on Wednesday November 15, 2023 @05:31PM (#38283548)
Function call overhead can be a real issue in languages like Python and JavaScript. But you can or should measure when in doubt!
Re:Push ifs up and fors down by hansvm on Wednesday November 15, 2023 @08:57PM (#38285046)
It's a real issue in most compiled languages too if you're not careful (also a sort of opposite issue; too few functions causing unnecessary bloat and also killing performance).
Re:Push ifs up and fors down by Thaxll on Wednesday November 15, 2023 @05:26PM (#38283497)
Well compilers are good and dumb at the same time.
Re:Push ifs up and fors down by malux85 on Wednesday November 15, 2023 @05:28PM (#38283518)
No, compilers (correctly) prefer correctness over speed, so they can optimise “obvious” things, but they cannot account for domain knowledge or inefficiencies farther apart, or that “might” alter some global state, so they can only make optimisations where they can be very sure there’s no side effects, because they have to err on the side of caution.

They will only give you micro optimisations which could cumulatively speed up sometimes but the burden of wholistic program efficiency is still very much on the programmer.

If you’re emptying the swimming pool using only a glass, the compiler will optimise the glass size, and your arm movements, but it won’t optimise “if you’re emptying the correct pool” or “if you should be using a pump instead” - a correct answer to the latter two could be 100,000 times more efficient than the earlier two, which a compiler could answer.

Re:Push ifs up and fors down by ezekiel68 on Wednesday November 15, 2023 @07:09PM (#38284220)
This is just a myth promoted by Big Compiler designed to sell you more compiler.
Re:Push ifs up and fors down by Izkata on Wednesday November 15, 2023 @11:29PM (#38285802)
There's a pretty famous StackOverflow question/answer about branch prediction failure: https://stackoverflow.com/questions/11227809/why-is-processi...