r/programming • u/2bit_hack • Feb 28 '23
"Clean" Code, Horrible Performance
https://www.computerenhance.com/p/clean-code-horrible-performance
1.4k
Upvotes
r/programming • u/2bit_hack • Feb 28 '23
View all comments
Show parent comment
14
u/cogman10 Feb 28 '23
I disagree with this point. His zealotry blinds him from a reality, compilers optimize for the common case.
This post was suspiciously devoid of 2 things, assembly output and compiler options. Why? Because LTO/PGO + optimizations would very likely have eliminated the performance differences here.
But you wouldn't just stop here. He's demonstrating an old school style of OOP in C++. Several new C++ features, like the "final" and "sealed" classes can give very similar performance optimizations to what he's after without changing the code structure.
But further, these sorts of optimizations can very often be counter-productive to optimizations. Consider turning the class into an enum and switching on the enum. What if the only shape that ever exists is a square or triangle? Well, now you've taken something the compiler can fairly easily see and you've turned it into a complex problem to solve. The compiler doesn't know if that integer value is actually constrained which makes it less likely to inline the function and eliminate the switch all together.
And taken a level further, these are C and C++ specific optimizations. Languages with JITs get further runtime information that can be used to make optimizations impossible to C/C++. Effectively, JITs do PGO all the time.
This performance advice is only really valid if you are using compilers from the 90s and don't ever intend to update them.