HerokuJonan SchefflerExplainer
MJIT: A Method Based Just-in-time Compiler for Ruby
Summary
Ruby programs are compiled into YARV instructions and executed by a stack-oriented virtual machine, while register-oriented representations can expose more opportunities for compiler optimization. The post explains how YARV represents a simple expression, contrasts stack and register architectures, and introduces Vladimir Makarov’s Ruby RTL, an intermediate representation distinct from GCC’s RTL. MJIT uses RTL generated by MRI to make speculative, operand-specific optimizations, then compiles methods to machine code rather than relying only on interpretation. The interview discusses deoptimization: when assumptions change, MJIT generates a new machine-code version and can switch between JITed execution and RTL interpretation. The participants say this overhead should be tiny and should not degrade performance overall, while acknowledging edge cases where MJIT is not faster and that benchmarking JITs is difficult.
Context
Ruby normally executes YARV instructions in its virtual machine. The discussion introduces MJIT to explain how method-based just-in-time compilation could change that execution model and how speculative optimization affects performance.
Approach / What changed
MJIT uses RTL generated by MRI to create speculative, operand-specific instructions and compile Ruby methods into machine code. When an assumption changes, it generates a new machine-code version and switches between JITed execution and RTL interpretation.
Takeaways
YARV is a stack-oriented intermediate representation and virtual machine for Ruby; its instructions operate by pushing operands onto and popping values from a stack.
Ruby RTL is distinct from GCC’s RTL and enables speculative optimizations based on operand assumptions, such as integer-specific behavior.
MJIT’s deoptimization path generates new machine code when assumptions change; the interview describes the overhead as small but acknowledges cases where MJIT is not faster.
Source and freshness
- Source
- heroku.com
- Published
- Added to Yomu
Summaries are generated from the original by a language model. Check the original before relying on it.
Keep reading
Related reading
The Good Stuff
Paid picks we use ourselves. Links carry a referral code — a subscription through one earns us a commission at no extra cost to you.
The best engineering reads, once a week
One useful email. Unsubscribe anytime.