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
2
9.6k
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
YJIT Makes Rails 1.7x faster / RubyKaigi 2024
k0kubun
7
13k
YJIT: Dive into Ruby's JIT compiler written in Rust / Rust.Tokyo 2022
k0kubun
1
2.1k
Towards Ruby 4 JIT / RubyKaigi 2022
k0kubun
3
11k
Optimizing Production Performance with MRI JIT / RubyConf 2021
k0kubun
1
430
Why Ruby's JIT was slow / RubyKaigi Takeout 2021
k0kubun
3
1.9k
数時間かかる週一リリースを毎日何度も爆速でできるようにするまで / CI/CD Conference 2021
k0kubun
21
14k
Ruby 3 JIT's roadmap / RubyConf China 2020
k0kubun
0
790
Ruby 3.0 JIT on Rails
k0kubun
9
9.2k
JIT ロードマップ / Ruby 3 さみっと
k0kubun
2
1.4k
Other Decks in Programming
See All in Programming
PHP ステートレス VS ステートフル 状態管理と並行性 / php-stateless-stateful
ytake
0
100
一休.com のログイン体験を支える技術 〜Web Components x Vue.js 活用事例と最適化について〜
atsumim
0
510
さいきょうのレイヤードアーキテクチャについて考えてみた
yahiru
3
750
動作確認やテストで漏れがちな観点3選
starfish719
6
1k
Domain-Driven Transformation
hschwentner
2
1.9k
SwiftUIで単方向アーキテクチャを導入して得られた成果
takuyaosawa
0
270
法律の脱レガシーに学ぶフロントエンド刷新
oguemon
5
740
Spring gRPC について / About Spring gRPC
mackey0225
0
220
『テスト書いた方が開発が早いじゃん』を解き明かす #phpcon_nagoya
o0h
PRO
2
270
Immutable ActiveRecord
megane42
0
140
データの整合性を保つ非同期処理アーキテクチャパターン / Async Architecture Patterns
mokuo
47
17k
Amazon ECS とマイクロサービスから考えるシステム構成
hiyanger
2
560
Featured
See All Featured
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
100
18k
Navigating Team Friction
lara
183
15k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
12
960
YesSQL, Process and Tooling at Scale
rocio
172
14k
Thoughts on Productivity
jonyablonski
69
4.5k
GraphQLとの向き合い方2022年版
quramy
44
13k
The World Runs on Bad Software
bkeepers
PRO
67
11k
StorybookのUI Testing Handbookを読んだ
zakiyama
28
5.5k
A better future with KSS
kneath
238
17k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
Writing Fast Ruby
sferik
628
61k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
133
33k
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