GraalVM, Truffle, SubstrateVM and Sulong
I’ve been reading a lot about Graal recently, and with a bunch of related projets, I needed to remind myself what are the differences and uses for each. Here’s a brief recap and intro to those projects. Most of the research and work is from Oracle and the JKU (Johannes Kepler University Linz).
It is implemented in Java, and splits its many features into different projects.
Graal is a native-code compiler, written in Java. The traditional HotSpot compilers are written in C++, and this blog post explains in easy to understand terms how it all works. It basically creates bytecode output (assembly machine code) from bytecode input (the JVM bytecode), so the thought was, why not write it in Java.
More than a simple compiler, it also runs a lot of sophisticated optimisations to produce high quality and speedy machine code. It is intended to someday replace HotSpot’s C2 compiler.
Truffle is a language interpretation framework. It basically helps writing languages into standardized abstract syntax trees (AST). This renders Truffle pretty much language agnostic, as we can implement an existing, or write a completemently new language using Truffle. This blog post and this one have additional details on its inner working.
The interesting part is Graal understands the interpretors implemented using Truffle, so all languages ported to Truffle can be run on the JVM, or even compiled ahead of time as a native application!
SubstrateVM is a JVM and ahead of time (AOT) compiler. As opposed to just in time (JIT) compilers, ahead of time compilation allows us to obtain an executable that does not depend on any JVM. Advantages are several: applications start faster, don’t require the JVM, and may use less memory. Disadvantages are loss of JVM optimisations (so peak performance could be lower than on the JVM), and no availability of Java tools like VisualVM.
Of interest, OpenJDK also includes an AOT compiler, which itself uses the Graal under the hood. SubstrateVM goes further, it is all implemented in Java and produces a statically linked binary, so no JVM is needed on the target machine, nor is included in the binary.
Sulong is an interpretor for LLVM bitcode implemented with Truffle. This is what allows GraalVM to support C, Fortran, Rust, and similar.
One VM To Rule Them All
GraalVM is a recent project, with a lot of fantastic development going on, and a lot of potential. It spawned several additional projects, such as TruffleRuby (now part of GraalVM), which has much higher performance than even JRuby or CRuby. This highlights how one could reimplement an existing language with Truffle, have it run as bytecode on the JVM, or even run as a native application through Graal compilation, with all the performance optimisations!
Twitter also reportedly uses GraalVM to run parts of its Scala code and improve CPU time.