Ruby 3.2.0 released with WebAssembly compiler, production-ready YJIT optimization, and much faster regular expressions

Ruby By Alex Mit via Shutterstock

Ruby developers got a Christmas gift with the December 25th release of Ruby 3.2.0, with major new features including initial WebAssembly support and the first non-experimental release of YJIT (Yet another Just-in-Time Compiler), with an average 41% performance boost versus the Ruby interpreter.

The YJIT project was started by Shopify and assisted by GitHub. As explained by project leader Maxime Chevalier-Boisvert, Shopify is “a company that runs a massive server infrastructure powered by Ruby on Rails”, and GitHub is also a big Ruby on Rails user although less than it once was, according to this interview with a VP of software engineering.

CRuby already had a JIT compiler called MJIT (Method Based Just-in-Time Compiler) but Chevalier-Boisvert said that “while it has delivered speedups on smaller benchmarks, so far, it’s been less successful at delivering real-world speedups on widely used Ruby applications such as Ruby on Rails.”

YJIT is based on a JIT compiler architecture called Basic Block Versioning, initially developed by Chevalier-Boisvert as part of her PhD research. The goal of the YJIT project is to “eventually replace the interpreter for most of the execution”. Ruby 3.2.0 supports both YJIT and MJIT.

YJIT is officially part of Ruby and has been declared production-ready now that it “has been tested on production workloads for over a year and proven to be quite stable,” according to maintainer Yui Naruse’s release announcement for Ruby 3.2.0. It supports x86-64 and arm64 on Linux, MacOS, BSD and other Unix-like platforms.

One year ago YJIT was ported to Rust, the reason being that “we are facing challenges in terms of code maintainability. As you know, JIT compilers can get very complex, and C99 doesn’t offer many tools to manage this complexity,” according to Chevalier-Boisvert. This means that building CRuby with YJIT requires the Rust compiler.

Another big feature in 3.2.0 is WASI-based WebAssembly support. “This enables a CRuby binary to be available on a Web browser, a Serverless Edge environment, or other kinds of WebAssembly/WASI embedders,” the team said. One use is for the “Try Ruby” playground which uses this technology to run Ruby in the browser. There are some gaps in the support since “WebAssembly itself is missing some features to implement Fiber, exception, and GC because it’s still evolving,” said Naruse.

One more new feature is a greatly Regexp (Regular Expression) algorithm which can improve performance by many times: an example in Naruse’s post shows an expression which took 10 seconds in Ruby 3.1 now taking 0.003 seconds to run.