4 ms·
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 devi
by anonymoushn 4y ago
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.
- Arnavion 4y ago>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 https://gcc.godbolt.org/z/498nKEzhK [2]: https://gcc.godbolt.org/z/WqhP3fzxK https://gcc.godbolt.org/z/WqhP3fzxK
- Tomis02 4y agoCorrect 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?
- muricula 4y agoThe enum to tell what shape an object is costs something, probably 4 bytes. Storing the 8 byte vtable constant isn't so bad considering.
- adamrezich 4y agolooking at a value is faster than dereferencing a pointer—why is this controversial?
- muricula 4y agoTo fix 2, add '-fno-rtti' to the args: https://gcc.godbolt.org/z/zMdfx71qE https://gcc.godbolt.org/z/zMdfx71qE Unfortunately, gcc does not seem to devirtualize in your example
- int_19h 4y agoEven 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).
- signaru 4y agoMartin 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 the same function with the same argument multiple times instead of saving the result in a variable for reuse.
- jameshart 4y agoThe 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)