Episode 278 · December 28, 2018
Learn how to enable the experimental new Just-in-time compiler for Ruby 2.6
ruby --jit test.rb ruby --jit-verbose=1 test.rb RUBYOPT="--jit" rails server RUBYOPT="--jit-verbose=1" rails server RUBYOPT="--jit" sidekiq
This episode we're gonna be talking about how to enable and run Ruby 2.6 with
the brand-new just-in-time (JIT) compiler.
That's the major feature of this release, but unfortunately it's still experimental
so it's something you probably won't want to run in production just yet. Hopefully
for future releases they make some major improvements to this, and it will improve
your production code. For now this is something that is experimental in the sense
that it doesn't improve the performance of your rails apps, your sidekiq workers,
that sort of things, but it does do a huge speed improvement for certain things.
One of those was a benchmark called Optcarrot
which is a actually a NES emulator. It's generating these frames from the NES, so
it's simulating all of that and we're trying to get it to run as fast as the original
NES which ran at 60fps. This is super cool because ruby is not generally known for
being that fast and this is a great way of doing something that's CPU intensive that
they can run as a benchmark, so it's pretty awesome. It's already been 1.7x faster
with Ruby 2.6 JIT enabled, which is phenomenal. That's a huge huge improvement!
Unfortunately this doesn't do great yet for memory intensive things like rails or
sidekiq, and that sort of things, so they don't really benefit from that at the
moment, generally speaking. But you should actually make sure that you run your code
with the JIT enabled and with it disabled and see how it works on your own code to
know for sure not whether or not it's going to be good or bad.
The JIT is actually disabled by default so you have to turn it on to run it, so what
we're going to do is take a look at that real quick at the terminal.
Make sure that you have Ruby 2.6 installed and running on your machine and then you can
ruby --jit test.rb.
That will enable the JIT, but this is not going to print out any output for you about the
JIT, so you may not feel like it's on for sure. You can able the verbose output option
to print out logs about the JIT compiling things like this:
ruby --jit-verbose=1 test.rb
That's one option to enable JIT, the other option is that a lot of times
you're running things like rails through a different executable, you're using
bundle exec rails or
rails command itself, and you're
not actually calling Ruby. Those other executables don't know the JIT option
or how to even pass it in to Ruby necessarily.
You can use the
RUBYOPT environment variable. If you wanted to do this on Heroku
you could go set that in your environment variable section. You could just pass
in the options that you want so if you want to enable the JIT you just say
and this will work for your rails executables or anything that you might want.
If you've set this globally so you can do it either on the command itself
like this where we just put it at the beginning:
RUBYOPT="--jit" rails s
Or you could export this variable so it's available for all commands and then
they'll all run with the JIT. Just keep that in mind on how you want to test things out.
So if you run
RUBYOPT="--jit" rails s, this is actually going to run with the JIT,
but you're not going to see any different output when this is on or off, you'll just
see the standard output from rails. Now if you turn
RUBYOPT="--jit-verbose=1" rails s
then you're going to see a lot of output from the JIT compiler. So this is actually
going and compiling a bunch of code as it's starting up Puma and a rails application
and you can see those lines of output here, and you see "JIT success", and the time it took,
and what code it's compiling, and where it's going to. It's outputting C code, and you can
see where that is, and it's called
_ruby_mjit, and some sort of random characters
here dot C. So it's printing out C code and then running that through the compiler which is
This is something you probably don't need to the verbose option on all the time, but
it's something just if you want to make sure that you're running the JIT, you can see that
right there and that makes that handy.
So that is the big experimental feature of Ruby 2.6 and because it's not a
blanket, everything is faster with us on it's disabled by default. They
don't want to release Ruby 2.6 and then everyone upgrades and all their
rails apps are significantly slower. So they have it turned off by default.
You can enable it run benchmarks on your code and make sure it's not a huge
amount slower. And if it isn't that much slower you can go ahead and run it
with the JIT in production.
This is a way for them to get the JIT out into the real world so that people are
using it and giving feedback on itm, and seeing how it performs in different real
scenarios and not just benchmarks. So that's a big step forward they're going to
be working on improving this for the next releases. That's hopefully very soon the
rails and other memory intensive applications rill actually be faster, hopefully
1.7x faster in the future. But for now it's disabled by default because of that.
If you need to enable the JIT, just remember
--jit is all you really need, and
it's probably easiest to always use
RUBYOPT for that so that's all you have to
do to enable the JIT in Ruby 2.6.
Join 27,623+ developers who get early access to new screencasts, articles, guides, updates, and more.