5 ms·
JITs don't have to repeat all their work every time they run. They can cache their output (this feature is planned for Java 9, I think). And while, as the artic
by pron 11y ago
JITs don't have to repeat all their work every time they run. They can cache their output (this feature is planned for Java 9, I think). And while, as the article says, JITs are pretty much a necessity for languages with dynamic dispatch, which are nearly impossible to optimize ahead-of-time, they can be great for statically-typed languages, too:
1. Their ability to speculatively optimize (and then de-optimize when the assumption proves false and recompile) makes it possible for them to implement zero-cost abstractions, such as inlining polymorphic virtual calls.
2. They make it possible to optimize across shared libraries, even those that are loaded dynamically.
To those interested in the future of JITs, I very much recommend watching one of the talks about Graal[1], HotSpot's (the OpenJDK JVM) next-gen JIT. Like HotSpot's current optimizing JIT compiler, it does speculative, profile-guided optimization, but exposes an API that lets the language designer (or even the programmer) to control optimization and code generation. It is also self-hosted (i.e. written in Java).
It's still under heavy development but early results are promising. Even though it supports multithreading (which complicates things), it performs better (often much better) than PyPy when running Python[2] and on par with V8 when running JavaScript[3].
[1]: https://wiki.openjdk.java.net/display/Graal/Publications+and+Presentations https://wiki.openjdk.java.net/display/Graal/Publications+and...
[2]: https://docs.google.com/spreadsheets/d/1fFMWcRIuPKt7wSAM5Ox9Rho4BBRhA5xgX6oemGhVxAA/edit#gid=1 https://docs.google.com/spreadsheets/d/1fFMWcRIuPKt7wSAM5Ox9...
[3]: http://www.slideshare.net/ThomasWuerthinger/jazoon2014-slides http://www.slideshare.net/ThomasWuerthinger/jazoon2014-slide...
- vidarh 11y ago> JITs are pretty much a necessity for languages with dynamic dispatch, which are nearly impossible to optimize ahead-of-time, Depends what you consider "nearly impossible". A lot of compilers for dynamic languages are just awful for no particularly good reason so it's often hard to assess what is slow because it is hard and what is slow because the implementation disregards the last 30 years of experience with compiling dynamic languages. E.g. I'm working on an ahead-of-time Ruby compiler. While it will need a JIT component for cases where people call eval, and while Ruby is particularly nasty to compile for a variety of reasons, the method dispatch is easy to reduce to one indirection via a vtable (you just need to propagate method overrides down the class hierarchy and update the vtables, but updates to the methods is much rarer than cals so it's ok for it to be more expensive), equivalent to C++ virtual member functions (though C++ compilers have better hope of being able to optimize away the virtual call). Despite the pathological cases possible because of the singly rooted object hierarchy, for most typical applications the wasted space in vtables (for method slots for methods that are unimplemented in a specific branch of the class hiearchy) is easily compensated for by e.g. needing less bookkeeping for methods that are known statically at compile time (which is the vast majority for most applications). If you're willing to compile a fallback path and suitable guards, you can sometimes optimize away many indirections entirely and even inline code ahead of time even for languages like Ruby (incidentally for example of that you can look at the work Chris Seaton has done on a JRuby Truffle/Graal backend - while that does these optimizations at runtime, many of them are applicable ahead of time too, though the JIT can get the advantage of not having to generate code for fallback cases unless they're actually needed at runtime) Note that I agree with you that JIT's have many advantages. At the same time, while I love the flexibility of dynamic languages, I prefer the smallest number of moving wheels possible in production environment. Spent too long doing devops.. Makes me lament the lack of attention to AOT/"as static as possible" compilers for dynamic languages.
- vardump 11y agoWhen you call/jump through a vtable, you've already lost inlining opportunities. Indirect branches themselves aren't free either. This is an order of magnitude performance loss for simple functions. Additional order of magnitude worse, if the simple function behind vtable indirect jump/call was a vectorization opportunity.
- vidarh 11y agoAgreed, which is why I also pointed out that with suitable guards and fallback path you can often inline as well, as large proportions of callsites in dynamic code is monomorphic or can be transformed to monomorphic by relatively small numbers of guards and split paths that can often be hoisted quite far. But a call/jump via vtable is already a massive improvement over most dynamic language implementations. It's not unusual still to find e.g. hash-table lookups and hierarchical searches up through the inheritance chain for no good reason.
- jerf 11y ago"E.g. I'm working on an ahead-of-time Ruby compiler.... easy..." If you haven't already... it's 2015. You might want to go look at the sun-bleached bones of all your predecessors who confidently proclaimed that all the dynamic scripting languages were slow for no good reason and a JIT could totally just JIT all the slowness away. If it's a journey you wish to take, by all means, be my guest, but, please, be sure you know what you're getting into. (The aforementioned sun-bleached bones are the bulk of the reason why I no longer believe in the old adage that "languages don't have performance characteristics, only implementations do", at least in the full sense intended. Languages may not have performance characteristics but the evidence rather strongly suggests they can put upper bounds on performance.)
- vidarh 11y agoYour contraction massively misrepresents what I wrote. Compiling Ruby is by no means easy. And optimizing it to similar speed as e.g. C is massively hard. But optimizing the dynamic dispatch down to the cost of a vtable call is trivially easy.
- ndesaulniers 11y agoIIRC, the Oracle guys came to Mozilla to give a talk about this. When Mozilla folks asked if it was open source, which at the time, the answer was "no." That response was met with a bunch of blank looks from both parties. Heh. At least, I think it was Graal. They were showing some VM running Ruby.
- pron 11y agoGraal is open source now: https://github.com/OracleLabs/GraalVM https://github.com/OracleLabs/GraalVM (I think it's now officially part of OpenJDK) And yes, it was probably Graal. There's an accompanying language JIT construction toolkit called Truffle, and they already have implementations for Java, Python, JavaScript, Ruby and R.