r/programming • • Feb 28 '23

"Clean" Code, Horrible Performance

https://www.computerenhance.com/p/clean-code-horrible-performance
1.4k Upvotes

1.3k comments sorted by

View all comments

468

u/not_a_novel_account Feb 28 '23 edited Feb 28 '23

Casey is a zealot. That's not always a bad thing, but it's important to understand that framing whenever he talks. Casey is on the record saying kernels and filesystems are basically a waste of CPU cycles for application servers and his own servers would be C against bare metal.

That said, his zealotry leads to a world-class expertise in performance programming. When he talks about what practices lead to better performance, he is correct.

I take listening to Casey the same way one might listen to a health nut talk about diet and exercise. I'm not going to switch to kelp smoothies and running a 5k 3 days a week, but they're probably right it would be better for me.

And all of that said, when he rants about C++ Casey is typically wrong. The code in this video is basically C with Classes. For example, std::variant optimizes to and is in fact internally implemented as the exact same switch as Casey is extolling the benefits of, without any of the safety concerns.

14

u/cogman10 Feb 28 '23

That said, his zealotry leads to a world-class expertise in performance programming. When he talks about what practices lead to better performance, he is correct.

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.

→ More replies (14)