
Improving TruffleRuby Performance - nirvdrum
http://nirvdrum.com/2017/05/30/a-systematic-approach-to-improving-truffleruby-performance.html
======
bostise
TruffleRuby looks really promising for those who like Ruby but would also like
better performance.

Whats the easiest way to install it these days ?

Last time I thought of installing it, it required signing up for an account on
the Oracle website simply to be allowed to download it, at which point I
abandoned my attempt.

~~~
thibaut_barrere
I think the easiest way so far is still the Oracle website:

[http://www.oracle.com/technetwork/oracle-labs/program-
langua...](http://www.oracle.com/technetwork/oracle-labs/program-
languages/downloads/index.html)

It has release 0.23 from a few days ago.

I think the home-brew recipe is not so recent (but may be wrong here).

------
jerven
I am always impressed by the IGV of graal/truffle to see how code is compiled.
Really looking forward to JVM-9/JVMCI general availability so it becomes
easier to play with truffle.

------
misterbowfinger
I'm kind of confused about the difference between TruffleRuby and JRuby. Are
they... using different VMs? If anyone can point to a blog post explaining the
difference, that'd be helpful. I'm mainly a Ruby dev and know little about the
JVM or GraalVM.

Aaaaalso, how does this compare to Eclipse Ruby+OMR?

[https://github.com/rubyomr-preview/ruby](https://github.com/rubyomr-
preview/ruby)

~~~
nirvdrum
TruffleRuby and JRuby are two different projects. The history is a bit
confusing. TruffleRuby was originally its own project internal to Oracle Labs.
It was open-sourced and merged into JRuby as an alternative runtime, called
JRuby+Truffle, and shipped with the JRuby 9K releases, activated by a special
flag. As JRuby and JRuby+Truffle both kept evolving, it really became two
separate projects within the same repository. So, at the end of 2016 we rolled
JRuby+Truffle back out of JRuby into its own project, now called TruffleRuby.

Both JRuby & TruffleRuby are written in Java and target the JVM. JRuby now has
its own IR for performing Ruby-level optimizations and it targets the JVM's
invokedynamic system. TruffleRuby uses the Truffle self-optimizing AST
framework [1] to implement its runtime and relies on integration with the
Graal compiler to produce fast code via partial evaluation. Graal is a new
compiler for the JVM written in Java. However, you need a JVMCI-enabled JVM to
activate Graal. JVMCI is a new feature in Java 9, but we have a backport
package available for Java 8 on the Oracle Technology Network (OTN) [3].

In addition, we ship a package with multiple Truffle languages and a Graal-
enabled JVM in a package we call GraalVM. You can download that from OTN as
well. That package also ships with a builder for the Substrate VM, which
allows you to build an ahead-of-time compiled static binary of the various
Truffle languages [4].

So, both JRuby and TruffleRuby target the JVM but they currently use different
JITs. There's nothing stopping JRuby from using Graal, and indeed they've been
looking at it, but they likely will continue using invokedynamic rather than
Truffle.

Eclipse Ruby+OMR is a completely different project looking to take some VM
components IBM has developed and integrate them with MRI. Since they're
actually running MRI rather than a completely new project, they have 100%
compatibility with Ruby, but they also have to operate within the confines of
MRI's internal design.

[1] --
[https://github.com/graalvm/graal/tree/master/truffle](https://github.com/graalvm/graal/tree/master/truffle)

[2] --
[https://github.com/graalvm/graal/tree/master/compiler](https://github.com/graalvm/graal/tree/master/compiler)

[3] -- [http://www.oracle.com/technetwork/oracle-labs/program-
langua...](http://www.oracle.com/technetwork/oracle-labs/program-
languages/downloads/index.html)

[4] -- [http://nirvdrum.com/2017/02/15/truffleruby-on-the-
substrate-...](http://nirvdrum.com/2017/02/15/truffleruby-on-the-substrate-
vm.html)

~~~
ksec
Thx, does anyone know what is happening on the OMR front? Since it has been
very quiet ever since it was first announced.

Also wondering, for Truffle / GraalVM / SubstrateVM, how far are we from
testing in production? I keep thinking TruffleRuby is Nightly / Beta and if i
remember correctly, does not run Rails ( yet ) ?

~~~
nirvdrum
> Thx, does anyone know what is happening on the OMR front? Since it has been
> very quiet ever since it was first announced.

I don't know if there's a more official channel, but I follow Matthew Gaudet's
tweets (@MattStudies). He provides periodic updates and provides links to
talks and such.

> Also wondering, for Truffle / GraalVM / SubstrateVM, how far are we from
> testing in production? I keep thinking TruffleRuby is Nightly / Beta and if
> i remember correctly, does not run Rails ( yet ) ?

We're not at the point where we recommend anyone run it in production. We do
actually run a sizable portion of Rails, but since the DB adapters are all C
extensions we can't really run those yet, which practically means we can't run
Rails. The distinction is we're able to run with things like the pure Ruby
PostgreSQL gem (postgres-pr) and the pure Redis gem (redis).

We're making some very good progress on our C extension support. We've just
recently made a successful HTTPS request running MRI's OpenSSL extension by
way of Sulong [1], which is an LLVM bitcode interpreter for Graal.

If you're interested in casually following what we're up to, we now have a
Twitter handle (@TruffleRuby). Or, you could always watch the GitHub project
[2], since it is open source.

[1] -- [https://github.com/graalvm/sulong](https://github.com/graalvm/sulong)

[2] --
[https://github.com/graalvm/truffleruby](https://github.com/graalvm/truffleruby)

------
themgt
Not knowing the internals of Array#size implementation / Array data structure,
but my first thought would be whether Array#empty? could just check for the
presence of at least one element (i.e. stop at the first), rather than
checking whether the size of a large array is equal to zero.

~~~
nirvdrum
While there's no requirement that a Ruby Array is implemented as an array in
the implementation language (C, C++, Java, etc.), in practice it is in all the
Ruby implementations. In order to traverse an array, you need to know the
length so you can ensure your array index is in bounds. I.e., to read the
first element of an array, you'll need to read the size anyway. That being the
case, you would want to compare the size to 0 because comparing ints is much
faster than reading from memory.

