nickjj / docker-rails-example

A production ready example Rails app that's using Docker and Docker Compose.
MIT License
941 stars 185 forks source link

Enable YJIT by default #57

Closed nickjj closed 6 months ago

nickjj commented 1 year ago

Debian Slim now has YJIT available thanks to https://github.com/docker-library/ruby/commit/6db728e.

That means it's available to use, which you can confirm with:

$ docker container run --rm ruby:3.2.0-slim-bullseye ruby --yjit -v
ruby 3.2.0 (2022-12-25 revision a528908271) +YJIT [x86_64-linux]

But it's not enabled by default. One of the easiest ways to enable it is by setting this environment variable RUBYOPT=--yjit. Technically this project is already equipped to use it. You can set that in your .env and you're done.

You can confirm it's "really" enabled and working by running:

$ docker compose up

$ ./run rails console
irb(main):001:0> RubyVM::YJIT.enabled?
=> true

I'm opening this issue mainly as a form of documentation and perhaps in the near'ish future we can enable it by default.

One of the main reasons why I didn't enable it by default already is because memory usage is higher with it enabled and that could play a role in production on a single server or more generally smaller server deploys. Rails is already pretty memory hungry especially when you account for running Puma, Sidekiq and Action Cable. Each of those processes will use more memory. Anything that amplifies that per process could have an impact.

What could be helpful here is to benchmark a couple of use cases to see how much extra memory it uses.

nickjj commented 6 months ago

Ruby 3.3.0 changed YJIT to be enabled by default but it's paused by default. This lets you toggle YJIT at runtime. There's also been quite a bit of memory optimizations in 3.3.0 vs 3.2.X. I'm happy to turn it on by default in this project.

The environment variable approach would still work. I went with this approach instead because Rails recently merged a PR which uses an initializer to enable YJIT at https://github.com/rails/rails/pull/49947. Now this project is compatible with that.