---
title: "MJIT: A Method Based Just-in-time Compiler for Ruby"
description: "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."
---

# MJIT: A Method Based Just-in-time Compiler for Ruby

[Heroku](https://yomu.fyi/company/heroku) · Jonan Scheffler · Apr 12, 2018

**Type:** Explainer

## 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.

**Tags:** [Architecture](https://yomu.fyi/topic/architecture), [Performance](https://yomu.fyi/topic/performance), [Ruby](https://yomu.fyi/topic/ruby)

- Source: [Heroku](https://www.heroku.com/blog/ruby-mjit)
- Source URL: https://www.heroku.com/blog/ruby-mjit
- Ingested by Yomu: 2026-09-09T14:26:20.071Z

[Read original post](https://www.heroku.com/blog/ruby-mjit)
