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
Ruby JIT Hacking Guide / RubyKaigi 2023
Search
Takashi Kokubun
May 13, 2023
Programming
10k
2
Share
Ruby JIT Hacking Guide / RubyKaigi 2023
Ruby JIT Hacking Guide
https://rubykaigi.org/2023/
Takashi Kokubun
May 13, 2023
More Decks by Takashi Kokubun
See All by Takashi Kokubun
一度始めたらやめられない開発効率向上術 / Findy あなたのdotfilesを教えて!
k0kubun
4
2.8k
ZJIT: The Ruby 4 JIT Compiler / Ruby Release 30th Anniversary Party
k0kubun
1
460
ZJIT: The Future of Ruby Performance / San Francisco Ruby Conference 2025
k0kubun
1
95
ZJIT: Building a New JIT Compiler for Ruby / REBASE 2025
k0kubun
0
96
Deoptimization: How YJIT Speeds Up Ruby by Slowing Down / RubyKaigi 2025
k0kubun
2
4k
YJIT Makes Rails 1.7x faster / RubyKaigi 2024
k0kubun
7
15k
YJIT: Dive into Ruby's JIT compiler written in Rust / Rust.Tokyo 2022
k0kubun
2
2.3k
Towards Ruby 4 JIT / RubyKaigi 2022
k0kubun
3
12k
Optimizing Production Performance with MRI JIT / RubyConf 2021
k0kubun
1
540
Other Decks in Programming
See All in Programming
仕様漏れ実装漏れをなくすトレーサビリティAI基盤のご紹介
orgachem
PRO
8
4.9k
PCOVから学ぶコードカバレッジ #phpcon_odawara
o0h
PRO
0
210
夢の無限スパゲッティ製造機 -実装篇- #phpstudy
o0h
PRO
0
190
我々はなぜ「層」を分けるのか〜「関心の分離」と「抽象化」で手に入れる変更に強いシンプルな設計〜 #phperkaigi / PHPerKaigi 2026
shogogg
2
810
AIエージェントで業務改善してみた
taku271
0
480
飯MCP
yusukebe
0
480
Laravel Nightwatchの裏側 - Laravel公式Observabilityツールを支える設計と実装
avosalmon
1
320
Coding at the Speed of Thought: The New Era of Symfony Docker
dunglas
0
4.6k
Codex CLI でつくる、Issue から merge までの開発フロー
amata1219
0
320
L’IA au service des devs : Anatomie d'un assistant de Code Review
toham
0
210
LM Linkで(非力な!)ノートPCでローカルLLM
seosoft
0
410
感情を設計する
ichimichi
5
1.2k
Featured
See All Featured
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.4k
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
670
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.1k
Ten Tips & Tricks for a 🌱 transition
stuffmc
0
95
GraphQLとの向き合い方2022年版
quramy
50
14k
Faster Mobile Websites
deanohume
310
31k
Exploring anti-patterns in Rails
aemeredith
3
310
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.7k
The Curious Case for Waylosing
cassininazir
0
290
Building Adaptive Systems
keathley
44
3k
エンジニアに許された特別な時間の終わり
watany
106
240k
Transcript
Ruby JIT Hacking Guide @k0kubun
@k0kubun
RJIT ERB Haml Slim I maintain
What's JIT?
Ruby JITs MJIT Ruby 2.6
Ruby JITs YJIT Ruby 3.1 MJIT Ruby 2.6
Ruby JITs YJIT Ruby 3.1 MJIT Ruby 2.6 RJIT Ruby
3.3
Railsbench Speedup relative to 2.5 No JIT (s/s) 0 0.5
1 1.5 2 Ruby 2.5 Ruby 2.6 Ruby 2.7 Ruby 3.0 Ruby 3.1 Ruby 3.2 Ruby 3.3 1.93 1.85 1.54 1.59 1.2 1.25 1.08 1.05 1.07 1.26 1.24 1.12 1.09 1.05 1.07 1 No JIT MJIT / RJIT YJIT
Ruby 3.2 YJIT
Ruby 3.3 YJIT
Using YJIT • Install Rust and then build Ruby •
Use --yjit or export RUBY_YJIT_ENABLE=1
Ruby JIT Hacking Guide
Ruby Hacking Guide
Ruby JIT Hacking Guide JIT JIT
How Ruby JIT works Ruby
How Ruby JIT works 1 + 2 Ruby Abstract
Syntax Tree
How Ruby JIT works 1 + 2 putobject 1 putobject
2 opt_plus leave Ruby Abstract Syntax Tree Instruction Sequence (Bytecode)
How Ruby JIT works 1 + 2 putobject 1 putobject
2 opt_plus leave Ruby Abstract Syntax Tree Instruction Sequence (Bytecode) Machine Code
x86_64 assembly
• Read asm comments •--yjit-dump-disasm •--rjit-dump-disasm x86_64 assembly
• mov: assignment instruction • esi: register for stack[0] x86_64
assembly stack[0] = 3
x86_64 assembly • mov: assignment instruction • edi: register for
stack[1] stack[0] = 3 stack[1] = 5
x86_64 assembly • add,sub: arithmetic instruction • rax: temporary register
stack[0] = 3 stack[1] = 5 temp = stack[0] temp -= 1 temp += stack[1]
x86_64 assembly • jo: jump if over fl ow •
rsi: register for stack[0] stack[0] = 3 stack[1] = 5 temp = stack[0] temp -= 1 temp += stack[1] jump if overflow stack[0] = temp
x86_64 encoding
x86_64 encoding opv86: https://hikalium.github.io/opv86/
x86_64 encoding opv86: https://hikalium.github.io/opv86/
Calling a custom JIT Ruby 3.2 Ruby 3.3+ 1. Run
Ruby with --mjit=pause --rjit=pause 2. Override RubyVM::MJIT.compile RubyVM::RJIT#compile 3. Call RubyVM::MJIT.resume RubyVM::RJIT.resume
Building JIT is fun
k0kubun/ruby-jit-challenge Ruby JIT Challenge
Ruby JIT Challenge Hashtag: #ruby-jit-challenge Speedup relative to No JIT
(s/s) 0 3 6 9 12 No JIT RJIT YJIT Ruby JIT 11.08 6.31 3.75 1 Fibonatti benchmark
Optimizing Ruby JIT
Side exits side exit
Method rede fi nition Rede fi nition Hook Invalidate
Method rede fi nition Rede fi nition Hook Invalidate side
exit
Constant rede fi nition
Register allocation: Stack 0: rsi 1: rdi 4: r10 2:
r8 3: r9 VM stack
Register allocation: Stack 0: rsi 1: rdi 4: r10 2:
r8 3: r9 VM stack
Register allocation: Local variables • Spill registers on C function
calls • Binding • debug_inspector API
Polymorphic method cache
Splitting
Method inlining
Conclusion • Enjoy custom JIT development • Let's make YJIT
the best Ruby JIT