Lock in $30 Savings on PRO—Offer Ends Soon! ⏳
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
2
10k
Ruby JIT Hacking Guide / RubyKaigi 2023
Ruby JIT Hacking Guide
https://rubykaigi.org/2023/
Takashi Kokubun
May 13, 2023
Tweet
Share
More Decks by Takashi Kokubun
See All by Takashi Kokubun
ZJIT: The Ruby 4 JIT Compiler / Ruby Release 30th Anniversary Party
k0kubun
0
9
ZJIT: The Future of Ruby Performance / San Francisco Ruby Conference 2025
k0kubun
1
47
ZJIT: Building a New JIT Compiler for Ruby / REBASE 2025
k0kubun
0
36
Deoptimization: How YJIT Speeds Up Ruby by Slowing Down / RubyKaigi 2025
k0kubun
2
3.6k
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.2k
Towards Ruby 4 JIT / RubyKaigi 2022
k0kubun
3
11k
Optimizing Production Performance with MRI JIT / RubyConf 2021
k0kubun
1
500
Why Ruby's JIT was slow / RubyKaigi Takeout 2021
k0kubun
3
2k
Other Decks in Programming
See All in Programming
大規模Cloud Native環境におけるFalcoの運用
owlinux1000
0
120
実は歴史的なアップデートだと思う AWS Interconnect - multicloud
maroon1st
0
210
Flutter On-device AI로 완성하는 오프라인 앱, 박제창 @DevFest INCHEON 2025
itsmedreamwalker
1
120
ローカルLLMを⽤いてコード補完を⾏う VSCode拡張機能を作ってみた
nearme_tech
PRO
0
100
Navigation 3: 적응형 UI를 위한 앱 탐색
fornewid
1
350
FluorTracer / RayTracingCamp11
kugimasa
0
240
非同期処理の迷宮を抜ける: 初学者がつまづく構造的な原因
pd1xx
1
730
Giselleで作るAI QAアシスタント 〜 Pull Requestレビューに継続的QAを
codenote
0
230
LLM Çağında Backend Olmak: 10 Milyon Prompt'u Milisaniyede Sorgulamak
selcukusta
0
120
「コードは上から下へ読むのが一番」と思った時に、思い出してほしい話
panda728
PRO
38
26k
俺流レスポンシブコーディング 2025
tak_dcxi
14
8.9k
令和最新版Android Studioで化石デバイス向けアプリを作る
arkw
0
410
Featured
See All Featured
[SF Ruby Conf 2025] Rails X
palkan
0
540
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.6k
Large-scale JavaScript Application Architecture
addyosmani
515
110k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
1.8k
A Modern Web Designer's Workflow
chriscoyier
698
190k
A better future with KSS
kneath
240
18k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3k
KATA
mclloyd
PRO
33
15k
Java REST API Framework Comparison - PWX 2021
mraible
34
9k
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.5k
A designer walks into a library…
pauljervisheath
210
24k
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