Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Towards Ruby 4 JIT / RubyKaigi 2022
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Takashi Kokubun
September 08, 2022
Programming
3
12k
Towards Ruby 4 JIT / RubyKaigi 2022
RubyKaigi 2022
Takashi Kokubun
September 08, 2022
Tweet
Share
More Decks by Takashi Kokubun
See All by Takashi Kokubun
ZJIT: The Ruby 4 JIT Compiler / Ruby Release 30th Anniversary Party
k0kubun
1
420
ZJIT: The Future of Ruby Performance / San Francisco Ruby Conference 2025
k0kubun
1
81
ZJIT: Building a New JIT Compiler for Ruby / REBASE 2025
k0kubun
0
82
Deoptimization: How YJIT Speeds Up Ruby by Slowing Down / RubyKaigi 2025
k0kubun
2
3.8k
YJIT Makes Rails 1.7x faster / RubyKaigi 2024
k0kubun
7
15k
Ruby JIT Hacking Guide / RubyKaigi 2023
k0kubun
2
10k
YJIT: Dive into Ruby's JIT compiler written in Rust / Rust.Tokyo 2022
k0kubun
2
2.3k
Optimizing Production Performance with MRI JIT / RubyConf 2021
k0kubun
1
530
Why Ruby's JIT was slow / RubyKaigi Takeout 2021
k0kubun
3
2k
Other Decks in Programming
See All in Programming
TROCCOで実現するkintone+BigQueryによるオペレーション改善
ssxota
0
120
maplibre-gl-layers - 地図に移動体たくさん表示したい
kekyo
PRO
0
160
CSC307 Lecture 10
javiergs
PRO
1
690
「やめとこ」がなくなった — 1月にZennを始めて22本書いた AI共創開発のリアル
atani14
0
350
RAGでハマりがちな"Excelの罠"を、データの構造化で突破する
harumiweb
9
2.4k
今更考える「単一責任原則」 / Thinking about the Single Responsibility Principle
tooppoo
3
1.4k
ふつうの Rubyist、ちいさなデバイス、大きな一年
bash0c7
0
110
AIとペアプロして処理時間を97%削減した話 #pyconshizu
kashewnuts
1
190
株式会社 Sun terras カンパニーデック
sunterras
0
2k
PJのドキュメントを全部Git管理にしたら、一番喜んだのはAIだった
nanaism
0
230
AI時代でも変わらない技術コミュニティの力~10年続く“ゆるい”つながりが生み出す価値
n_takehata
2
610
CDIの誤解しがちな仕様とその対処TIPS
futokiyo
0
160
Featured
See All Featured
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8k
The agentic SEO stack - context over prompts
schlessera
0
680
For a Future-Friendly Web
brad_frost
183
10k
The SEO identity crisis: Don't let AI make you average
varn
0
400
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
231
22k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
1.9k
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
67
37k
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
280
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.6k
4 Signs Your Business is Dying
shpigford
187
22k
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
1
190
Transcript
Towards Ruby 4 JIT @k0kubun
@k0kubun Maintain: MJIT, Haml, ERB Shopify team
GitHub Sponsors
Haml 6
Introduction to Ruby JIT
How does Ruby JIT work? Ruby
How does Ruby JIT work? 1 + 2 Ruby Abstract
Syntax Tree
How does Ruby JIT work? 1 + 2 putobject 1
putobject 2 opt_plus leave Ruby Abstract Syntax Tree Instruction Sequence (Bytecode)
How does Ruby JIT work? 1 + 2 putobject 1
putobject 2 opt_plus leave Ruby Abstract Syntax Tree Instruction Sequence (Bytecode) Machine Code
How does Ruby JIT work?
CRuby JIT 1: MJIT
CRuby JIT 2: YJIT
Current CRuby JITs speed.yjit.org
Current CRuby JITs speed.yjit.org
Current CRuby JITs • YJIT • Available since Ruby 3.1
• --jit or --yjit • MJIT • Available since Ruby 2.6 • --mjit
Current CRuby JITs • YJIT • Ruby 3.1: x86_64 only,
no code GC, written in C • Ruby 3.2: arm64 support, (hopefully) code GC, written in Rust • MJIT • Ruby 3.1: Stable-ish, portable, native threads, written in C • Ruby 3.2: Experimental, fork + SIGCHLD, written in Ruby
MJIT in Ruby
None
None
mjit.rb: Secret "standard library" in Ruby 3.2 • mjit.rb •
Even more powerful than TracePoint • You can monkey-patch CRuby JIT • No compatibility guarantee • Every module is private, so const_get is required
BYOJ: Bring Your Own JIT
BYOJ: Bring Your Own JIT • Load and pause MJIT
with --mjit=pause • Define RubyVM::MJIT.compile • Use RubyVM::MJIT.const_get(:C) to hack RubyVM • Call RubyVM::MJIT.resume to start JIT With Ruby 3.2:
YJIT-style JIT • Monkey-patch RubyVM::MJIT.compile
MJIT-style JIT • Monkey-patch RubyVM::MJIT::Compiler.compile
MJIT-style JIT
Everyone is writing CRuby JIT
Benchmarking Ruby JIT
yjit-bench
yjit-bench • yjit-bench has three kinds of benchmarks: 1. Headlining
Benchmarks 2. Other Benchmarks 3. Micro Benchmarks
1. Headlining benchmarks • activerecord • hexapdf • liquid-render •
mail • psych-load • railsbench ✉
2. Other Benchmarks • binarytrees, fankuchredux, nbody • chunky_png •
erubi, erubi_rails • lee • optcarrot • rubykon
3. Micro Benchmarks • 30k_ifelse, 30k_methods • cfunc_itself, str_concat •
fib • getivar, setivar • keyword_args • respond_to
None
Benchmark Your Own JIT • ./run_benchmarks.rb -e “/path/to/ruby --any-option” •
Pass multiple -e options to compare different JITs
Towards Ruby 4 JIT
My wish on Ruby 4 JIT • I want Ruby
4 to be as fast as Java or JavaScript • Ruby 4's performance should be a reason to leave Python
None
More Concrete Examples
None
None
None
Ruby 4 Canary • true is mov-ed (immediate) • No
opt_* VM instruction • Constant folding • Ruby / C method inlining
Ruby 4 Canary’ • Single branch instruction to access @one
• Single register to access two • No heap allocation • No stack frame
None
None
Ruby 4 Canary 2 • 5000050000 is mov-ed (immediate) •
Ruby -> C -> Ruby inlining
How can we get there?
Optimization Challenges 1. Constants 2. Variables 3. Method calls 4.
Garbage collection
1. Constants
1. Constants
1. Constants
1. Constants
1. Constants
1. Constants
1. Constants
1. Constants
2. Variables
2. Variables
2. Variables
2. Variables
2. Variables
2. Variables
2. Variables
2. Variables
2. Variables
2. Variables
2. Variables
2. Variables 2021 2022 (tomorrow)
3. Method calls
3. Method calls
3. Method calls
3. Method calls
3. Method calls
3. Method calls
3. Method calls • Code locality • Method inlining: C
㱻 Ruby • Pass arguments with native ABI • Deoptimization on redefinition or interruption (or TracePoint)
4. Garbage collection
4. Garbage collection
4. Garbage collection
Next Steps • We still have a lot of rooms
for improvements on yjit-bench • More cross-instruction optimizations • More method inlining over Ruby and C
Conclusion • Build your own JIT with Ruby 3.2 •
Benchmark your JIT with yjit-bench