- Clone JRuby 9k master
- Download http://lafo.ssw.uni-linz.ac.at/graalvm/openjdk-8-graalvm-b132-macosx-x86_64-0.7.tar.gz and untar into JRuby directory
- or http://lafo.ssw.uni-linz.ac.at/graalvm/openjdk-8-graalvm-b132-linux-x86_64-0.7.tar.gz
$ function jt { ruby tool/jt.rb $@; }
$ jt build
$ jt run --graal test/truffle/can-we-fold-yet.rb
$ jt run --graal test/truffle/can-we-fold-yet.rb test/truffle/can-we-fold-yet.rb
Can Truffle constant fold yet?
> 14
Yes! Truffle can constant fold this to 14
> 14 + 2
Yes! Truffle can constant fold this to 16
> eval([1, 2, 3].inspect).sort[1] * 2
Yes! Truffle can constant fold this to 4
> eval(rand < 0.5 ? '14 - 2' : '10 + 2')
Yes! Truffle can constant fold this to 12
> eval('rand')
No :( Truffle can't constant fold that
Some more examples:
Proc#binding
returns an object representing the environment captured in a block - even if the variables aren't actually lexically referenced. People have said it makes it 'impossible' to optimise local variables like thex
in this example http://www.sitepoint.com/laurent-sansonetti-on-rubymotion-internals/. This example proves that wrong:We can merge equal results from two control flow paths, even if we don't know which path is actually taken:
We can constant fold through Hash creation and lookup:
We can constant fold through higher order functions like
map
:We can constant fold through metaprogramming, even if the method that we call is not determinable statically:
Metadata can also be constant folded:
Some examples of things that don't work for balance: