Enabling YJIT
Introduction
I just did this today. So, this post will not include the results of enabling YJIT.
As finance folks say
This is not
investmentprogramming advice.
Why
In one acronym, FOMO.
A while back I read this PR, Enable YJIT by default if running Ruby 3.3+. It made it look like a low hanging fruit. I thought, why not?
The why yes list included:
How
On the server(s)
My Ruby on Rails (Ruby 3.3.0 -via rbenv
-) app was running on an Ubuntu server.
So, what I did was:
First, I checked if my Ruby had YJIT. I did not remember if I had installed it with it 🤷♂️.
ruby --enable-yjit -v
I didn’t. As a matter of fact, I did not have Rust installed either. So, I did that first.
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
And then I added Rust to my path.
nano ~/.bashrc
export PATH="$HOME/.cargo/bin:$PATH"
source ~/.bashrc
rustc --version
Finally, I installed Ruby with YJIT.
RUBY_CONFIGURE_OPTS="--enable-yjit" rbenv install 3.3.0
On the app
I just added the initializer to enable YJIT (taken from the PR mentioned above ☝️).
# config/initializers/enable_yjit.rb
if defined? RubyVM::YJIT.enable
Rails.application.config.after_initialize do
RubyVM::YJIT.enable
end
end
Conclusion
Everything went so smooth that it feels like the fruit fell into my hands.
I did not mentioned it because it’s kind of a given, but having a good test coverage provides a lot of confidence when trying out this things. And, of course, I tried everything on my local machine first and on my staging server later.
The only regret so far is not having done this 30 minutes later. I finished the whole thing just minutes before Ruby 3.3.1 was released 🤦♂️.