---
title: "When is JIT Faster Than A Compiler?"
description: "JITs can beat ahead-of-time compilers for highly dynamic languages like Ruby because they can compile code under speculative assumptions, such as that Integer#+ hasn't been redefined, and throw that code away (de-optimize) when an assumption breaks."
url: "https://swe.quest/articles/when-is-jit-faster-than-a-compiler-a0b7c41444704d7c"
canonical: "https://shopify.engineering/when-jit-faster-than-compiler"
source: "Shopify Engineering"
published: "2022-08-11"
difficulty: "easy"
kind: "Deep dive"
reading_minutes: 7
tags: ["compilers","runtimes","ruby","performance"]
---

# When is JIT Faster Than A Compiler?

A post on [Shopify Engineering](https://swe.quest/sources/shopify.md), published 2022-08-11, 7 min read, Deep dive. Original: [https://shopify.engineering/when-jit-faster-than-compiler](https://shopify.engineering/when-jit-faster-than-compiler)

## Summary

JITs can beat ahead-of-time compilers for highly dynamic languages like Ruby because they can compile code under speculative assumptions, such as that Integer#+ hasn't been redefined, and throw that code away (de-optimize) when an assumption breaks. An AOT compiler must emit code that stays correct no matter what the program does later. An interpreter must constantly re-check whether anything changed. Both end up conservative and slow. YJIT in CRuby uses this speculate-and-invalidate approach, enabled with --yjit, and is expected to be production-ready for some uses in Ruby 3.2.

Written by swe.quest, not by the author.

**Difficulty:** Easy: no special background needed; explains what something is and why it matters

## Topics

- [Compilers](https://swe.quest/tags/compilers.md) (Compilers & runtimes)
- [Runtimes & VMs](https://swe.quest/tags/runtimes.md) (Compilers & runtimes)
- [Ruby](https://swe.quest/tags/ruby.md) (Languages)
- [Performance engineering](https://swe.quest/tags/performance.md) (Reliability & operations)

## Prerequisites

- interpreters vs compilers ([easy posts on Compilers](https://swe.quest/tags/compilers.md?difficulty=easy))

## Keywords

YJIT, CRuby, JIT compilation, de-optimization, MJIT

## Related posts

- [YJIT: Building a New JIT Compiler for CRuby](https://swe.quest/articles/yjit-building-a-new-jit-compiler-for-cruby-51c5ee3c390b2164.md): Shopify Engineering · 2021-10-14 · Easy · 6 min read · Compilers, Ruby, Runtimes & VMs, Performance engineering
  YJIT is a new JIT compiler built inside CRuby by Shopify and GitHub engineers, based on Basic Block Versioning, and aimed at real-world workloads like Rails where the GCC-based MJIT has struggled.
- [Try Out YJIT for Faster Rubying](https://swe.quest/articles/try-out-yjit-for-faster-rubying-6105453188fbb741.md): Shopify Engineering · 2021-10-15 · Medium · 7 min read · Ruby, Compilers, Runtimes & VMs, Performance engineering
  YJIT, Shopify's just-in-time compiler built into CRuby and slated for Ruby 3.1, compiles frequently used Ruby code to native machine code, speeding up a simple Rails benchmark by 20% or more and a real Shopify web service by about 6% on canary traffic.
- [Ruby 3.2’s YJIT is Production-Ready](https://swe.quest/articles/ruby-3-2-s-yjit-is-production-ready-05f17a68a56ea05c.md): Shopify Engineering · 2023-01-17 · Easy · 10 min read · Ruby, Compilers, Runtimes & VMs, Performance engineering, Rust
  YJIT, Shopify's JIT compiler for CRuby, became production-ready in Ruby 3.2 after a port from C99 to Rust and a cut in memory overhead to about a third of Ruby 3.1 levels.
- [Our Experience Porting the YJIT Ruby Compiler to Rust](https://swe.quest/articles/our-experience-porting-the-yjit-ruby-compiler-to-rust-221d770c36dd7d35.md): Shopify Engineering · 2022-05-11 · Medium · 16 min read · Rust, Compilers, Ruby, Refactoring & migrations, C & C++
  Shopify's YJIT team ported the CRuby JIT compiler from about 11,000 lines of C99 to Rust because C's lack of modules, generics, containers and safe macros made a growing compiler hard to manage.
- [Year in Review 2022: Tenderlove's Ruby and Rails Reflections and Predictions](https://swe.quest/articles/year-in-review-2022-tenderlove-s-ruby-and-rails-reflections-and-predictions-d873d71ac3e91545.md): Shopify Engineering · 2022-12-22 · Easy · 8 min read · Ruby, Runtimes & VMs, Memory management, Developer tools, Compilers
  Ruby's 2022 highlights include YJIT's rewrite in Rust and its new ARM support, so \`ruby --yjit\` now runs on Apple Silicon development machines as well as production.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
