RJIT: A Pure-Ruby JIT for Ruby

k0kubun
2 min readMar 9, 2023

--

I’m Takashi Kokubun (@k0kubun), a Staff Developer at Shopify who works on optimizing YJIT full-time, which is Ruby’s production-grade JIT compiler written in Rust. While I work solely on YJIT during my work hours, I’ve also maintained another JIT in Ruby, MJIT, in my spare time.

This year, I proposed to change the MJIT’s design to simplify the Ruby VM, which will ultimately make the YJIT development easier. Since the new version is no longer a “M”ethod “JIT”, which MJIT was named after, I changed the project name to RJIT. RJIT is a pure-Ruby JIT compiler, so it’s called “R”uby “JIT”.

Why does Ruby have two JITs?

It’s not rare that a language supports multiple JITs. For example, the Java HotSpot VM has the JVM Compiler Interface (JVMCI) that allows you to integrate a JIT like the Graal Compiler.

In the case of Ruby, the reference implementation CRuby doesn’t have public interface to integrate a third-party JIT. However, while CRuby already has YJIT that optimizes production pretty well, people have still developed other CRuby JITs, such as vnmakarov/ruby (sir-mirjit), tenderlove/tenderjit, and jhawthorn/hawthjit.

Since Ruby 3.2 MJIT was partly rewritten in Ruby, it became possible to integrate a third-party JIT like those by monkey-patching it. It’s always fun to write your own compiler. If you just remove MJIT, you won’t be able to enjoy that anymore. It’s also a great place to experiment with ideas for JIT optimization that can later be applied to YJIT. So I decided to keep this project while my primary interest remains to be making YJIT the best JIT for Ruby.

Which JIT should I use in production?

YJIT. Period.

YJIT is written in Rust while RJIT is in Ruby. While its peak performance could be as fast as YJIT if RJIT generates the same code as YJIT, there will always be a drawback in the warmup speed and memory footprint. I don’t think RJIT will ever beat this trade-off. Fast warmup and low memory footprint is crucial for companies like Shopify where developers deploy Ruby applications quite often to massive-scale clusters.

Furthermore, Shopify use YJIT in their tier-1 applications. They are business-critical, so if a problem is found in YJIT, the YJIT team fixes it very quickly. You can’t expect the same thing from a hobby project maintained in a single person’s spare time, RJIT. The YJIT team is a team full of kind and talented engineers. I’d like to once again thank the team and Shopify for having me as a teammate.

Conclusion

RJIT is there for your fun. Enjoy your JIT development in Ruby. If you’re interested, my RubyKaigi 2023 talk will cover how to develop and/or contribute to a Ruby JIT. See you in Matsumoto!

--

--