Ruby's use of the OM and a new DSL
Chris Seaton
chris.seaton at oracle.com
Wed Sep 2 11:24:02 UTC 2015
Hello all,
JRuby+Truffle has been using the Truffle OM [1, 2] for language instance
variables since last year, via a DynamicObject within RubyBasicObject. This had
quite a bit of overhead as at the very least it means allocating two objects
instead of one.
We've now transitioned completely to the OM, so that we have just a
DynamicObject (there are no Ruby* classes anymore) and any internal
implementation fields (like the character array for a string) are now stored as
OM HiddenKey properties.
As long as your properties are static - created manually and not just as the
result of DynamicObject.set - this has no peak performance overhead. However
manually setting up that many properties would have been a great deal of work.
To make this easier we developed a new DSL for creating OM properties.
The idea is that you write an interface with getters and setters, and the DSL
will provide you a class that implements this interface, using the OM.
Then the main change is instead of doing
rubyString.getCharacters()
you now do
StringLayoutImpl.INSTANCE.getCharacters(rubyString)
Here are two examples of these interfaces - one simple and one more complicated:
* https://github.com/jruby/jruby/blob/6bb6fe6b4714da082e25f5ea159b5a16b9de2ffe/truffle/src/main/java/org/jruby/truffle/runtime/layouts/ArrayLayout.java
* https://github.com/jruby/jruby/blob/6bb6fe6b4714da082e25f5ea159b5a16b9de2ffe/truffle/src/main/java/org/jruby/truffle/runtime/layouts/ThreadLayout.java
And the generated code from these interfaces:
* https://gist.github.com/chrisseaton/79da5498ccf5613d3f1e
Features that the DSL provides:
* Getters, setters, guards
* Shape properties (useful for storing things like the guest-language class)
* @Nullable annotation (non-nullable by default)
* Implicit final if you omit a setter
* Lots of assertions to catch problems early
* Statically reference guest-language instance variables
* Zero peak overhead compared to regular Java fields
The DSL is currently part of the JRuby code base [3] and is not very polished,
but if there is interest from other people we can tidy it up and move it into
the Truffle API.
Transitioning to use the OM completely like this was admittedly quite a lot of
work, and another drawback is that all our objects are now just DynamicObject,
but we're happy with the result and some early increases in performance from it.
I would definitely recommend it to new Truffle languages.
Get in touch if you want to know more.
Chris
[1] A. Wöß, C. Wirth, D. Bonetta, C. Seaton, C. Humer, and H. Mössenböck, "An
object storage model for the Truffle language implementation framework,"
presented at the PPPJ '14: Proceedings of the 2014 International Conference
on Principles and Practices of Programming on the Java platform: Virtual
machines, Languages, and Tools, New York, New York, USA, 2014, pp. 133–144.
[2] http://lafo.ssw.uni-linz.ac.at/javadoc/truffle/all/
[3] https://github.com/jruby/jruby/tree/6bb6fe6b4714da082e25f5ea159b5a16b9de2ffe/truffle/src/main/java/org/jruby/truffle/om/dsl
More information about the graal-dev
mailing list