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
Lightning-Fast Method Calls with Ruby 4.1 ZJIT / RubyKaigi 2026
k0kubun
3
350
一度始めたらやめられない開発効率向上術 / Findy あなたのdotfilesを教えて!
k0kubun
4
3k
ZJIT: The Ruby 4 JIT Compiler / Ruby Release 30th Anniversary Party
k0kubun
1
470
ZJIT: The Future of Ruby Performance / San Francisco Ruby Conference 2025
k0kubun
1
100
ZJIT: Building a New JIT Compiler for Ruby / REBASE 2025
k0kubun
0
110
Deoptimization: How YJIT Speeds Up Ruby by Slowing Down / RubyKaigi 2025
k0kubun
2
4.1k
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
Other Decks in Programming
See All in Programming
의존성 주입과 모듈화
fornewid
0
140
ハーネスエンジニアリングとは?
kinopeee
10
5k
煩雑なSkills管理をSoC(関心の分離)により解決する――関心を分離し、プロンプトを部品として育てるためのOSSを作った話 / Solving Complex Skills Management Through SoC (Separation of Concerns)
nrslib
4
930
「速くなった気がする」をデータで疑う
senleaf24
0
180
書籍「ユーザーストーリーマッピング」が私のバイブル
asumikam
3
340
Oxlintとeslint-plugin-react-hooks 明日から始められそう?
t6adev
0
270
Claude Code × Gemini × Ebitengine ゲーム制作素人WebエンジニアがGoでゲームを作った話
webzawa
0
130
How We Benchmarked Quarkus: Patterns and anti-patterns
hollycummins
1
140
ソフトウェア設計の結合バランス #phperkaigi
kajitack
0
110
PHPで TLSのプロトコルを実装してみるをもう一度しゃべりたい
higaki_program
0
200
Reactive ❤️ Loom: A Forbidden Love Story
franz1981
2
240
感情を設計する
ichimichi
5
1.5k
Featured
See All Featured
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
2
3.8k
The Limits of Empathy - UXLibs8
cassininazir
1
300
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
2.9k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.3k
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
160
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
2.8k
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
1
350
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.7k
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
320
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
The Mindset for Success: Future Career Progression
greggifford
PRO
0
310
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
360
30k
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