A Ruby Inquiry: Is it Interpreted or Compiled?
Join the DZone community and get the full member experience.Join For Free
however, it turns out that both jruby and rubinius, two other popular implementations of ruby, support using a compiler the same way you would with a statically typed language like c or java. both jruby and rubinius first compile your ruby code to byte code, and later execute it.
today i’m going to show you how to use these ruby compilers, and i’ll also take a peek under the hood to see what they produce internally. possibly you’ll rethink some of your assumptions about how ruby works along the way.
using the rubinius compiler
using the rubinius compiler is as simple as running any ruby script. here’s a very silly but simple ruby program i’ll use as an example today:
class adder def add_two(x) x+2 end end puts adder.new.add_two(3)
now if i save that into a file called “simple.rb,” switch to rubinius using rvm, and run the script i’ll get the number “5” as expected:
$ rvm rbx-1.2.4-20110705 $ ruby simple.rb 5
not very interesting, i know. but what is interesting is that when i ran simple.rb rubinius created a new, hidden directory called “.rbx” with a strange, cryptically named file in it:
$ ls -a . .. .rbx simple.rb $ find .rbx .rbx .rbx/a7 .rbx/a7/a7fc1eb2edc84efed8db760d37bee43753483f41
this vaguely reminds me of how git saves the git repository data in a hidden folder called “.git,” also using cryptic hexadecimal names. what we are looking at here is a compiled version of simple.rb: the “a7fc1eb2e…” file contains my ruby code converted into rubinius byte code.
whenever you run a ruby script, rubinius uses a two step process to compile and run your code:
on the top you can see that first rubinius compiles your code into byte code, and then below later executes it using the rubinius virtual machine, which can compile the byte code into native machine language. rubinius also caches the byte code using the hexadecimal naming scheme i showed above, avoiding the need for the compile step entirely if the ruby source code file didn’t change.
as brian ford explained on the rubinius blog , you can actually run the rubinius compiler directly like this:
$ rbx compile simple.rb -o simple.bytecode
this compiles my ruby code and saves the byte code in the specified file. if we look at the simple.bytecode file, we’ll see a series of alphanumeric tokens that don’t make any sense. but if you run the compiler using the “-b” option you can see an annotated version of the rubinius byte code:
$ rbx compile simple.rb -o simple.bytecode -b ============= :__script__ ============== arguments: 0 required, 0 post, 0 total arity: 0 locals: 0 stack size: 6 lines to ip: 1: 0..26, 7: 27..63 0000: push_rubinius 0001: push_literal :adder 0003: push_nil 0004: push_scope 0005: send_stack :open_class, 3 ... etc ... =============== :add_two =============== arguments: 1 required, 0 post, 1 total arity: 1 locals: 1: x stack size: 3 lines to ip: 2: -1..-1, 3: 0..5 0000: push_local 0 # x 0002: meta_push_2 0003: meta_send_op_plus :+ 0005: ret ... etc ...
at the bottom here we can see the compiled version of my silly add_two method. it’s actually somewhat easy to understand the byte code, since it’s annotated so well:
- first “push_local” saves the value of the “x” parameter on the virtual machine stack.
- then it pushes the literal value 2.
- then it executes the + operation.
- and finally it returns.
the rubinius virtual machine reminds me of those old “reverse polish” calculators from the 1980s, in which you would enter values on a stack in a similar way. as i discussed 3 weeks ago , the rubinius source code is actually quite easy to understand since a large portion of it is actually written in ruby, while the rest is written in well documented c++. the rubinius compiler is no exception: it’s actually written in ruby too! if you’re interested, you can see how the rubinius compiler works without having to understand c++ at all. to get started take a look in the “lib/compiler” directory.
the rubinius virtual machine, which runs the rubinius byte code, is implemented in c++ and leverages an open source project called llvm . like jruby, it uses a “just in time” compiler to convert the byte code to machine language at runtime. this means that your ruby code, for example the add_two method above, ends up being converted into native machine language and run directly by your computer’s hardware!
using the jruby compiler
now let’s take a look at how jruby compiles ruby code; i’ll start by using rvm to switch over to jruby, and then i’ll run the same simple.rb script:
$ rvm jruby-head $ ruby simple.rb 5
no surprise: we get the same result. at a high level, jruby uses the same two step process to run your script – first it compiles the ruby code into byte code, and then executes the byte code using the java virtual machine (jvm). see my post last week called journey to the center of jruby to learn more about this.
here’s another diagram showing the two step process, this time for jruby:
like with rubinius, it’s possible to run the jruby compiler directly using the “jrubyc” command… following the java executable naming pattern (“java” –> “javac”). thanks to my friend alex rothenberg for pointing this out. running “jrubyc” will create a java .class file, which we can inspect using the java decompiler like i did last week:
$ jrubyc simple.rb $ ls simple.class simple.rb $ javap -c simple.class > simple.bytecode
now the simple.bytecode file will contain an annotated version of the java byte code the jvm will execute. unlike rubinius, which creates byte code that is fairly clean, simple and easy to understand, java byte code is much more cryptic and confusing. searching through the simple.bytecode file for my method add_two , i found the following snippet of byte code:
public static org.jruby.runtime.builtin.irubyobject method__1$ruby$add_two(simple, org.jruby.runtime.thread... code: 0: aload_3 1: astore 9 3: aload_1 4: aload_2 5: aload 9 7: invokedynamic #80, 0 // invokedynamic #1:"fixnumoperator:+":(lorg/jruby/runtime/thread... 12: areturn
although quite difficult to understand, there are a couple of important details to notice:
first, jruby has compiled my ruby add_two method into a byte code method called method__1$ruby$add_two . this proves that my ruby script has been compiled! that is, when jruby ran simple.rb above, it did not read the ruby code, interpret it and just follow the instructions like the mri interpreter would do. instead, it converted my ruby script into byte code, and specifically my add_two method into the byte code snippet above.
second, notice the use of the “invokedynamic” byte code instruction. this is a new innovation of the java virtual machine, making it easier for the jvm to support dynamic languages like ruby. here you can see it’s used by the add_two method to call the + operator of the ruby fixnum class, for my x+2 ruby code. if you’re interested in what invokedynamic actually means, be sure to check out a very detailed post charles nutter wrote about it back in 2008. this use of invokedynamic is actually new for java 1.7 and jruby 1.7, so if you’re using the current release version of jruby (1.6.4) or earlier you won’t see it appear in the byte code.
finally, as i explained last week since the jvm uses a just in time (jit) compiler, all of the byte code you see above – in other words my ruby script including the add_two method – will be compiled directly into native machine language if the jvm notices that it is called enough times, that it’s in a “hotspot.”
why look under the hood? who cares how ruby works?
today i’ve shown you some of the internal, technical details of rubinius and jruby. many of you might find this boring and unimportant: who cares how ruby works internally? all i care about is that my ruby program works. and from one point of view that is all that really matters.
however, i find ruby internals to be fascinating… i really do like having at least a small understanding of what’s going on inside of ruby while it’s running my code. i also believe it will help me to become a more effective and knowledgeable ruby developer, even if i never contribute a line of internal code to rubinius, jruby or mri. and studying ruby internals has definitely lead me to a number of surprising discoveries, and forced me to rethink the mental model i have always had of the ruby interpreter… or should i say, the ruby compiler!
Opinions expressed by DZone contributors are their own.