We know a lot about Ruby! And today we’d like to explain you Ruby new feature that is now available in Ruby 2.6. This is MJIT. It will compile instructions that are used often into binary code. The result is an optimized binary which runs your code faster.
What is MJIT? It stands for “Method Based Just-in-Time Compiler”. Ruby compiles your code into YARV instructions, these instructions are run by the Ruby Virtual Machine. Ruby 2.6 comes with a set of JIT-specific options that will help us discover exactly how it works. You can see these options by running
This is how the JIT works:
- Count method calls
- When one method is called 5 times (default for
jit-min-calls) trigger JIT
- A C file that contains the instructions for this method is created (these are YARV instructions, but inlined)
- Compilation happens in the background (unless
--jit-wait) using a regular C compiler like GCC
- When the compilation is done the resulting shared library file is used when this method is called
The goal of MJIT is to make Ruby faster. MJIT is a “Just-in-Time Compiler” available in Ruby 2.6, it can be enabled with the
--jit flag. MJIT is promising & can speed up some small programs, but there is still a lot of work to do!