skip to content

In Rails 8.1, what does config.yjit default to in each environment, and what changes when you set it to false or a Hash?

level: middleimportance: should knowfreq 35%

answer

  1. load_defaults 8.1, not production.rb
  2. !Rails.env.local?
  3. 7.2 turned it on everywhere
  4. enable_yjit initializer late in boot
  5. Hash goes to RubyVM::YJIT.enable

basics

~10 s

Under load_defaults 8.1, config.yjit is !Rails.env.local?: off in development and test, on in production and other environments. false makes Rails skip enabling YJIT; a Hash enables it with those options, such as mem_size.

solid answer

~40 s

`config.yjit` is set by `load_defaults`, not in production.rb. The original default was `false`. 7.2 made it `true`, and 8.1 made it `!Rails.env.local?`, so development and test run without YJIT, while production and staging get it. Late in boot, Rails' `enable_yjit` initializer calls `RubyVM::YJIT.enable` when the setting is truthy and that method exists, which needs Ruby 3.3 or later. A Hash is passed through as keyword options, for example `config.yjit = { mem_size: 64 }` to cap YJIT's memory at 64 MiB per process. `false` only stops Rails from enabling it. A process started with Ruby's own `--yjit` flag or `RUBY_YJIT_ENABLE=1` keeps YJIT on. 8.1 turned it off locally because reloading and redefining methods, mocks included, keep invalidating compiled code.

code

ruby · 6 lines
ruby
# config/environments/production.rb
Rails.application.configure do
  # load_defaults 8.1 already sets config.yjit = !Rails.env.local?
  # On a memory-tight host, cap YJIT's memory per process (MiB):
  config.yjit = { mem_size: 64 }
end

go deeper

for a junior

Recall that Rails can turn on Ruby's YJIT compiler and that in 8.1 it is on in production but off in development and test.

for a middle

Explain where the default comes from (load_defaults), the 7.2 to 8.1 change and why, and what the Hash and false values do.

for a senior

Account for the interpreter flags that bypass the setting, Ruby 3.2's no-op, and per-worker memory when rolling YJIT out.

for a principal

Decide fleet-wide whether YJIT's speed is worth its memory, and keep the dev, CI and production asymmetry in mind when judging benchmarks.

## What the setting is **YJIT** is CRuby's just-in-time compiler. It turns frequently run Ruby methods into machine code, trading extra memory for speed. How YJIT itself works and which Ruby flags tune it belong to Ruby's own performance material. This question is about the Rails switch: `config.yjit`, a Railties setting that decides whether Rails turns YJIT on while the app boots. ## Its default, release by release The value comes from `config.load_defaults`, so an app's default depends on the version it targets, not on the Rails gem it runs: | `load_defaults` target | `config.yjit` default | Effect | |---|---|---| | before 7.2 | `false` | Rails never enables YJIT | | 7.2 and 8.0 | `true` | enabled in every environment on Ruby 3.3+ | | 8.1 | `!Rails.env.local?` | off in development and test, on everywhere else | `Rails.env.local?` is true exactly for `development` and `test`, so `production`, `staging` or any custom environment get YJIT under 8.1 defaults. The 8.1 change explains itself in the changelog: development and test reload code and redefine methods (mocking, for instance), and YJIT generally is not faster there. Compiled code keeps getting thrown away, so the memory buys nothing. ## How Rails turns it on `Rails::Application::Finisher` has an initializer named `enable_yjit`. It runs late in boot and does roughly this: 1. If `config.yjit` is truthy **and** `RubyVM::YJIT.enable` is defined, 2. take the setting as options if it is a Hash, otherwise none, 3. call `RubyVM::YJIT.enable(**options)`. Three consequences follow: - **Ruby 3.2 ignores it.** `RubyVM::YJIT.enable` arrived in Ruby 3.3. Rails 8.1 still runs on 3.2, where `config.yjit = true` silently does nothing; you would need Ruby's own flags there. - **Early boot code is not compiled.** Enabling from an initializer, rather than with an interpreter flag, means code that runs only before that point in boot stays interpreted, which saves JIT memory. Ruby's own YJIT documentation suggests enabling it from Ruby code after the application is initialized for that reason. - **It runs where boot runs.** With Puma preloading, that is the master before workers fork. Each worker then compiles its own hot code, so the JIT memory is paid **per process**. ## The Hash form and false - `config.yjit = { mem_size: 64 }` passes `mem_size: 64` to `RubyVM::YJIT.enable`: a soft limit of 64 MiB, below Ruby's default of 128. It is the first lever on a memory-tight box before giving YJIT up entirely. - `config.yjit = { stats: true }` enables YJIT with statistics collection, which prints a report when the process exits. That is diagnostic, not something to leave on. - `config.yjit = false` stops Rails from calling `enable`. It does **not** disable YJIT that the interpreter already started because of `--yjit` in `RUBYOPT` or `RUBY_YJIT_ENABLE=1`. If memory matters, check how the process was launched as well as the Rails config. - Overriding per environment is ordinary configuration, for example in `config/environments/staging.rb`. ## Checking what a running process has Because three things can decide the outcome (the Rails setting, the Ruby version and the interpreter's own flags), check the result instead of the config: - In a production console, `RubyVM::YJIT.enabled?` answers whether YJIT is on in that process. - `Rails.application.config.yjit` shows what Rails was asked to do, which may differ. - `RUBY_DESCRIPTION` gains a `+YJIT` marker as soon as YJIT is enabled, including when Rails' initializer enables it at runtime, so reading it after boot is another quick check. - Watch per-worker resident memory before and after the change on one host; that is the cost side the setting controls. ## In the legacy marketplace app An older app that upgrades Rails but keeps an old `load_defaults` never gets YJIT from Rails at all, because its target predates 7.2. Raising `load_defaults` to 8.1 (or setting `config.yjit` directly) turns it on in production. The cost lands on every worker's memory, which is why the rollout belongs with the memory budget: measure each worker's resident memory after warm-up with YJIT on, then adjust `mem_size` or the worker count. Remember the asymmetry too. Since 8.1, your laptop and CI no longer run YJIT, so a local benchmark tells you little about production with it on. If you need to profile with YJIT locally, set `config.yjit = true` in that environment's file for the session instead of changing the default for everyone.

  • A Rails 8.1 app sets config.yjit = false, yet its processes still show YJIT enabled; how?
    Rails' setting only decides whether its initializer calls `RubyVM::YJIT.enable`. If the interpreter was started with `--yjit` (often through `RUBYOPT`) or with `RUBY_YJIT_ENABLE=1`, YJIT is already on before Rails boots, and `false` cannot turn it off. Remove the flag or variable from the process environment.
  • Why does the per-process nature of YJIT's memory matter for a Puma cluster?
    Each forked worker compiles the methods it runs into its own code memory, so the JIT's cost repeats per worker. Eight workers each using tens of MiB for compiled code add up. That is why `mem_size` or the worker count is adjusted together with enabling YJIT, after measuring warmed-up workers.

  • Rails 8.1 enables YJIT in development too, so local benchmarks reflect production.
  • config.yjit = false switches YJIT off even when Ruby was started with --yjit.
  • config.yjit = true turns YJIT on under Ruby 3.2 as well.
  • config.yjit only accepts true or false, so a Hash raises at boot.
  • YJIT compiles once in the Puma master, so extra workers add no JIT memory.

Assumes Rails 8.1 (load_defaults 8.1 sets config.yjit = !Rails.env.local?) on Ruby 3.3 or later, where RubyVM::YJIT.enable exists; Ruby 4.0 accepts mem_size in MiB.