Enabling YJIT

Posted . Visible to the public.

YJIT Show archive.org snapshot is Ruby's default just-in-time compiler. It is considered production-ready since Ruby 3.2 ( source Show archive.org snapshot ).

To activate YJIT you need two steps:

  • Your ruby binary needs to be compiled with YJIT support.
  • You need to enable YJIT.

Getting a Ruby with YJIT support

We usually install Ruby with tools like rbenv or asdf. This compiles the ruby binary from the source code. Support for YJIT will be automatically added during this compilation, if you have rustc installed. This new requirement for the Ruby build process is documented here: ruby-build Wiki Show archive.org snapshot

You can check, whether your Ruby installation supports YJIT, by running:

$ ruby --yjit -v
ruby 3.3.4 (2024-07-09 revision be1089c8ec) +YJIT [x86_64-linux]

If YJIT is not available, the output of this command will tell you.

If you already have a specific Ruby version without YJIT installed, you can simply redo the installation after installing rustc. E.g.

rbenv install 3.3.4

You will probably need a bundle pristine afterwards.

Enabling YJIT

Ruby doesn't yet enable YJIT by default:

# Start Ruby without YJIT
$ ruby -e "puts RubyVM::YJIT.enabled?"
false

However, there are multiple ways to enable it:

# via CLI flag
$ ruby --yjit -e "puts RubyVM::YJIT.enabled?"
true

# via ENV variable
$ RUBY_YJIT_ENABLE=1 ruby -e "puts RubyVM::YJIT.enabled?"
true

# Start Ruby without YJIT, but enable it later
$ ruby -e "RubyVM::YJIT.enable; puts RubyVM::YJIT.enabled?"
true

The last variant is what Rails 7.2+ is doing after the boot process with its new default configuration. So if you are on a current Rails version and didn't explicitly turn off YJIT, you are probably already using it.

Keep in mind that Rails turns on YJIT after the boot process. This is intentional (see section "New Features in YJIT 3.3") Show archive.org snapshot to avoid compiling code that runs only once during initialization. Therefore, it is probably a good idea to not use the CLI flag --yjit or the ENV variable RUBY_YJIT_ENABLE=1 for starting Rails processes on your server.

Klaus Weidinger
Last edit
Michael Leimstädtner
License
Source code in this card is licensed under the MIT License.
Posted by Klaus Weidinger to makandra dev (2024-11-25 13:50)