News for hackers. Stuff that matters.
Re:“Clean” Code, Horrible Performance by anonymoushn on Tuesday February 28, 2023 @04:02AM (#34967197)
I think "them" is someone named Robert Cecil Martin, but I'm not sure if this example from the video appears in his book.

What compiler are you using that devirtualizes every class hierarchy? I suspect that Casey is using C++ so he may (unfortunately) have multiple translation units in his program.

Re:“Clean” Code, Horrible Performance by Arnavion on Tuesday February 28, 2023 @04:30AM (#34967396)
>I suspect that Casey is using C++ so he may (unfortunately) have multiple translation units in his program.

Yes, the video uses C++.

Obviously if one compiles a library then the compiler has no way of knowing that other subclasses of `shape_base` do not exist. My point is that when compiling a binary as they are doing for their video, the compiler knows that there are no other subclasses that it needs to cater to.

It might require LTO explicitly, of course. At the very least godbolt doesn't devirtualize without LTO [1], but godbolt itself breaks if I enable LTO [2] and I CBA to test locally right now.

[1]: https://gcc.godbolt.org/z/498nKEzhK

[2]: https://gcc.godbolt.org/z/WqhP3fzxK

Re:“Clean” Code, Horrible Performance by Tomis02 on Tuesday February 28, 2023 @09:24AM (#34969590)
Correct me if I'm wrong. Even if the compiler devirtualizes the classes, you still have the memory cost of storing the vtable pointer in each of the object instances (8 bytes for each instance), which means you need to do more fetches from memory. Does CPU prefetching negate the cost of these additional memory lookups?
Re:“Clean” Code, Horrible Performance by muricula on Tuesday February 28, 2023 @04:11PM (#34974905)
The enum to tell what shape an object is costs something, probably 4 bytes. Storing the 8 byte vtable constant isn't so bad considering.
Re:“Clean” Code, Horrible Performance by adamrezich on Wednesday March 01, 2023 @03:40PM (#34987994)
looking at a value is faster than dereferencing a pointer—why is this controversial?
Re:“Clean” Code, Horrible Performance by muricula on Tuesday February 28, 2023 @04:09PM (#34974882)
To fix 2, add '-fno-rtti' to the args: https://gcc.godbolt.org/z/zMdfx71qE Unfortunately, gcc does not seem to devirtualize in your example
Re:“Clean” Code, Horrible Performance by int_19h on Tuesday February 28, 2023 @07:23PM (#34976987)
Even when the target is an app, said app might be loading DLLs at runtime which may contain other subclasses.

The fundamental problem here is that C++ doesn't have a good abstraction to represent visibility of public types, since any other translation unit - even across the DLL boundary! - can re-declare the type and then derive from it. The only way to constrain visibility is to use anonymous namespaces, and that only works if the type can be confined to a single unit (that C++ compilers seem to ignore the optimization opportunities here in practice perhaps indicates just how rare this actually is).

Re:“Clean” Code, Horrible Performance by signaru on Tuesday February 28, 2023 @07:34AM (#34968563)
Martin Fowler's "Refactoring" also has "Replace Conditional with Polymorphism". I like the intent of the book but I don't follow it 100%. Another part of that book that tripped me is where he calls a function with the same argument multiple times instead of saving the result in a variable for reuse.
Re:“Clean” Code, Horrible Performance by jameshart on Tuesday February 28, 2023 @04:13PM (#34974939)
The existence of a refactoring in the Fowler refactoring catalog is not normative advice that it should always be applied. Refactoring also has “Extract Function” and “Inline Function” as refactorings. They can’t both be right…

Refactorings are moves you can make. Choosing when to make them is up to you. In fact, Fowler provides guidance along with each refactoring suggesting when it might be applicable (i.e., not always)