Summary: An artificial test on indirection shows that indirection is slower than no indirection by a significant amount. A bit pointless imo. It would be more interesting to talk about maintainability, flexibility and overhead when avoiding this kind of indirection inside hot loops but allowing it outside, in 'management' code.
It gets far more interesting with actual code in your classes.
I have a real-world example of whole-program benchmarking showing an improvement in performance by using a pointer to an interface type (with the concrete classes further using PIMPL) rather than stuffing everything directly in the top-level class. So multiple indirections were faster than no indirection at all. (Slightly. Averaging ~1.4%, but always positive over many runs ranging from a few seconds to several months.)
Exactly. You have so much 'OOP Bad' stuff posted, which newbies read and just assume OOP bad. In this case, it's only bad when that difference in performance matters. In a huge amount of code, it just doesn't, and the benefits of using it are pretty much pure win. Even in a lot of cases where it might impact performance a little bit, it will probably still be a win if it makes the code easier to maintain and more flexible.
30
u/SirB May 26 '22
Summary: An artificial test on indirection shows that indirection is slower than no indirection by a significant amount. A bit pointless imo. It would be more interesting to talk about maintainability, flexibility and overhead when avoiding this kind of indirection inside hot loops but allowing it outside, in 'management' code.