-
Notifications
You must be signed in to change notification settings - Fork 0
Truffle
The Truffle runtime of JRuby is an experimental implementation of an interpreter for JRuby using the Truffle AST interpreting framework and the Graal compiler. It’s an alternative to the IR interpreter and bytecode compiler. The goal is to be significantly faster, simpler and to have more functionality than other implementations of Ruby.
JRuby+Truffle is a project of Oracle Labs and academic collaborators at the Institut für Systemsoftware at Johannes Kepler University Linz.
Documentation for JRuby+Truffle is at http://lafo.ssw.uni-linz.ac.at/graalvm/jruby/doc/. This includes project outline, how to use Truffle and how to use Truffle-specific functionality.
This wiki page includes installation instructions, more informal status information and FAQs.
JRuby+Truffle comes as part of most configurations of JRuby - the notable exception is the standard Maven artefacts if you are embedding JRuby. The documentation explains how to use Truffle from within the source tree.
When JRuby is installed with Truffle, -X+T will which to Truffle mode.
Using rbenv and ruby-build you can install jruby-9.0.0.0+graal-dev which includes the GraalVM, so no separate configuration is needed.
$ rbenv install jruby-9.0.0.0+graal-dev
$ rbenv shell jruby-9.0.0.0+graal-dev
$ ruby -X+T -e 'puts Truffle.graal?'
true
We track completeness against the suite of specs formerly known as RubySpec. These generated reports show overall completeness and work outstanding. We support over 93% of the language and over 44% of the core library. Work on the standard library is still more limited.
What is Truffle?
Truffle is a Java framework for writing AST interpreters. To implement a language using Truffle you write an AST for your language and add methods to interpret - perform the action of - each node.
Truffle also has the concept of specialisation. In most AST interpreters the nodes are megamorphic - they handle all possible types and other possible conditions. In Truffle you write several different nodes for the same semantic action but for different types and conditions. As runtime conditions change, you switch which nodes you are using. After the program has warmed up you should end up with an AST that is precisely tailored for the types and conditions that you are actually using. If these conditions change, you can just switch nodes again.
For more information Truffle see the publications, API documentation, and FAQ.
What is Graal?
Graal is a new implementation of a JIT compiler in the OpenJDK Java Virtual Machine. Unlike the current compilers, Graal is written in Java, and exposes a Java API to the running program. This means that instead of emitting bytecode a JVM language can directly control the compiler. However this is complicated, so normally Truffle uses Graal on your behalf to partially evaluate your AST interpreter into machine code.
For more information about Graal see the publications and API documentation.
Why is the Truffle backend slow on a standard JVM?
When running on a standard JVM, Truffle runs about as fast as the JRuby AST interpreter. By default JRuby compiles to bytecode, which is faster than interpreting an AST, so you may not be used to the performance of a simple AST interpreter and it may seem unusually slow.
Eventually the expected way to run Truffle programs will be using the Graal compiler, so the performance on a standard JVM will not be as important.
Why is the Truffle backend faster on Graal?
When running on a VM with the Graal compiler, Truffle can use the API exposed by Graal. Truffle gets the IR representation of all of the AST interpreter methods involved in running your Ruby method, combines them into something like a single Java method, optimises them together, and emits a single machine code function. On Graal, Truffle also provides wrappers for JVM functionality not normally available to Java applications such as code deoptimization. The Truffle backend uses this to provide dramatically simpler and faster implementations of Ruby semantics compared to the current implementation in JRuby.
Where did this code come from?
Chris Seaton wrote an implementation of Ruby on Truffle and Graal as part of an internship at Oracle Labs in the first half of 2013. The code was merged into JRuby in early 2014. Benoit Daloze and Kevin Menard joined as researchers in the second half of 2014. Since then we have also accepted contributions from people outside Oracle Labs.
Who do I ask about the Truffle backend?
Ask about Truffle in the #jruby Freenode IRC room. We'll get notified if you mention truffle so your question won't be missed.
How do I know if I’m using a Graal VM?
Use -Xtruffle.printRuntime=true. This should print the name of the Truffle runtime that you are using. ‘Default’ means Truffle running as a normal Java library - which will be about as slow as the normal JRuby AST interpreter. ‘Graal’ means that you are using Graal VM to compile the Truffle interpreter to native code, and should be significantly faster.
Why doesn’t the Truffle backend work for my application or gem?
The Truffle backend implements almost all of the language but only about half the core library.
Why doesn’t the Truffle backend perform well for my benchmark?
Benchmarks that we haven’t looked at yet are likely to require new code paths to be specialized. Currently we’ve added specialisation for the code paths in the benchmarks and applications that we’ve been using. Adding them is generally not complicated and over time we will have specialisations to cover a broad range of applications.
Also, check that you are using a Graal VM (see above).
How is this related to invokedynamic?
The Truffle backend doesn’t use invokedynamic, as it doesn't emit bytecode. However it does have an optimising method dispatch mechanism that achieves a similar result - see the method dispatch nodes.
How is this related to the JRuby IR?
The Truffle backend and the JRuby IR are not related and take very different approaches. They are mutually exclusive backends - you can’t use both of them at the same time.