4 ms·
> flexibility is something you pay for in performance I lightbulb moment I had recently was that the classic interface-based OO programming paradigm utilising
by jiggawatts 5y ago
> flexibility is something you pay for in performance
I lightbulb moment I had recently was that the classic interface-based OO programming paradigm utilising virtual functions was essentially the following declaration by the programmer to the compiler: The function that is invoked can change at any time, even call-to-call; assume nothing. it is a black box with unknown, unknowable contents. Do not inline it, do not inspect it, expect the worst.
If you look at a typical web framework, with call stacks hundreds of methods deep, you'll come to the same horrifying realisation that these are all inherently un-optimisable because of that statement. The compiler can do nearly nothing, and the processor will also struggle to paper over the inefficiency. Meanwhile, in reality, the specific implementations of those interfaces never change during actual production execution! Dynamic dispatch is utilised only during development time, and even then only at compile time to inject test harnesses, mocks, or whatever.
At runtime, 99.999999% of all function calls are essentially static, and dynamic dispatch is a pure waste.
- dzaima 5y agoExcept that a smart runtime can actually work with the assumption that only one implementation of a class will be used, and inline what would otherwise be virtual methods. And, as far as I know, the JVM definitely does this, and at multiple levels too - if there's only one implementation of an interface, it can just assume that everything's static and inlineable (at the cost of deoptimizations if that turns out to be false later). If not, it can still decide to inline, and just check the type beforehand.
- MaxBarraclough 5y ago> The compiler can do nearly nothing, Optimizing JIT-compiling JVMs are often able to optimize virtual calls that only ever resolve to one implementation. They may even inline such calls. (edit dzaima got there first.)
- duped 5y agoIt's old too, Self was doing this in the late 80s
- scns 5y agoIt was written by the same people: The Java HotSpot Performance Engine was released on April 27, 1999, built on technologies from an implementation of the programming language Smalltalk named Strongtalk, originally developed by Longview Technologies, which traded as Animorphic. The Longview virtual machine was based on the Self virtual machine, with an interpreter replacing the fast-and-dumb first compiler. When Sun cancelled the Self project, two key people, Urs Hölzle and Lars Bak left Sun to start Longview. In 1997, Sun Microsystems purchased Animorphic. Source: wikipedia
- andi999 5y agoYes and speaking of c++ the v table pointer might destroy your cache line.
- kaba0 5y agoJust as a note, one may get horrified at the cost of all these indirections, C code is full of function pointers passed as callbacks. Hell, actually there is no much difference between a vtable and typical C code doing the same thing. Chances are, C++ compiler writers made it more efficient. Also, c++ doesn’t default to virtual so you rarely pay the price.
- jasonwatkinspdx 5y agoThe only reason vtables et all work as well as they do is modern branch predictors are incredible.
- dzaima 5y agoWell, the alternative to vtables is manually branching (besides completely rewriting the code to avoid different actions), which isn't that much better and still requires branch prediction. And I think JVMs also might convert a dispatch from two classes to a conditional branch, but I'm not sure.
- nayuki 5y agoYes: https://shipilev.net/blog/2015/black-magic-method-dispatch/#_three_types_and_beyond https://shipilev.net/blog/2015/black-magic-method-dispatch/#...
- strictfp 5y agoYup,and this is excerbarated by the fact that c++ doesn't really offer any convenient equivalent of virtual methods with static dispatch. std::variant is almost it, but it's interface is quite horrendous as of now. Another interesting thing is that c++ also intermingles dynamic allocation with polymorphism, which can lead to a lot of unnecessary pointer chasing
- dkersten 5y agoIf you're ok with third party libraries, there are some solutions to static polymorphism. For example, EnTT comes with this: https://github.com/skypjack/entt/wiki/Crash-Course:-poly https://github.com/skypjack/entt/wiki/Crash-Course:-poly
- strictfp 5y agoInteresting! As by pure chance, my current project already incorporates EnTT, so I will definitely give this a closer look. Thank you.