TruffleRuby

TruffleRuby is an implementation of the Ruby programming language that aims to:

  • Run idiomatic Ruby code faster
  • Run Ruby code in parallel
  • Boot Ruby applications in less time
  • Execute C extensions in a managed environment
  • Add fast and low-overhead interoperability with languages like Java, JavaScript, Python and R
  • Provide new tooling such as debuggers and monitoring
  • All while maintaining very high compatibility with the standard implementation of Ruby

If you are attempting to experiment with deploying TruffleRuby to production we would encourage you to contact us so we can help you understand what is possible at the moment and to help solve any issues for you.

TruffleRuby repository

The TruffleRuby project is open source and located on GitHub: oracle/truffleruby. If you encounter any issues or want to leave feedback or feature requests please create an issue in this repository.

Installing TruffleRuby

TruffleRuby can be installed to a GraalVM build with the gu command. See graalvm/bin/gu --help for more information.

Running Ruby applications

Run Ruby applications with the standard ruby command:

$ ruby [options] program.rb

Other Ruby commands are available, including gem, irb, rake, rdoc, and ri.

TruffleRuby uses the same options as the standard implementation of Ruby with some additions.

Installing a Ruby gem

$ gem install chunky_png
$ ruby -r chunky_png -e "puts ChunkyPNG::Color.to_hex(ChunkyPNG::Color('mintcream @ 0.5'))"
#f5fffa80

What does TruffleRuby do?

Run idiomatic Ruby code faster

TruffleRuby in conjunction with the GraalVM produces very fast machine code. Traditionally, performance-critical Ruby code has had to be extracted out to language extensions implemented in native code. This approach is cumbersome and error-prone. TruffleRuby on the GraalVM is able to optimize Ruby code. More importantly, it can optimize idiomatic Ruby code.

The following example template rendering benchmark is taken from the MRI benchmark suite. It has been modified to work with the benchmark-ips benchmarking harness, and to write the result of the render to a file in order to include IO.

require 'benchmark/ips'
require 'erb'

data = <<erb
<html>
<head> <%= title %> </head>
<body>
  <h1> <%= title %> </h1>
  <p>
    <%= content %>
  </p>
</body>
</html>
erb

title = 'hello world!'
content = "hello world!\n" * 10
erb = ERB.new(data)
out = File.open('test.out', 'w')

Benchmark.ips do |x|
  x.iterations = 3
  x.report("erb") { out.write(erb.result(binding)) }
end
$ gem install benchmark-ips
$ ruby benchmark.rb
MRI 2.5.3 JRuby 9.2.5.0 TruffleRuby
CE native
TruffleRuby
EE JVM
34 K renders/s 29 K renders/s 542 K renders/s
(16x faster)
1 M renders/s
(31x faster)

Run Ruby code in parallel

TruffleRuby runs threads in parallel, without a global-interpreter-lock. It includes sophisticated techniques to automatically make objects thread-safe only when they are going to be used by more than one thread, leaving them unsynchronized when used from only one thread to give you parallelism without an overhead when you don’t want to use it.

Boot Ruby applications in less time

A hello-world script in TruffleRuby runs about as fast as MRI, or over ten times faster than JRuby. TruffleRuby can run without a heavy-weight JVM so that you start running your Ruby code almost immediately.

Execute C extensions in a managed environment

TruffleRuby executes C extensions using a managed C interpreter and just-in-time compiler, not by running native code.

Provide new tooling

TruffleRuby includes a built-in debugger that allows you to use Chrome DevTools on your Ruby application.

$ ruby --inspect test.rb
Debugger listening on port 9229.
To start debugging, open the following URL in Chrome:
    chrome-devtools://devtools/bundled/inspector.html?ws=127.0.0.1:9229/ffffffffffffffff-fffffffffffffffff

Add fast and low-overhead interoperability

TruffleRuby allows you to write polyglot programs using the other languages from GraalVM.

High compatibility with the standard implementation of Ruby

TruffleRuby aims to be fully compatible with the standard implementation of Ruby, MRI, version 2.4.4, revision 63013. We test extensively using the MRI test suite and the Ruby Spec project.

Interoperability

GraalVM supports several other programming languages, including JavaScript, R, Python, and LLVM. TruffleRuby lets you run code from these languages, and use objects and methods from one language in another.

As an example, we can pass a JSON-encoded string into Graal.js, decode it into a JavaScript object, and then inspect the decoded object in Ruby:

require 'json'

obj = {
  time: Time.now,
  msg: 'Hello World',
  payload: (1..10).to_a
}

encoded = JSON.dump(obj)

js_obj = Polyglot.eval('js', 'JSON.parse').call(encoded)

puts js_obj[:time]
puts js_obj[:msg]
puts js_obj[:payload].join(' ')

We use the --jvm --polyglot option to say we want to run a polyglot program, and -Xsingle_threaded because JavaScript is only compatible with single threaded programs.

$ ruby --jvm --polyglot -Xsingle_threaded test.rb
2018-04-11 15:25:08 -0700
Hello World
1 2 3 4 5 6 7 8 9 10

Compatibility

TruffleRuby aims to be fully compatible with the standard implementation of Ruby, MRI, version 2.4.4, revision 63013.

Any incompatibility with MRI is considered a bug, except for rare cases detailed below. If you find an incompatibility with MRI, please report it to us.

Our policy is to match the behaviour of MRI, except where we do not know how to do so with good performance for typical Ruby programs. Some features work but will have very low performance whenever they are used and we advise against using them on TruffleRuby if you can. Some features are missing entirely and may never be implemented. In a few limited cases, we are deliberately incompatible with MRI in order to provide a greater capability.

In general, we are not looking to debate whether Ruby features are good, bad, or if we could design the language better. If we can support a feature, we will do.

In the future, we aim to provide compatibility with extra functionality provided by JRuby, but at the moment we do not.

Identification

TruffleReport defines these constants for identification:

  • RUBY_ENGINE is 'truffleruby'
  • RUBY_VERSION is the compatible MRI version
  • RUBY_REVISION is the compatible MRI version revision
  • RUBY_PATCHLEVEL is always zero
  • RUBY_RELEASE_DATE is the Git commit date
  • RUBY_ENGINE_VERSION is the GraalVM version, or 0.0- and the Git commit hash if your build is not part of a GraalVM release.

Additionally, TruffleRuby defines:

  • TruffleRuby.revision which is the Git commit hash

In the C API, we define a preprocessor macro TRUFFLERUBY.

Features entirely missing

Continuations and callcc

Continuations and callcc are unlikely to ever be implemented in TruffleRuby, as their semantics fundamentally do not match the technology that we are using.

Fork

You cannot fork the TruffleRuby interpreter. The feature is unlikely to ever be supported when running on the JVM but could be supported in the future on the SVM. The correct and portable way to test if fork is available is:

Process.respond_to?(:fork)

Standard libraries

win32ole is unsupported.

Quite a few of the less commonly used standard libraries are currently not supported, such as fiddle, sdbm, gdbm, tk. It’s quite hard to get an understanding of all the standard libraries that should be available, so it’s hard to give a definitive list of those that are missing.

Safe levels

$SAFE and Thread#safe_level are 0 and no other levels are implemented. Trying to use level 1 will raise a SecurityError. Other levels will raise ArgumentError as in standard Ruby.

$SAFE level 1 can be allowed, but ignored, with the -Xsafe option.

Features with major differences

Threads run in parallel

In MRI, threads are scheduled concurrently but not in parallel. In TruffleRuby threads are scheduled in parallel. As in JRuby and Rubinius, you are responsible for correctly synchronising access to your own shared mutable data structures, and we will be responsible for correctly synchronising the state of the interpreter.

Fibers do not have the same performance characteristics as in MRI

Most use cases of fibers rely on them being easy and cheap to start up and having low memory overheads. In TruffleRuby we implement fibers using operating system threads, so they have the same performance characteristics as Ruby threads. As with coroutines and continuations, a conventional implementation of fibers fundamentally isn’t compatible with the execution model we are currently using.

Some classes marked as internal will be different

MRI provides some classes that are described in the documentation as being only available on MRI (C Ruby). We implement these classes if it’s practical to do so, but this isn’t always the case. For example RubyVM is not available.

Features with subtle differences

Command line switches

-y, --yydebug, --dump= are ignored with a warning as they are internal development tools.

-X is an undocumented synonym for -C and we (and other alternative implementations of Ruby) have repurposed it for extended options. We warn if your -X options look like they are actually intended to be as in MRI.

Programs passed in -e arguments with magic-comments must have an encoding that is UTF-8 or a subset of UTF-8, as the JVM has already decoded arguments by the time we get them.

Setting the process title doesn’t always work

Setting the process title (via $0 or Process.setproctitle in Ruby) is done as best-effort. It may not work, or the title you try to set may be truncated.

Line numbers other than 1 work differently

In an eval where a custom line number can be specified, line numbers below 1 are treated as 1, and line numbers above 1 are implemented by inserting blank lines in front of the source before parsing it.

The erb standard library has been modified to not use negative line numbers.

Polyglot standard IO streams

If you use standard IO streams provided by the Polyglot engine, via the -Xpolyglot.stdio option, reads and writes to file descriptors 1, 2 and 3 will be redirected to these streams. That means that other IO operations on these file descriptors, such as isatty may not be relevant for where these streams actually end up, and operations like dup may lose the connection to the polyglot stream. For example, if you $stdout.reopen, as some logging frameworks do, you will get the native standard-out, not the polyglot out.

Also, IO buffer drains, writes on IO objects with sync set, and write_nonblock, will not retry the write on EAGAIN and EWOULDBLOCK, as the streams do not provide a way to detect this.

Error messages

Error message strings will sometimes differ from MRI, as these are not generally covered by the Ruby Specification suite or tests.

Features with very low performance

ObjectSpace

Using most methods on ObjectSpace will temporarily lower the performance of your program. Using them in test cases and other similar ‘offline’ operations is fine, but you probably don’t want to use them in the inner loop of your production application.

set_trace_func

Using set_trace_func will temporarily lower the performance of your program. As with ObjectSpace, we would recommend that you do not use this in the inner loop of your production application.

Backtraces

Throwing exceptions, and other operations which need to create a backtrace, are slower than on MRI. This is because we have to undo optimizations that we have applied to run your Ruby code fast in order to recreate the backtrace entries. We wouldn’t recommend using exceptions for control flow on any implementation of Ruby anyway.

To help alleviate this problem in some cases backtraces are automatically disabled where we dynamically detect that they probably won’t be used. See the -Xbacktraces.omit_unused option.

C Extension Compatibility

VALUE is a pointer

In TruffleRuby VALUE is a pointer type (void *) rather than a integer type (long). This means that switch statements cannot be done using a raw VALUE as they can with MRI. You can normally replace any switch statement with if statements with little difficulty if required.

Identifiers may be macros or functions

Identifiers which are normally macros may be functions, functions may be macros, and global variables may be macros. This may cause problems where they are used in a context which relies on a particular implementation (e.g., taking the address of it, assigning to a function pointer variable and using defined() to check if a macro exists). These issues should all be considered bugs and be fixed, please report these cases.

rb_scan_args

rb_scan_args only supports up to ten pointers.

RDATA

The mark function of RDATA and RTYPEDDATA is not called during garbage collection. Instead we simulate this by caching information about objects as they are assigned to structs, and periodically run all mark functions when the cache has become full to represent those object relationships in a way that the our garbage collector will understand. The process should behave identically to MRI.

Compatibility with JRuby

Ruby to Java interop

JRuby’s Java interop API is not implemented far enough to be used. We provide an alternate polyglot API for interoperating with multiple languages, including Java, instead.

Java to Ruby interop

Calling Ruby code from Java is supported by the Graal-SDK polyglot API.

Java extensions

Use Java extensions written for JRuby is not supported. We could apply the same techniques as we have developed to run C extensions to this problem, but it’s not clear if this will ever be a priority.

Compatibility with Rubinius

We do not have any plans at the moment to provide support for Rubinius’ extensions to Ruby.

Features not yet supported in native configuration

  • Java interop

Running TruffleRuby in the native configuration is mostly the same as running on the JVM. There are differences in resource management, as both VMs use different garbage collectors. But, functionality-wise, they are essentially on par with one another. The big difference is support for Java interop, which currently relies on reflection. TruffleRuby’s implementation of Java interop does not work with the SVM’s limited support for runtime reflection.

Spec Completeness

‘How many specs are there’ is not a question with an easy precise answer. The three numbers listed below for each part of the specs are the number of expectations that the version of MRI we are compatible with passes, then the number TruffleRuby passes, and then the TruffleRuby number as a percentage of the MRI number. This is run on macOS. The numbers probably vary a little based on platform and configuration. The library and C extension specs are quite limited so may be misleading.

  • Language: 3913, 3903, 99%
  • Core: 176111, 169117, 96%
  • Library (:library and :openssl on TruffleRuby): 20820, 16934, 81%
  • C extensions: 1679, 1627, 97%

Compatibility with gems

You can use the compatibility checker to find whether the gems you’re interested in are tested on GraalVM, whether the tests pass successfully and so on. Additionally, you can drop your Gemfile.lock file into that tool and it’ll analyze all the gems you’re using at once. Note that the processing is done on the client-side, so no information is uploaded to any servers.

Options

TruffleRuby has the same command line interface as MRI 2.4.4.

Usage: truffleruby [switches] [--] [programfile] [arguments]
  -0[octal]       specify record separator (\0, if no argument)
  -a              autosplit mode with -n or -p (splits $_ into $F)
  -c              check syntax only
  -Cdirectory     cd to directory before executing your script
  -d, --debug     set debugging flags (set $DEBUG to true)
  -e 'command'    one line of script. Several -e's allowed. Omit [programfile]
  -Eex[:in], --encoding=ex[:in]
                  specify the default external and internal character encodings
  -Fpattern       split() pattern for autosplit (-a)
  -i[extension]   edit ARGV files in place (make backup if extension supplied)
  -Idirectory     specify $LOAD_PATH directory (may be used more than once)
  -l              enable line ending processing
  -n              assume 'while gets(); ... end' loop around your script
  -p              assume loop like -n but print line also like sed
  -rlibrary       require the library before executing your script
  -s              enable some switch parsing for switches after script name
  -S              look for the script using PATH environment variable
  -T[level=1]     turn on tainting checks
  -v, --verbose   print version number, then turn on verbose mode
  -w              turn warnings on for your script
  -W[level=2]     set warning level; 0=silence, 1=medium, 2=verbose
  -x[directory]   strip off text before #!ruby line and perhaps cd to directory
  --copyright     print the copyright
  --enable=feature[,...], --disable=feature[,...]
                  enable or disable features
  --external-encoding=encoding, --internal-encoding=encoding
                  specify the default external or internal character encoding
  --version       print the version
  --help          show this message, -h for short message

Features:
  gems            rubygems (default: enabled)
  did_you_mean    did_you_mean (default: enabled)
  rubyopt         RUBYOPT environment variable (default: enabled)
  frozen-string-literal
                  freeze all string literals (default: disabled)

TruffleRuby also reads the RUBYOPT environment variable, if run from the Ruby launcher.

Unlisted Ruby switches

MRI has some extra Ruby switches which are aren’t normally listed in help output but are documented in the Ruby manual page.

  -U              set the internal encoding to UTF-8
  -KEeSsUuNnAa    sets the source and external encoding
  --encoding=external[:internal]
                  the same as --external-encoding=external and optionally --internal-encoding=internal
  -y, --ydebug    debug the parser
  -Xdirectory     the same as -Cdirectory
  --dump=insns    print disassembled instructions

TruffleRuby-specific switches

Beyond the standard Ruby command line switches we support some additional switches specific to TruffleRuby.

TruffleRuby:
  -Xlog=SEVERE,WARNING,INFO,CONFIG,FINE,FINER,FINEST
                  set the TruffleRuby logging level
  -Xoptions       print available TruffleRuby options
  -Xname=value    set a TruffleRuby option (omit value to set to true)
  -J-option=value Translates to --jvm.option=value

As well as being set at the command line, options, except for log, can be set using --ruby.option=. For example --ruby.cexts.remap=true. They can also be set as JVM system properties, where they have a prefix polyglot.ruby.. For example --jvm.Dpolyglot.ruby.cexts.remap=true, or via any other way of setting JVM system properties. Finally, options can be set as Graal-SDK polyglot API configuration options.

The priority for options is the command line first, then the Graal-SDK polyglot API configuration, then system properties last.

The logging level is not a TruffleRuby option like the others and so cannot be set with a JVM system property. This is because the logger is once per VM, rather than once per TruffleRuby instance, and is used to report problems loading the TruffleRuby instance before options are loaded.

TruffleRuby-specific options, as well as conventional Ruby options, can also bet set in the TRUFFLERUBYOPT environment variable, if run from the Ruby launcher.

-- or the first non-option argument both stop processing of Truffle-specific arguments in the same way it stops processing of Ruby arguments.

JVM- and SVM-specific switches

If you are running TruffleRuby on a JVM or the GraalVM, we additionally support passing options to the JVM using either a -J- or --jvm. prefix. For example -J-ea. -J-classpath and -J-cp also implicitly take the following argument to be passed to the JVM. -J-cmd print the Java command that will be executed, for debugging.

  --jvm.[option]  Pass options to the JVM; for example, '--jvm.classpath=myapp.jar'. To see available options. use '--jvm.help'.

-- or the first non-option argument both stop processing of JVM-specific arguments in the same way it stops processing of Ruby arguments.

TruffleRuby also supports the JAVA_HOME, JAVACMD and JAVA_OPTS environment variables when running on a JVM (except for JAVACMD on the GraalVM).

SVM-specific switches

The SVM supports --native.D for setting system properties and --native.XX:arg for SVM options.

  --native.[option]  Pass options to the native image. To see available options, use '--native.help'.

Determining the TruffleRuby home

TruffleRuby needs to know where to locate files such as the standard library. These are stored in the TruffleRuby home directory.

The search priority for finding Ruby home is:

  • The value of the TruffleRuby home option (i.e., -Xhome=path/to/truffleruby_home).
  • The home that the Truffle framework reports.
  • The parent of the directory containing the Ruby launcher executable.

If the home option is set, it’s used even if it doesn’t appear to be a correct home location. Other options are tried until one is found that appears to be a correct home location. If none appears to be correct a warning will be given but the program will continue and you will not be able to require standard libraries. You can tell TruffleRuby not to try to find a home at all using the no_home_provided option.

Operations Manual

If you’re attempting to experiment with deploying TruffleRuby to production we’d encourage you to contact us so we can help you understand what is possible at the moment and to help solve any issues for you.

TruffleRuby configurations

There are two main configurations of TruffleRuby - native and JVM. It’s important to understand the different configurations of TruffleRuby, as each has different capabilities and performance characteristics. You should pick the execution mode that is appropriate for your application.

When distributed as part of GraalVM, TruffleRuby by default runs in the native configuration. In this configuration, TruffleRuby is ahead-of-time compiled to a standalone native executable. This means that you don’t need a JVM installed on your system to use it. The advantage of the native configuration is that it starts about as fast as MRI, it may use less memory, and it becomes fast in less time than the JVM configuration. The disadvantage of the native configuration is that you can’t use Java tools like VisualVM, you can’t use Java interoperability, and peak performance may be lower than on the JVM. The native configuration is used by default, but you can also request it using --native. To use polyglot programming with the native configuration, you need to use the --polyglot flag.

TruffleRuby can also be used in the JVM configuration, where it runs as a normal Java application on the JVM, as any other Java application would. The advantage of the JVM configuration is that you can use Java interoperability, and peak performance may be higher than the native configuration. The disadvantage of the JVM configuration is that it takes much longer to start and to get fast, and may use more memory. The JVM configuration is requested using --jvm.

If you are running a short-running program you probably want the default, native, configuration. If you are running a long-running program and want the highest possible performance you probably want the JVM configuration, by using --jvm.

Getting the best startup time performance

To get the best startup time performance in most cases you want to use the native configuration, which is the default.

Getting the lowest memory footprint

To get the lowest memory footprint you probably initially want to use the native configuration, but as you get a larger working set of objects you may find that the simpler garbage collector and current lack of compressed ordinary object pointers (OOPS) actually increases your memory footprint and you’ll be better off with the JVM configuration using --jvm to reduce memory use.

Getting the best peak performance from TruffleRuby

To get the best peak performance from TruffleRuby for longer-running applications we would in most cases recommend the JVM configuration with --jvm.

However to reach this peak performance you need to warm-up TruffleRuby, as you do with most heavily optimising virtual machines. This is done by running the application under load for a period of time. If you monitor the performance (by measuring operation time, or response time) you will see it reduce over time and then probably stabilise.

Tuning TruffleRuby

To tune TruffleRuby you will need to consider the options of either your JVM or the SubstrateVM, and then Truffle, and Graal.

TruffleRuby has a large set of options, which you can see with the -Xoptions flag.

Logging

Ruby application logging and warning works as in the standard implementation of Ruby.

For logging of TruffleRuby internals, standard Java logging is used. The logging level can be set with -Xlog=INFO, =FINEST, or so on.

Troubleshooting TruffleRuby

The GraalVM implementation of Ruby is experimental and we can not guarantee it to be bug free. TruffleRuby uses sophisticated techniques to optimise a Ruby program and its users are strongly encouraged to submit useful bug reports to Truffleruby issues. 
If you encounter a performance problem, please consider the recommendations in Reporting Performance Problems document

.