r/cpp • u/Benjamin1304 • Mar 15 '23
"Clean" Code, Horrible Performance and the forgotten variant
I just stumbled upon the "Clean" Code, Horrible Performance blog post and accompanying video (https://www.youtube.com/watch?v=tD5NrevFtbU).
The one thing that really struck me was how he went directly from OOP to good Ol' C-isms, without even considering the other alternatives we have in C++.
I really wanted to know how a std::variant based approach would compare against the OOP code and his enum based version, so I wrote a small benchmark that you can play with on quick-bench.
The result is simple, the std::variant+std::visit approach is on par, and even slightly faster, than his optimized version with both GCC and Clang. But by using the variant we can keep the code very close to the textbook example and still get the performance gain.
The video blown up a bit so I think it's important to remind people that we also have clean and performant solutions to such problems in modern C++. And it's also important to stress that this example doesn't mean that OOP makes your code 1.5x slower, just that it might not be the best tool for this job performance-wise as the virtual dispatch cost is too significant compared to what the functions actually do.
I didn't want to compare with the other optimizations he did because they are really specific to the use case. Just introduce a new shape with a more complex area function and everything falls apart.
75
u/Gnammix Mar 15 '23
To be fair, it was never a fair comparison, the inheritance version arguably does "more" than both the union and the variant one; it just didn't made any sense in this specific case.
30
u/Benjamin1304 Mar 15 '23
I agree but this point is completely omitted in the video, it's just "this is what clean code looks like and look how bad the performance is". He could have recognized that the example is a textbook example and so must fit in a textbook and be understandable by the average reader.
2
u/digama0 Mar 27 '23
As confirmation of /u/SirClueless's point that Casey is aware of
std::variantand is dismissive of it, we have the following line from the follow-up discussion between Uncle Bob and Casey:My point here relates back to my original point, which is that discriminated unions (C++ also has a janky std library version of these called "variants") seem to always be the better rule of thumb.
My reading is that he knows that std::variant is effectively equivalent in runtime performance to the switch statement but finds the syntax so horrendous as to not be worth consideration.
6
u/SirClueless Mar 15 '23
I don't think he omitted this as a slight to C++ or something. He omitted it because std::variant is not particularly popular or widely-understood so it doesn't have a lot of pedagogical value, and its behavior should be expected to be equivalent to a switch statement unless the compiler is failing at its job so it doesn't add a meaningful data point to the discussion of performance.
It therefore seems unremarkable to me to choose to omit this example in the interest of brevity. Even as a proponent of modern C++ it seems like the right decision.
24
u/guepier Bioinformatican Mar 15 '23
don't think he omitted this as a slight to C++ or something.
If you watch his other videos he definitely has major issues (some legitimate, some not so much) with C++. No idea whether you’d call this a “slight” but yes, he very much does have an axe to grind.
And his not using (something like)
std::variantin particular is because he objects to this kind of abstraction offered by modern C++ in principle, not because it “is not particularly popular or widely-understood”.9
u/SirClueless Mar 15 '23
I've watched several. He has an axe to grind with OOP as it is taught in schools (i.e. with C++ code straight out of the nineties). People generally don't teach std::variant in school so what's the point of espousing a particular view about whether std::variant is a good or bad solution to this problem? If you have a problem with that, there are other people to take it up with besides YouTubers.
Let's not pretend std::variant has no bones to pick at either. It does atrocious things to compile times because of the way compilers have implemented it, it's missing a number of monadic operations that most other languages have, std::visit has a tremendously awkward signature that demands you write a helper struct to compose an overload set or use a template function and
if constexprinstead of taking a list of invocables, std::get is clunky as all get-out, and above all the language has no pattern matching so it feels like a second-class citizen. Do you really think MollyRocket would have charitable things to say about it beyond that the performance is equivalent to a switch statement at runtime?19
u/guepier Bioinformatican Mar 15 '23
I’m not disagreeing with your points about
std::variantbut I don’t think you’re right that Casey only objects to OOP-heavy 90s style C++. He seems to equally dislike modern C++, or anything using opaque abstractions, really. His “abstraction phobia” is what I have a problem with (amongst other things, but they don’t matter here).(And just to get this out of the way: I don’t see him as black and white; he’s certainly very smart and knowledgeable, and I usually learn something from his videos.)
5
u/SirClueless Mar 15 '23
Got some specific examples? His criticisms have generally seemed pretty pragmatic to me rather than ideological. i.e. it's not that there's a phobia for clean abstractions, it's that if you can't make an abstraction clean (for example, because you've committed to supporting the raw alternative forever, or because you find when you go to implement the abstraction that your compiler is not sufficiently magic to understand your abstraction) then maybe you shouldn't make the abstraction.
6
u/OlivierTwist Mar 22 '23
His criticisms have generally seemed pretty pragmatic
But his example is terrible from pragmatic point of view: it is not maintainable at all. It works only when all data types and use cases are known well in advance.
2
u/TheTomato2 Mar 30 '23
On top of that its meant to be educational material for new coders or coders that aren't aware of the OOP pitfalls (which happens to be a lot). Using std::variant isn't trivial for people inexperienced in C++-isms.
3
Mar 15 '23
The purpose is to make people aware of the performance trade off. Not really to say one way is defacto better or worse.
Clean Code tends to be worse for performance. If you had no idea that was true, then this video is useful. Some people learn things for the first time
3
u/Etheric2355 Mar 16 '23
While this was true 30 years ago, as decades pass I find this is has not been true for a while.
Those days, the performance is usually on par.
In some cases, sure, you can get worse performance. And in some rare cases you actually get better performance, too (when the added intent information is picked up by the compiler, which leverages it for better code generation).
Now there is also what one considers to be clean code. Using the wrong paradigm for the problem, such as forcing OOP on a program than never needed it is not clean code, it's either absurd dogmatism or a hammer-nail issue.
2
u/kogyblack Mar 15 '23
Does more what? I really want to see your points here since the initial polymorphic code is many lines longer, more complex thus harder to read and way slower.
From my experience: it's not more adaptable, as people love to sell this idea because would you have to write way more just to add a simple new case. It's not easier to work with because working with polymorphic type is a constant "no idea where this call goes, I have to run and follow the dynamic dispatch". It simply "does more", is more complex, executes more code to do the same. Only point I see is that it can save memory compared to the tagged union approach.
24
u/Som1Lse Mar 15 '23
The different shapes can be in entirely different source files and can even be loaded dynamically. In this sense it does more. It supports more use cases.
Is that a good idea? Probably not. In the famous words of Mike Acton: Solving problems you probably don't have creates more problems you definitely do.
-4
u/kogyblack Mar 15 '23
You can have different files for different types with static dispatch also. Loading dynamically is a very specific case. I would argue that if you need to load the different types dynamically, do you really think it's better to have them as part of the same base type? Haha People can design code behemoths all they want, I think it would be hard to call it 'clean code' :D
Mike Acton is a reference, glad that he's less aggressive as a speaker than Casey, so it's easier to spread his good points around
3
u/Etheric2355 Mar 16 '23
Yeah, no-one ever needed dynamic loading, and plugins are a very specific case. That's why DLL no longer exist those days, and proprietary SDKs have died out.
24
u/Gnammix Mar 15 '23
Proper type erasure? You can just program against the interface without knowledge of the specific types? With both union and variant you need to know them all at compilation time.
The moment you try to do something similar you will have to add some form of indirection, i.e.: implement a vtable, and incur in similar performance loss/miss optimization opportunities.
Obviously if you have the source of everything and you compile everything at the same time you can play it as you like; but then again, as I said, in that case inheritance make much less sense.
-3
u/kogyblack Mar 15 '23
Okay, that would be one case. But you understand that you only need that in the interface of public libraries, right?
Even if you're building a public library, you can use type erasure just for the types that you will be in you API and have better code for everything else. The problem is that OOP is said to be the only solution, you need type erasure for everything, you need polymorphism for everything. At my work I'm basically getting paid to clean up the mess, and all comes down to these general "clean code" practices being applied and then after 5 years the code is so hard to work with, so slow, full of interfaces that are implemented by a single class because "what if we need to add another variant here", that they need to pay some people just to fix the code...
11
u/Gnammix Mar 15 '23
I do agree with you that, especially in some places, that kind of design is oversold but I don't think that clean code means necessarily polymorphism, or that OOP is only polymorphism via inheritance. The are good concept in clean code and OOP, people should just use what they need and not shoehorn everything everywhere.
Just to overstate the obvious, I'm not endorsing that kind usage, but given the video premises, that argument is just a strawman and I don't think the alternative proposed in the video is any better.
I think OP solution using variant shows that you can write expressive and performant code in modern C++, and if profiling show that even that is slow for your use case you can start "counting cycles"; at the end the nice thing of the language is that it gives you choices.
4
u/kogyblack Mar 15 '23
I'm not arguing against OP's code. std::variant should be a good solution too, although the code ergonomics using it are verbose and unnecessary in my view.
And I'm not arguing that there are no use cases for OOP, just that people are taught as the one-fits-all solution and end up writing code that is harder to read, harder to maintain and modify, and still very slow, because of these generalizations.
Just to show you my point, my last comment is negative right now haha my work is literally measure and fix bad code in big tech, code that goes back 20 years and it's still being incremented and improved (moving to C++20 now), but most devs think that their uni teachers and the generalization books are on the right track, but Casey, that has 40y experience on performance code, or someone that works removing technical debt of long projects are talking bs :D
We have to be critical. OOP can be used, but it's just another tool. It's too general to be good for most medium/big project, and there are way cleaner and more direct ways of writing code for 99% of the cases. People just overuse it and, even when they keep losing many hours weekly fighting against it, they defend it like there's no better tool.
-1
u/xtrqw Mar 16 '23
There's a lot of zealotry related to OOP and clean code practices, so it's hard to reason with other people about it, because a lot of them didn't arrive rationally at these ideas, it's what they've been told and taken to heart or they haven't heard/thought of alternatives.
Casey's code is a lot better and easier to read to me and I'd say it's the same for a lot of the diehards, but they don't want to admit it or maybe they're confusing familiarity with clarity.
7
u/Fearless_Process Mar 15 '23
Dynamic dispatch is not only used in public interfaces, it's useful anytime that concrete types can't be determined at compile time which happens in many different situations.
18
u/MarcoGreek Mar 15 '23
I understand the point of introducing a too flexible architecture but things like std::variant make it possible in a much more performant way. I really like too use it. I would prefer a language level support to make things easier but ...
I find it hard to measure performance if it is not a magnitudes slower than expected because modern CPUs with their caches and branch predictions are quite complex. So I very skeptical at micro benchmarks.
Some of my colleagues still strongly dislike everything new which is understandable but a little bit tiresome too. I really hope C++ community understands that we have to rethink compability not as a dogma but as an economic requirement like progres too. So their is maybe a better way to make (almost) everybody happy. 😉
9
u/Benjamin1304 Mar 15 '23
Yes microbenchmarking is hard and we must be aware of its limitations but in this case it shows what's expected : virtual dispatch, together with the impossibility of inlining, makes it perform worse since the functions to run are just a handful of instructions.
In my benchmark the shapes are in random order so branch prediction probably doesn't help here, but in the article the benchmarking code is not shown so we can't even know if there are any biases in it...
11
u/MarcoGreek Mar 15 '23
Yes but very often the code inside of a virtual function is much larger. If you use LTO the compiler could even devirtualize it.
I know that virtual functions can be slow. I use them heavily for writing mocks. In that case the compiler could easily devirtualize it but still it does not matter because the functions are so expensive that a virtual call is negletable. Not that I am not wishing that C++ would support testing as much more important than now.
4
u/emdeka87 Mar 15 '23
Yep... If C++ had language support for variant we maybe wouldn't even have this discussion/blog post now.
8
u/OlivierTwist Mar 15 '23
How union and "variant + visit" approaches can support new shapes without modification of total_area function?
It seems that TS and the blog author missed the whole point of virtual functions and run-time polymorphism.
8
u/Benjamin1304 Mar 15 '23
I didn't say it can, and that's where runtime polymorphism shines.
My point was just to show that in this case you can get the same performance without completely rewriting the code, just remove the base class, take a variant and call visit.
But if you really want to support new shapes without modifications it's still doable with libraries like dyno
2
u/OlivierTwist Mar 16 '23
To make this point clearer the comparison should be just between "switch vs std::variant", because in context of original blog post comparing three variants makes impression that static polymorphism can replace dynamic one.
P.S. Thank you for the code and effort.
1
14
u/muchcharles Mar 15 '23 edited Mar 15 '23
Before around 15 months ago, variants had bad performance in the stable release of GCC:
https://www.reddit.com/r/cpp/comments/kst2pu/comment/giiofwu/
Has anyone verified debug build performance of them, which is important in interactive applications Casey works on?
Checked each major compiler?
Less likely to regress in performance than Casey's way due to a future standards change or implementation change to variants, on release and debug builds?
4
u/Benjamin1304 Mar 15 '23
I was surprised when I first ran the benchmark using std::visit and seeing it perform this well. Some years ago it was very bad to the point that I used a tagged union instead in one of my libraries. But now, at least in release builds, it's very good.
For the debug build perf I haven't checked but you can test it with the benchmark I provided in the OP
37
u/eyes-are-fading-blue Mar 15 '23 edited Mar 15 '23
These performance gurus taint every single code base they touch by hand-unrolling the loops in the unit tests.
Addendum:
The author thinks he has a case against 3 (small functions) and 4 (single responsibility) but he doesn't. His tests to show the performance impact of these principles just repeats to demonstrate performance impact of virtual functions.
6
u/Benjamin1304 Mar 15 '23
To me the first "optimization" is just what allow to perform the others since you it starts to couple everything together. These other optimizations pile on the first one but are very domain specific and are possible only because of the exact starting code. This LUT thing wouldn't have been possible if a shape had a bit more complex area function.
2
u/kogyblack Mar 15 '23
You're basically saying that it's better to not think about your code and just generalize. You should code for what you need, not for all possible cases you won't ever need.
Casey took an example from the book and even complicated a bit later. The book is the wrong there by showing a case that can be easily written better, both in size, maintainability and performance. If there's a good example that these are good, which of course there are (just way more uncommon than it seems), Casey wouldn't be able to take 10 min to improve it.
People should focus on keeping their code simple and writing for the cases they need. Simple code is easier to change in case something needs to be added later on. And it usually takes years to add new cases (if you keep adding new cases frequently, it's a big indicator that you didn't even take time to design what you're implementing), and, if your code is complex, usually there's no room to improve on anything unless you refactor the whole architecture to work better for the all cases.
5
u/Benjamin1304 Mar 16 '23
No I haven't said that and it's clearly not something I would ever say.
The only thing I said was that his first optimization is something you can apply to any OOP code but the other ones are highly dependent on the domain (specific shapes with similarly looking area functions) and most of the time they are only applicable if you start putting all the code in one place.
I work in robotics where performant code is really important, but maintainable code is as much important, and you have to understand when it's ok to sacrifice one for the other.
And as I said in another comment, the code here is a textbook example, we shouldn't draw too much conclusions from it. If it was the exact same hierarchy but the virtual functions would do any amount of significant work then transforming the code as he did wouldn't bring any significant performance benefit.
The whole point of this post is to make people think and test things themselves and not blindly follow someone's mantra.
14
u/tjientavara HikoWorks developer Mar 15 '23
I actually once optimized unit-tests, made it about 100x faster than the original trivial-looking code. Of course that unit-test was taking several minutes to complete.
Unicode has a lot of test-vectors for their algorithms :-)
0
Apr 04 '23
[deleted]
1
u/eyes-are-fading-blue Apr 04 '23
Well said. Certain design values are shown to work starting from 70s.
4
u/ShakaUVM i+++ ++i+i[arr] Mar 16 '23
Clean is orthogonal to performant. It's pretty common for people to "clean up" code and accidentally tank performance. It's a good idea to test for performance regressions even if your correctness tests all pass.
2
u/Benjamin1304 Mar 16 '23
Agree on orthogonality, but the opposite is also true. I've see "horrible" code that performed better after just cleaning it. In the end "clean" code is subjective, even if a book tries to give a definition. For me, the variant version of the example is as clean as the OOP one even though it doesn't adhere to the book's rules.
2
u/ShakaUVM i+++ ++i+i[arr] Mar 16 '23
Sure, it can go either way.
IMO, this is why people need to study both architecture and software engineering techniques
8
u/ZekeDragon Mar 24 '23
Wow, I'm impressed by how bad this article is and how blind most people seem to be to how badly Casey is misrepresenting what "Clean Code" is about and, more specifically, what Robert C. Martin wrote in the book Clean Code - A Handbook of Agile Software Craftsmanship. Casey's video and the article written by some poor Fiverr sod associated with Casey's video asserts that the "performance relevant" advice "they" have given (which, I want to point out, Casey seems to have deliberately left out whom specifically from the video and article) can be broken down into five points:
Prefer polymorphism to "if/else" and "switch"
Code should not know about the internals of objects it’s working with
Functions should be small
Functions should do one thing
“DRY” - Don’t Repeat Yourself
The article than states this: "In order to construct what I would consider the most favorable case for a “clean” code implementation of something, I used existing example code contained in “clean” code literature. This way, I am not making anything up, I’m just assessing “clean” code advocates’ rules using the example code they give to illustrate those rules." The problem with this is that it is a lie. The code provided does not appear in the Clean Code book by Uncle Bob. Instead, a relatively similar set of code is in this book, but there's two big caveats Casey deliberately ignores: The code is written in Java and it isn't being used as an example of clean code. He did all of this because he knew this would not be the "most favorable case" for Clean Code, but specifically because he could point out the performance difference between repeatedly using polymorphism vs using a switch statement vs just computing the value directly using a "table" which is really just an array of four static floats. And this all in C++ and not Java.
The book Clean Code - A Handbook of Agile Software Craftsmanship does use a Shape class in an example in this book, but the context really matters: This is in the section "Data/Object Anti-Symmetry", in which Uncle Bob explains that Data structures and Objects are fundamentally different concepts and implies Clean Code shouldn't mix the concepts. The book states "These two examples show the difference between objects and data structures," not "These two examples show excellent Clean Code practices." The point was to, in a quick and contrived way, show that data structures expose the underlying data and should have no meaningful functions, whereas Objects hide their internal data behind functions that are operated on.
More to the point, the intent was to show the distinction between when using objects is better or when using data structures is better. "In any complex system there are going to be times when we want to add new data types rather than new functions. For these cases objects and OO are most appropriate. On the other hand, there will also be times when we’ll want to add new functions as opposed to data types. In that case procedural code and data structures will be more appropriate." The section literally says "Object Oriented programming isn't always the best way to go" and Casey so brutally misrepresents this as to claim that Clean Code advocates demand the use of OO and Polymorphism in most or all cases.
The term "Prefer polymorphism to "if/else" and "switch"" does appear in the book, specifically in a totally different section 200 pages away called "Smells and Heuristics." The whole section is so small I can quote it directly:
This might seem a strange suggestion given the topic of Chapter 6. After all, in that chapter I make the point that switch statements are probably appropriate in the parts of the system where adding new functions is more likely than adding new types.
First, most people use switch statements because it’s the obvious brute force solution, not because it’s the right solution for the situation. So this heuristic is here to remind us to consider polymorphism before using a switch.
Second, the cases where functions are more volatile than types are relatively rare. So every switch statement should be suspect.
I use the following “ONE SWITCH” rule: There may be no more than one switch statement for a given type of selection. The cases in that switch statement must create polymorphic objects that take the place of other such switch statements in the rest of the system.
This is a far, FAR cry from what Casey implied the book, and by some consequence "Clean Code advocates," taught. It says that polymorphic code is usually preferable to switch statements because in complex code bases you are usually working with a wide variety of different types of data rather than a wide variety of different functions on the same data, but obviously this is highly dependent on the project you are working on.
My biggest issue with Casey's article is not his snide condescention, nor his hilariously amateurish attempts at "improving" the code provided by Uncle Bob. Instead, it is his lies and misrepresentations of what the Clean Code book and the years of experience that Uncle Bob and others had when imparting their knowledge in these books. He outright fabricates a position that "clean code advocates" supposedly take as an easy straw man, then beats that, and this is provably causing ignorant young programmers to reject clean code practices, just by observing his article's comment section.
That disgusts me.
11
u/Clean-Water9283 Mar 16 '23
Wow, this is just wrong on an intellectual level. I spend a lot of time talking about optimization, and this presentation really rubbed me the wrong way.
Clean code is as much about maintainability as it is about performance. The presenter did get a performance improvement by using unclean coding practices, but never talked about maintainability. This video recreates the antique arguments in favor of coding in assembly language, which also cherry-picked examples. These arguments would be unsuccessful in a real developer team and a real project where the classes evolved separately over time.
The performance improvements he obtained owed much to the simplicity of the example. It's ludicrous to think anyone would build a shape->{square, triangle, circle} class hierarchy that only had an area() member. These classes would be bigger, with more members and more operations defined on them.
The presenter got very lucky that all the objects contained the same member variables length and width, so that the area of all objects could be computed as factor*length*width. In a more realistic example where the objects were defined by some notion of an origin point and a size, the area computations would be different. Unclean coding practices might still have produced a speedup in the larger example, but the code would have been an obvious maintenance nightmare.
The presenter's definition of the magical CornerCount() member function in terms of a call to two polymorphic functions is just bad design. Clean coding practices would have made this a member function, saving at least one virtual function call.
In non-toy examples, virtual function calls are a measurable win versus switch statements switching on a discriminator signifying the derived type. The presenter's code was actually carefully tuned to eliminate this advantage. In a fairer test, there would be a vector of randomly assorted shapes.
What's really sad is that this same presentation could be retooled as a really good example of the advantages of data-oriented design, where clean coding practices were still used, but the encapsulation is done differently.
4
u/Corendos Mar 17 '23
You missed the whole point of the video, it can be summarized with: "Clean code" has a measurable impact on performance and no measurable impact on maintainability. If you compare that to what is proposed as an alternative, something that has no measurable impact on maintainability but a clear improvement with respect to performance, it's not hard to see why "Clean Code" is probably not a wise choice of guidelines to follow.
By the way, I'll add that this triggered a discussion between Uncle Bob and Casey that is highly valuable : https://github.com/unclebob/cmuratori-discussion/blob/main/cleancodeqa.md
6
u/Clean-Water9283 Mar 17 '23
Casey has provided us with a little optimization example, albeit using a contrived and unrealistic program. However his conclusion, that clean coding is a bad idea, does not follow from his example.
Just because virtual function calls reduced performance in this example doesn't mean that they always do so. Just because a switch statement improved performance in this example doesn't mean it always does so. In fact the clean coding rule to prefer virtual function call over if/switch exists partly because it improves performance in many cases.
Just because there isn't a simple command line tool for measuring the maintainability of a 40-line program doesn't mean that the effect of poor maintainability is not discernible. I would expect that casey's proposed changes, writ large across a project, would have a devastating impact on maintainability.
Thanks for the link to uncle bob's convo on this topic. I thought bob's answer was weak, and that casey had clearly been prepping for this debate for awhile. I go back to Knuth's famous quote ("97% of of the time...optimization is the root o all evil"). Most of the code in an IDE, browser, or compiler is not executed frequently, so that even if casey wins the battle at the nanosecond level, he would lose the war trying to get software built without considering maintainability out the door. Casey is right only inasmuch as there are a few places in every project where squeezing out the last iota of performance trumps the wise consideration to build maintainable code.
I don't know or follow casey, but his arguments are typical of inexperienced developers who think they are so smart that rules don't apply to them. An emphasis on maintainability comes from experience, which these developers lack, and so do not value. Such developers would be well served to learn and follow clean coding rules until they know enough to find the few places where breaking those rules improves overall performance.
1
u/Corendos Mar 17 '23
a contrived and unrealistic program
Something coming from the "Clean Code" book, addressed to beginners that have no way of knowing that
Just because virtual function calls reduced performance in this example doesn't mean that they always do so
That's what you claim but you provide nothing to support it. Meanwhile there are dozens of examples supporting the fact that static dispatch is way faster than dynamic dispatch.
I would expect that casey's proposed changes, writ large across a project, would have a devastating impact on maintainability.
You would expect, but have nothing to support it as well. Meanwhile there is a measurable performance delta.
"97% of of the time...optimization is the root of all evil"
A sentence that is often taken out of its context and misinterpreted. It does not mean that the 3% of case where performance matters and you have to "optimize" implies that the 97% of the rest you have to waste computing power. That's the whole point, he argues that there is a way that is not measurably less maintainable but measurably more performant. That's just a win-win situation, you may choose to ignore it but that's just laziness/pride/whatever.
And I won't even comment your last paragraph because that's just wild guesses on the character.
5
u/Clean-Water9283 Mar 18 '23
[Sorry, I don't know how to quote your comment in reddit. Sigh.]
The fact that "the Clean Code book" uses an imperfect example in no way proves casey's point.
Virtual function dispatch is unambiguously slower than static function dispatch. I never claimed otherwise. That doesn't mean that a virtual function is slower than a statically dispatched function containing a chain of if statements or a switch statement with nonconsecutive case values. I actually have demonstrated that, in chapter 7 of my book Optimized C++.
You're correct that I have no proof, no surveillance camera video of bad programming actually causing a maintainability problem, and you do have a contrived example showing a performance improvement. Nevertheless, casey's article only proves that a specific example can be optimized, not that the optimizations have no impact on maintainability.
You're right, that quote from Knuth is often taken out of context, as you just did. I am aware of its full context, and the two or three different places where Knuth said similar things. Here's a longer version of the same quote with more context.
Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered. We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.
—Donald Knuth, Structured Programming with go to Statements, ACM Computing Surveys 6 (Dec 1974): 268. CiteSeerX: 10.1.1.103.6084
Professor Knuth doesn't have any measurements of maintainability to support his opinion either, but a whole lot of very smart, very senior people think it's true. Do you really want to defend the contrary opinion?
2
u/Corendos Mar 18 '23
That doesn't mean that a virtual function is slower than a statically dispatched function containing a chain of if statements or a switch statement with nonconsecutive case values I agree but that's outside of the scope. One of the key point of the article is that the "prefer polymorphism to "if/else" and "switch"" general rule is a bad one because it has no measurable impact on maintainability/productivity but measurably waste computing power. That's all there is to it. You can say that the example is imperfect but one counter point is enough to discard a general rule.
I see you are talking about "optimization" but that's in no way what was done there. Optimization happens when you already have something performant and you want to squeeze the last percent left. That's when you dive into analyzing generated assembly etc. Here, we are talking about order of magnitude of difference and there was no analysis of the generated assembly so that doesn't fall into the "optimization" category.
Simply having in mind how a computer executes code let you architecture your code in a way that is not measurably less maintainable but measurably more performant. That's why I keep saying that it's a win-win situation.
As for the Knuth quote, it actually supports my claim: don't "optimize" (as in "squeeze the last percent of performance") prematurely because in 97% of the case, it won't be useful. Somehow this quote is now being interpreted as "it's okay to completely ignore performance for supposed maintainability/productivity gain 97% of the case". No wonder software quality has decreased over the past decades with this mindset...
Anyway I'm not here to convince you, I can only suggest that you give it a try.
4
u/Clean-Water9283 Mar 18 '23
I agree with you only in part. It's always a good idea to write code that uses fewer computing resources. (I even said so in my book, which I mention only as a reference). Where we part ways is that I believe there are other considerations, including productivity, maintainability, and delivered quality, whereas you profess that performance trumps all other considerations.
According to Amdahl's Law, if the most frequently executed part of the code consumes 90% of run time, then even if heroic programming could reduce the cost of the remaining code to zero, performance would only increase by about 11%. The question then becomes, how much are you willing to sacrifice to obtain this modest performance increase? Often the most frequently executed code comprises less than 10% of the total lines of code in a project. (Pareto says 20%, Knuth implies 3%).
You keep saying "not measurably less maintainable" as if your inability to measure maintainability was an excuse to ignore it altogether. Surely your experience as a developer allows you to distinguish between code that is easy to work with, and code that is a hot mess. This is the part of the argument which I find so intellectually dishonest that I was compelled to confront it.
0
Apr 04 '23
[deleted]
2
u/Corendos Apr 04 '23
Donald Knuth's and his student mocked a guy at an algorithms conference for spending 2 years making a sort become log*
Yes, that's what I'm saying
If the performance of the application is already acceptable and not detrimental to the user experience then making it even 10000000000000000000000x faster with 1 line of code is not only a waste of time but actively making the code worse
No that's not, you have no measurable metric to claim that it's "making the code worse".
But don't worry, it's fine if you want to ignore it you're allowed to be lazy.
0
Apr 04 '23
[deleted]
2
u/Corendos Apr 04 '23
I find it funny how you guys think these concepts are just from a book instead of being a culmination of tons of academic papers, category theory, type theory, ADTs, and 40 years of enterprise experience
Please show me where I said that, you're out of topic. Are the concepts presented in the video in this book ? Yes. End of discussion, nothing else was said.
But no Bob voted for Trump and he's a big meanie who tells you to write something a human being can read so he must be wrong
What the heck has politics anything to do with that ? Stop playing the victim, I couldn't care less about his political views as I'm not even American and don't think it has any consequences on his opinions about software development.
4
u/KuntaStillSingle Mar 15 '23
Klaus Iglberger gushes over variant when talking about visitor pattern: https://youtu.be/PEcy1vYHb8A?t=1637, https://youtu.be/PEcy1vYHb8A?t=2130
7
8
Mar 15 '23
The funny part is, that Casey didn't even do a good job at optimizing it.
Instead of doing this hacky reducing of formulas to the same formula, you can just use a new vector for every type (SOA).
This is faster, and more extendable: https://quick-bench.com/q/dGEwQftsG83Vb5t0LA6VLkKhjPk
3
u/Hedede Mar 21 '23
There's a mistake in your code. You forgot to add
breaks which makesstd_multiappear slower than it is. See my other comment.1
u/OlivierTwist Mar 19 '23 edited Mar 21 '23
This is faster, and more extendable: https://quick-bench.com/q/dGEwQftsG83Vb5t0LA6VLkKhjPk
Just faster and this variant is the most memory in-efficient.Edit: sorry, I was wrong about memory, it is deferentially more efficient than switch. Still it requires changes in several points for each new type (unlike "clean" case with dynamic polymorphism).
1
1
u/Hedede Mar 21 '23
Is it? If the shapes had different sizes in memory, it would be more memory efficient compared to
std::variant. That's becausestd::varianthas to have the size of the largest type.1
1
u/digama0 Mar 27 '23
FYI, Casey was deliberately avoiding this approach for the purposes of keeping to the original specification of the problem. He mentions this approach in passing in his discussion with Uncle Bob:
I agree broadly, for example, with the "Data-oriented Design" people: most of the time, you would be better off if code always knew what type it was dealing with. If you have circles, rectangles, and triangles, then you have three separate arrays, one for each type, so that there never is a dispatch.
1
u/muchcharles Mar 16 '23 edited Mar 16 '23
I think that would make many more assumptions on access and removal patterns on the array of shapes. He did something that is more like a pareto-optimal improvement. I think his is no worse on any use case.
2
u/SleepyMyroslav Mar 16 '23
Memory layout and size are important. Please look at bench results.
Code that assumes that different shapes have similar memory size might be wrong in practice. I had real examples when worked with EDA. Using more memory for each shape is a waste of both memory and performance.
3
u/Hot_Anteater_4691 Mar 21 '23
The issue with this example is: the overhead for a virtual method compared to the trivial computation done in the method is high. But it would simply would not matter unless you call it millions of times.
1
u/OlivierTwist Mar 23 '23
...or if these functions do something a bit more heavy than just one multiplication. Example.
8
u/argothiel Mar 16 '23 edited Mar 16 '23
His whole approach is terrible. He took virtual polymorphism, rewrote it into some hardcodes using the compile-time knowledge, obfuscated and scattered the logic throughout the entire code. If he wants to add a class, he needs to update several places in the code, and if he forgets one, he gets the UB.
While in the alternative approach you use static polymorphism, which gives you the same or better performance, and you have to modify only one place in the code with all the safeguards present and detectable at the compile time.
I wanted to comment on his blog but apparently you need to pay to be able to comment.
5
u/cdglove Mar 16 '23
I wanted to comment on his blog but apparently you need to pay to be able to comment.
He'll also block you on twitter if you criticise him. Ask how I know :-)
It seems he gets off on generating outrage but isn't really interested in hearing opposing views. Even if he does engage in discussion he tries to trap the other side.
Example: https://github.com/unclebob/cmuratori-discussion/blob/main/cleancodeqa.md
2
u/argothiel Mar 16 '23
He's pretty good at generating outrage. I've spent several hours writing a better solution to his problem fueled only by rage even while expecting that I was not going to share it anywhere.
1
u/argothiel Mar 16 '23 edited Mar 16 '23
Here are some ideas for a modern implementation using the same abstraction the author used for creating a raw table of coefficients, but put into classes. Note the benefit of having a circle with an actual radius member instead of width and height. And you can optimize it even further for the clean code (I admit I was too lazy to perfect it, but I hope you get the idea):
5
u/smozoma Mar 15 '23
previous post of this article with critiques
https://old.reddit.com/r/programming/comments/11dyx43/clean_code_horrible_performance/
2
u/jack_acer Mar 15 '23
I wonder how it would compare to a container like https://github.com/nasosi/eumorphic
Or https://github.com/ollieatkinson/Eumorphic
(But in swift)
11
Mar 15 '23
--------------------------------------------------------- Benchmark Time CPU Iterations --------------------------------------------------------- clean_code 8392305 ns 8391370 ns 87 switch_case 5795073 ns 5794445 ns 120 std_variant 7469332 ns 7468529 ns 95 with_multi 969806 ns 969718 ns 746 with_eumorphic 913953 ns 913866 ns 716
with_multiis basically what eumorphic does but done by hand, they have the same performance (https://quick-bench.com/q/F9RKHd4-fOYBNL0MlZsYuEq81O4)This was the eumorphic code:
template <class T> using segment_container_t = std::vector<T>; using collection_t = eumorphic::collection<segment_container_t, square, rectangle, triangle, circle >; inline collection_t shape_builder(std::size_t count) { std::srand(std::time(nullptr)); collection_t shapes; for (std::size_t i = 0; i < count; i++) { switch (std::rand() % 4) { case 0: shapes.insert(square{utils::random_float()}); case 1: shapes.insert(rectangle{utils::random_float(), utils::random_float()}); case 2: shapes.insert(triangle{utils::random_float(), utils::random_float()}); case 3: shapes.insert(circle{utils::random_float()}); } } return shapes; } inline float total_area(const collection_t &shapes) { float total_area{}; eumorphic::for_each( shapes, [&]( auto &&elem ) { total_area += elem.Area(); } ); return total_area; }4
u/Hedede Mar 21 '23 edited Mar 21 '23
You made the comparison unfair by forgetting the
breaks in the switch, your code has to do 4x work. Sowith_multiis faster even under unfavorable conditions. ;)However, std_variant with pre-sorted data is pretty close: https://quick-bench.com/q/ie4Wh6NLFB20MXjsDJ_uRor5ntI
This clearly demonstrates that on modern CPUs the way you organize data is way more important than the algorithm itself (as long as it is reasonably efficient). Bunch pointers to data scattered all over the memory -> bad locality -> poor performance. Randomly ordered data -> branch mispredictions -> poor performance.
1
Mar 21 '23
oh, wow that is way faster. Cool to see that the sorted variant approach has similar speed, although you run into problems with it once one shape gets to big.
1
u/OlivierTwist Mar 22 '23
although you run into problems with it once one shape gets to big.
Because of sorting?
1
Mar 22 '23
Doesn't a variant use a union under the hood, so each element has the size of the biggest type inside of the variant.
1
u/OlivierTwist Mar 22 '23
Yes, there is a memory overhead which can be significant. But so far that is the only alternative to virtual functions which uses standard containers and doesn't require change of total_area function when new shape is added.
1
u/OlivierTwist Mar 22 '23
I made another variant based on "sorted variant" approach where adding new shapes doesn't require touching multiple places (e.g. shape_builder).
It is almost as good "multi" and "sorted variant" in terms of performance, but easier to extend with new shapes.
1
6
u/justinkroegerlake Mar 16 '23
you know he knows he's wrong when he disables comments on the video.
Having worked in some very clean, and very unclean, codebases I can tell you the right metrics are
how many production-facing bugs there are
how many hours it takes you to find something in the code
how long it takes to figure out how to modify the code
We also can delete all our tests without affecting the runtime, should we do that too?
2
u/Corendos Mar 17 '23
What's the point of enabling the comments where the majority of what will be posted will be comments with a lot of "gotchas" or trying to own the author? It's not like comment section is a place where valuable discussions will take place, so if that's just noise better disable them
3
u/justinkroegerlake Mar 19 '23
everyone knows there's gonna be dumb comments on any youtube video and to ignore them.
If I'm looking at a video on electrical work and (now, with no dislike count) I see the overwhelming majority of comments are explaining why something is unsafe, I move on. I'm sure this video would have a lot of reasonable, critical comments, as it does right here in this thread.
5
u/streu Mar 15 '23
The gripe that I have with that blog article (and, likewise, with Uncle Bob's teachings) is that it claims that there is one version that is somehow more correct than the other.
If we are talking about managing shapes, it absolutely matters what I'm doing this for. It's a big difference whether I'm managing a 3-D scene before or after triangulation. Before, I want virtual functions and the flexibilities I get from that. After, I have only triangles and lines remaining that I want to throw at my shaders at maximum efficiency. Of course, the data structures and C++ features I use for that will differ significantly.
11
u/BlueDwarf82 Mar 15 '23
(and, likewise, with Uncle Bob's teachings) is that it claims that there is one version that is somehow more correct than the other.
"Consider this book a description of the Object Mentor School of Clean Code. The techniques and teachings within are the way that we practice our art. We are willing to claim that if you follow these teachings, you will enjoy the benefits that we have enjoyed, and you will learn to write code that is clean and professional. But don’t make the mistake of thinking that we are somehow “right” in any absolute sense. There are other schools and other masters that have just as much claim to professionalism as we. It would behoove you to learn from them as well.
Indeed, many of the recommendations in this book are controversial. You will probably not agree with all of them. You might violently disagree with some of them. That’s fine. We can’t claim final authority."
Clean Code, Chapter 1
3
u/MoreOfAnOvalJerk Mar 16 '23
His solution uses a switch statement and a union, so its essentially the same thing that variant does. It also has the same problem that variant does where if one type is huge, the overall cache locality of an array of the variants/unions goes down and perf suffers.
I think it’s kind of preference between variant and unions, but I find variant to be a better and safer construct. It doesn’t come saddled with the weird requirements for micromanagement that union has, and it has some nice convenient functions that let you hook in lambdas. Unions+switch can do that too, but there’s more boilerplate and you can easily screw up if you have a data type in the union that requires a construction/destruction.
2
3
u/NeilSilva93 Mar 16 '23
What weirded me out was the blog writer's coding style. Lower case for type/class names and upper case for variable names? Too freaky man.
5
u/XDracam Mar 16 '23
The entire video is just a propaganda shitshow. It's so bad that I haven't even looked at the blog post. He's ignoring decades of history just to show how his tiny code snippets run O(1) faster while being significantly worse in all other aspects.
3
u/n4jm4 Mar 15 '23 edited Mar 15 '23
if/else is often a code smell that something is wrong in the data model, or even wrong in the feature request. That someone is attempting to do way too much with an application, lacking a consistent, universal data model.
While it is true that some HPC optimizations involve baroque code, it is also true that a boring, simple, clean, computationally efficient core forms the foundation of a highly performant program.
Any deviation from elegant, maintainable, intuitive code should be the exception, no the rule. Even little quirks like resorting to star char instead of higher level string data types, are more often than not, a mistake.
Many HPC optimization quirks like manually unrolled loops, turn out to be premature, or even cause stalls. They can be artifacts of a poorly written benchmark.
Today's apps have bloated to the point where you can't even run the whole system locally. Whether monolith or distributed monolith, doesn't matter.
They can be accidents of a limited environment, such as the engineer's particular workstation--at the first expense of performance on the production environment.
They can be accidents of old compiler implementations. Updating to a modern compiler standard like C++17, C++20, C++23, on more recent LTS distributions, will result in better performing programs.
A lot of people get hung up on mantras at the expense of quality programming.
Don't let over-engineering get in the way of attending to the true root cause of a bottleneck. Question the business requirement first. Implementation details naturally follow from the miscreant assumptions of some C level management dolt.
As an aside, I believe OOP has equal amounts of good points and bad points. The bad points involve inheritance and object fetishization and too much emphasis on defensive / futureproofing API's. Java (a horrid OOP implementation) has become a rotting pile of Pattern Commandments. To the detriment of the average program.
Encapsulation and reuse is good to an extent. But functional programming takes the best concepts of OOP and supercharges them. I submit that Haskell and Rust and Chicken Scheme will win out in the long run, as their optimizations eclipse what is possible to do in other languages.
Yes, we can work around some of these foolish assumptions. But that's like trying to cut grass with a nailclipper, when well known tools like a lawnmower would have been able to dramatically outperform the dawdling, overspecified, top down, out of touch system far removed from consumer needs. We don"t need faster horses, we need automobiles.
Alexandrescu makes a lot of strong points for doing HPC. I would highlight the wisdom of keeping codebases small. Not only do small codebases focus attention on fewer concerns, but compilers find smaller codebases easier to mechanically optimize. And they tend to fit icache better. And you can't reason about a program too big to fit completely in your head.
6
Mar 15 '23
A series of "ifs" is really not as bad as people make out.
In the case where it becomes 100s of "if" statements then its likely hard to follow.
But a series of "ifs" is very easy to read.
For that reason the "if" is easily transformable to other ways of solving the problem. That quality should not be understated.
2
u/FrezoreR Mar 15 '23
This is just another take on the age old mantra: you either to get safety or speed.
It's no secret that abstraction sometimes adds runtime costs. It's just that for most applications it doesn't matter. If it indeed does matter then optimize those cases.
Finally, I disagree that the things he calls out can't me measured. It's just that a lot of that is measuring things like cognitive load, which is not something a typical engineer would measure. Although, there tons of studies to back some of that up.
I kind of like the pragmatic approach. Actually, the pragmatic programmer book is a good reference here.
It's not surprising that cpo developers think a lot about performance, but it is interesting to see these conversions popping up over and over :)
I mean sometimes you even want to write assembly to get that last bit of performance, but it again comes with a cost.
1
u/OlivierTwist Mar 20 '23
FYI: /u/Benjamin1304, I made "clean contiguous memory" variant: run time polymorphism with objects located in contiguous chunk of memory.
Unfortunately the benchmark isn't very useful because the results very a lot based on shapes count and order of tests.
1
u/Hedede Mar 21 '23 edited Mar 21 '23
It's still slower than other alternatives even when the data is sorted: https://quick-bench.com/q/hDT5q4GeTB2d5KpAYfkpjdCQqus
Edit: I made a mistake by forgetting that after sorting pointers still point to their original data.
Here's the corrected one: https://quick-bench.com/q/8SqG8sIO8KDp0uwi2ZrH_6Nm1-s
It is 2x faster than the original code, but still not as fast as
std::variant.1
u/OlivierTwist Mar 21 '23
It should be slower because of virtual functions, but that is the only variant with run time polymorphism and the easiest to add new shapes.
1
u/SplinterOfChaos Mar 16 '23
The thing that gets me about the whole "horrible performance" thing going around is that as a game programmer (and this has been shared by a number of game devs), 60 frames per second is 16 milliseconds. It doesn't matter if my update loop requires 15 milliseconds or 2, my game has the same performance in both cases.
It's really important stuff to know for performance-critical code but if that's not where your profiler is saying your problem is, it probably doesn't matter.
6
u/Zanarias Mar 16 '23 edited Mar 16 '23
It doesn't matter if my update loop requires 15 milliseconds or 2, my game has the same performance in both cases.
Maybe, but...
A 16ms frametime target is pretty subpar on today's hardware, even my crappy-cheapest-available phone has a 90hz screen and 120-144hz monitors are fairly common now too especially in the gaming market (if you're targeting PC of course). This is ignoring displays that are even faster. Do you have enough spare time to meet these thresholds? 7ms update cutoff for 144hz displays.
Additionally, due to how current graphics APIs function, the best way to improve overall "game-feel" in a game is just to brute force render as fast as possible with vertical synchronization disabled (to reduce input lag). You only start to get a real smooth experience at 300-400+ frames per second as the gap between the rendered frame and the screen refresh is short and consistent enough to be imperceptible. This sounds high but really isn't even for semi-complex 3d environments. 2.5ms update cutoff for a 400fps target.
For the best possible experience for players, you really should be a lot closer to your 2ms than your 15ms. Games are not really the right sector to be randomly abandoning performance for developer convenience, if at all possible.
2
u/SplinterOfChaos Mar 16 '23
Even at 144hz, the logic is the same. That means 6.94ms and the difference bewteen an update loop that happens in 2ms vs 6ms leads to identical player experiences.
Games are not really the right sector to be randomly abandoning performance for developer convenience, if at all possible.
What I'm saying is not in contrast to that. What I dislike is the "performant by default" way of thinking which often leads devs to optimize parts of their system which do not actually affect performance, or if they do, do not improve the player experience. Performance considerations should be driven by profiling and data, not ideology.
2
u/SplinterOfChaos Mar 16 '23
You only start to get a real smooth experience at 300-400+ frames per second as the gap between the rendered frame and the screen refresh is short and consistent enough to be imperceptible.
Forgot to respond to this: I don't find that to be true and many devs speak of having measured upwards of 100ms input delay on consoles. Having faster update frames doesn't improve the amount of time it takes wireless controllers to communicate with the console and the difference between responding in 100ms vs 110 ms is negligable.
According to most UX studies, as long as a game responds to input within 100ms, it is perceptibly "instant"
1
u/axilmar Mar 16 '23
The solution is simple: use templates. Put the shapes into a tuple-like structure, then iterate over the tuple (iteration will be at compile time).
The solution will have no virtual dispatch, will be cache friendly, and also OOP.
It's articles like that that give C++ a bad name without deserving it.
1
u/OlivierTwist Mar 22 '23
Templates are good, but they solve different problem in general.
1
u/axilmar Mar 23 '23
Sure, but it can be used to solve this problem as well: keep stuff organized in objects, have very good performance. Even better if switch is avoided.
1
u/OlivierTwist Mar 23 '23
No, they can't solve the problem when dynamic polymorphism is needed. Casey Muratori is doing terrible job for by opposing dynamic polymorphism to static one. They are just for different use cases. The way he optimize the code may be acceptable in some situations (quite specific), but this style should not be used by default.
For reference: I wrote "std_variant_sorted_flexible" template solution which is also "clean": DRY and very easy to extend.
2
1
u/axilmar Mar 23 '23
Sure, but the keyword here is 'needed'. For the sort of code given in the example, it is not needed.
If polymorphism is needed, then vtables can be avoided by a structure that contains a pointer to a function, for performance.
Then the CPU wouldn't need to load the vtable, then access it in order to find the function to call...it would directly call the function stored in the struct.
-2
u/lazyubertoad Mar 15 '23
That guy is a hoax! Disclaimer - I did not even read the Clean Code, lol. I'm afraid of (dynamic) polymorphism, actually, but the shapes example looks sane to me.
The area calculation is highly unlikely to be a bottleneck! Can you please give an example where the area function shows in the profiler? So yeah, code that takes some 0.05% time to run can be made 40% faster. Yeah, but so what? Have you ever saw virtual calls causing problems in the profiler? I did not. That probably happens, but likely not often.
In case for some reasons we have gorillions of shapes - I'd handled that case separately, like grouped them by type and called the class area function directly. Maybe added a function, like calcMassAreasOfSameType to have whatever possible optimizations. That'd be even faster than the switch or having some tables, as I wouldn't even do that and I'd keep the virtual function intact in case it still needs to be called here and there.
Then he outright shits on the conditions and says - OK, but if we ignore that our code is no longer correct for the circle shape (which is the reason we have it polymorphic, ffs!) - then we can have it even faster. Sorry, at that point he can outright skip everything, and just output a random fixed number - but hey, that'd be fast!
That code is also likely have UB, because he did not made the destructor virtual. Is it like that in the Clean Code or that guy just doesn't really know C++?
There are no absolute rules, but all the rules that are worth it have a reason for their existence and God helps you if you disregard those reasons.
-8
u/00jknight Mar 15 '23
Casey is the Uncle Bob of tomorrow.
1
u/dodheim Mar 18 '23
I applaud you for not deleting this comment despite the downvotes, you're absolutely right. Anyone whose primary message is dogma is bound to be laughed at in the future once everyone's been handed that easiest and laziest of perspectives, hindsight. Casey's zeal and intent on ignoring any unfavorable (to him) details is actively painting him into Clown Corner, whether most people see it that way yet or not.
-4
u/noooit Mar 15 '23
Using variant and visit is irrelevant to the video though. Those are actually just typesafe improvement over c.
What he is actually saying is that people going OOP by default all the time is the problem.
imo, those people are usually the ones who also make stupid class.
3
u/Benjamin1304 Mar 16 '23
Sure variant is a typesafe union but in this example it's easy to see how little the code changes when going from OOP to variant compared to the complete rewrite when going from OOP to enum.
He is "demonstrating" how each aspect of "clean code" affects performance and that's fine, but failing to admit that there is a solution that is very much in line with the original code but performs better (and as good as his handrolled optimization) is dishonest.
I agree with you that it's bad when people say "you should always to this", but he's doing the same thing...
1
u/noooit Mar 16 '23
It's not dishonesty. It's just one demonstration and he doesn't claim his way should be the default.
But again, your variant implementation is irrelevant to what he wants to say. He could replace the implementation with yours and yet his message stays exactly the same.
-6
u/Pupper-Gump Mar 15 '23
cpp is a superset of c. This means it has everything c does with very few exceptions. Anyone on their high horse about typing more code and creating more potential issues with object data is deluding themselves.
7
7
Mar 15 '23
[removed] — view removed comment
13
u/Astarothsito Mar 15 '23
In the OOP example the data is scattered around the heap and you get no vectorization and cache misses. Additionally you add virtual function calls and branch prediction misses because the code is more dynamic and less predictable.
We can easily fix it by allocating each structure together in a vector instead of random, and by putting final in the implementation classes and with that simple easy change OOP becomes one of the most performant options and the most maintainable.
1
Mar 15 '23
The issue is if you have a vector of objects with members m1,m2..., and if you want to loop and do something to m2 of each object, you will end up caching m3, m4 etc of the object.unnecessarily and will end up with more cache misses compared to an array of m2s.
0
u/James20k P2005R0 Mar 15 '23
+1, laying them out contiguously helps but isn't the core issue. You'll get drastically better performance in the data oriented case where eg element is laid out in its own vector
Even if you do have a vector of say vec3f vs 3 vectors of floats and they're all used, you still often get significantly better performance in the latter case. I suspect its partly because its easier for the compiler to vectorise, partly because there's less shuffling of memory to get everything where it needs to go, and partly because your memory stride is now 4 instead of eg 12 or something weird
A vector of objects is generally very poorly performing for high performance numerics!
2
u/Astarothsito Mar 15 '23
Sorry, I was thinking in a vector per shape, and if we really need it we could even construct another of pointers of the base class pointing to the real vectors holding the data.
So all shapes would be operated "by batches" instead of randomly.
4
u/mark_99 Mar 15 '23
SoA was basically invented to facilitate SIMD, so it's always going to be better if you're doing that type of operation on the data. You need to go to AVX512 for 'gather' https://www.felixcloutier.com/x86/vgatherdps:vgatherdpd
The other win is if you are only touching some of the fields, you don't end up loading a lot of memory you don't then use due to cache line quantization.
Stride generally doesn't matter. Small objects like vec3f are in 1 cache line anyway, and even if not the CPU/MMU can prefetch by stride and load many such streams at once.
Sorting / partitioning polymorphic objects can definitely be a win, a vcall is basically a branch so the branch predictor gets it right if they are the same target for long runs, plus the code is in the i-cache.
You still have the downside that virtual fns are not inlined, that's usually what matters more than the virtual call itself.
2
u/SkoomaDentist Antimodern C++, Embedded, Audio Mar 15 '23
You need to go to AVX512 for 'gather'
AVX2 is enough for simd gather operations and doesn't suffer from the performance hit AVX512 can trigger.
2
u/Astarothsito Mar 15 '23
Sorting / partitioning polymorphic objects can definitely be a win, a vcall is basically a branch so the branch predictor gets it right if they are the same target for long runs, plus the code is in the i-cache.
You still have the downside that virtual fns are not inlined, that's usually what matters more than the virtual call itself.
That's the reason for a unique vector for each derived class, if the functions or class are mark as final and we call it directly instead of using the base they are inlined and the interface of the function is still enforced.
2
Mar 15 '23
[removed] — view removed comment
8
u/tjientavara HikoWorks developer Mar 15 '23
You can make a polymorphic wrapper, where the wrapper object has an internal buffer and you allocate the polymorphic object inside this buffer. In other words: a
unique_ptrwith small object optimization.I actually use this technique for type-erased messages on a ring-buffer for logging.
5
u/Astarothsito Mar 15 '23
How would you allocate a vector of polymorphic types? The pointers would be contiguous but they would all point to different addresses decided by the MMU.
In this scenario I was thinking of a vector per shape so you would have std::vector<circle>, std::vector<square> and so on, the code doesn't increase a lot but the manager class should hide each individual "for loop" like the function in the original C. Obviously, you don't have a single expression for everything but having 4 for each loops is not that difficult to maintain.
1
227
u/coachkler Mar 15 '23
Anyone who's too rigid with rules is doing it wrong. "Clean code" is mostly there for maintainability. And while this trivial example gives a major speedup, it's just that trivial.
When you have a large code base, hundreds of thousands of lines, not every path is time critical.