5 ms·
Java uses table dispatch by default, but you can opt into direct dispatch by using the final keyword. C++ uses direct dispatch by default, but you can opt i
by CalChris 10y ago
Java uses table dispatch by default, but you can opt into
direct dispatch by using the final keyword. C++ uses direct
dispatch by default, but you can opt into table dispatch by
adding the virtual keyword. Objective-C always uses message
dispatch, but allows developers to fall back to C in order
to get the performance gains of direct dispatch. Swift has
taken on the noble goal of supporting all three types of
dispatch.
I kinda already knew this but it's nice and succinct.
- corysama 10y agoFor the under-the-hood details of message dispatch, see "An Illustrated History of objc_msgSend" http://sealiesoftware.com/msg/index.html http://sealiesoftware.com/msg/index.html
- jdmichal 10y agoJust for completeness, I believe C# fits with C++, which is something that can occasionally catch a Java developer.
- pjmlp 10y agoC# is actually a mix. It uses static dispatch by default, virtual dispatch like C++ and Object Pascal, dynamic dispatch like Objective-C.
- noblethrasher 10y agoInterestingly, while the C# language uses static dispatch by default, it is at least the case that Microsoft's compiler always emits the `callvirt` instruction[1]. This is because the language spec requires that a method call to a null instance of a reference type must throw a NullReferenceException, and callvirt is apparently the cheapest way to do that on the CLR. [1] https://blogs.msdn.microsoft.com/ericgu/2008/07/02/why-does-c-always-use-callvirt/ https://blogs.msdn.microsoft.com/ericgu/2008/07/02/why-does-... [2] §6.1 of the C# language spec.
- KingOfBrian 10y agoI've only used C# very topically. Do you have any documentation that describes how it behaves?
- pjmlp 10y agoNot sure, I guess this video could be interesting: https://www.microsoft.com/resources/msdn/en-us/msdntv/20051103CLRJP.html https://www.microsoft.com/resources/msdn/en-us/msdntv/200511... For dynamic invocations, Objective-C style, you can see here: https://msdn.microsoft.com/en-us/library/dd264741.aspx https://msdn.microsoft.com/en-us/library/dd264741.aspx https://msdn.microsoft.com/en-us/library/ee461504.aspx#Anchor_1 https://msdn.microsoft.com/en-us/library/ee461504.aspx#Ancho...
- chrisseaton 10y ago> but you can opt into direct dispatch [in Java] by using the final keyword Most Java JIT compilers will also opt you into direct dispatch automatically where possible, for example a class with no subclasses or an interface with only one implementation. They'll also opt you into direct dispatch per call site if a given call site has only ever seen objects of a single class.
- KingOfBrian 10y agoThe other cool think about running in a JVM is those optimizations (I believe) are done at run-time -- a luxury that swift doesn't have.
- pjmlp 10y agoSwift can also do it. This is what is called PGO, profile guided optimization. Basically you do several test runs using real use cases, under a profiler and then do a release build using the PGO gathered data as additional input.
- chrisseaton 10y agoYes they have to be done at runtime. In both cases the optimisation could become invalid if you load more classes, so the optimisation also needs to be reversible at runtime. This is an example of how it possible for a JIT to out-perform a static compiler.
- schrodinger 10y agoIt's so hard to read those horizontally-scrolling fixed-width text blurbs on mobile!!!
- TazeTSchnitzel 10y agoHacker News really needs to add 1) blockquotes and 2) lists. If it had these, most abuses of monospace preformatted text would disappear.
- jcrites 10y ago> Most languages support one or two of these. Java uses table dispatch by default, but you can opt into direct dispatch by using the final keyword. This is not accurate. Java does not use table dispatch by default. The HotSpot compiler will optimize most code to use direct dispatch, reserving table dispatch only for cases where calls cannot be inlined; and final does not particularly enable direct dispatch. A discussion of these issues: http://www.ibm.com/developerworks/java/library/j-jtp1029/index.html http://www.ibm.com/developerworks/java/library/j-jtp1029/ind... > Like many myths about Java performance, the erroneous belief that declaring classes or methods as final results in better performance is widely held but rarely examined. The argument goes that declaring a method or class as final means that the compiler can inline method calls more aggressively, because it knows that at run time this is definitely the version of the method that's going to be called. But this is simply not true. Just because class X is compiled against final class Y doesn't mean that the same version of class Y will be loaded at run time. So the compiler cannot inline such cross-class method calls safely, final or not. Only if a method is private can the compiler inline it freely, and in that case, the final keyword would be redundant. > On the other hand, the run-time environment and JIT compiler have more information about what classes are actually loaded, and can make much better optimization decisions than the compiler can. If the run-time environment knows that no classes are loaded that extend Y, then it can safely inline calls to methods of Y, regardless of whether Y is final (as long as it can invalidate such JIT-compiled code if a subclass of Y is later loaded). So the reality is that while final might be a useful hint to a dumb run-time optimizer that doesn't perform any global dependency analysis, its use doesn't actually enable very many compile-time optimizations, and is not needed by a smart JIT to perform run-time optimizations.
- drtse4 10y ago> Like many myths about Java performance, the erroneous belief that declaring classes or methods as final results in better performance is widely held but rarely examined. Yes, was about to point this out, one of the more resilient misconceptions about Java. I've lost count of how many times I had to explain this and remove a boatload of unnecessary "final".
- KingOfBrian 10y ago