4 ms·
Apple's engineers are right on this one. From a performance point of view, the only way Java (and dynamic languages like JavaScript) get away with having almost
by pcwalton 11y ago
Apple's engineers are right on this one. From a performance point of view, the only way Java (and dynamic languages like JavaScript) get away with having almost everything be virtual is that they can JIT under a whole-program analysis, add final automatically in the absence of subclassing, and deoptimize in the presence of classloaders. In other words, Java optimistically compiles under the assumption that classes that aren't initially subclassed won't be, and if that assumption later turns out to be invalid it deletes the now-incorrect JIT code and recompiles. This isn't an option for Swift, which is ahead-of-time compiled and can't recompile on the fly.
If you don't allow an ahead of time compiler to devirtualize anything, you're going to have worse method call performance than JavaScript.
- coldtea 11y ago>If you don't allow an ahead of time compiler to devirtualize anything, you're going to have worse method call performance than JavaScript. Well, Swift as it is (e.g. before adopting final by default) has great performance. So what gives?
- pcwalton 11y agoPerformance isn't one dimensional. Maybe the benchmarks you're looking at aren't gated on method call performance. Maybe the authors of whatever benchmark you're looking at wrote final manually. Maybe they used freestanding functions instead of methods. Maybe your benchmarks aren't calling functions at all. Who knows? The point is that anyone who has worked on compilers for OO languages will tell you the same thing about devirtualization. This stuff goes back to Smalltalk implementations in the 80s.
- mpweiher 11y ago> Maybe the benchmarks you're looking at aren't gated on method call performance. Most real world programs aren't gated on method call performance. And things have changed since the 80ies. Heck, even in the 90ies, Squeak Smalltalk was plenty fast for media work using a plain byte-code interpreter (no JIT), because the heavy lifting was done in primitives. Just like Tcl was used in supercomputing.
- hyperpape 11y agoI immediately agreed with what you wrote, but now there's a voice in the back of my head asking "if this is so obviously right, why is it just happening now, after 2.0? It's not like this Lattner guy doesn't know what method calls cost..." A post last week featured Lattner writing about Swift treating methods as final when they weren't declared that way (https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151207/001948.html https://lists.swift.org/pipermail/swift-evolution/Week-of-Mo...). I wonder if the early thought was that they'd get enough mileage out of those "cheats", and then later decided that final should be the default.
- pcwalton 11y agoProbably. It's hard to predict how language design decisions will impact performance (and everything else, such as ease of use and productivity). That's why it's good to have an iterative process (like Swift is now adopting) instead of being rigid, inflexible, and opinionated.
- dilap 11y agoYet Obj-C seems to get by quite fine, and everything is super-dynamic there. Being able to dynamically hack behavior is a huge boon when coding against a closed-source, 3rd party platform. Doesn't matter so much in an open-source context, where you can just modify code and ship your own version of the library if necessary.
- pcwalton 11y agoDepends what you mean by "just fine". Objective-C method dispatch is very slow. It's significantly slower than that of JavaScript, for example.
- Alphasite_ 11y agoThey aren't are they? Cached method calls are fairly fast from what i've read. [1] https://www.mikeash.com/pyblog/performance-comparisons-of-common-operations-leopard-edition.html https://www.mikeash.com/pyblog/performance-comparisons-of-co...
- pcwalton 11y agoAccording to that chart, Obj-C method calls are either 4.5x or 7.8x slower than C++ virtual method calls, depending on whether you're looking at 32- or 64-bit code. That alone is pretty bad. But it's even worse than that: I'm talking about devirtualized calls, which are far faster than virtual calls in C++. More importantly than just the call time, devirtualization can have outsized performance impact because it enables inlining, converting intraprocedural optimizations to interprocedural optimizations. The difference between inlining and not can easily be a 10x-level performance difference with an optimizing compiler like LLVM.
- mpweiher 11y agoThe chart is wrong for message sends. I've been measuring this for a long time, and get ~2.1ns for a message send vs. 1.2ns for a cached message send on a comparable machine. If you want inlining, use a static inline function.