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
Takashi Kokubun
September 08, 2022
Programming
3
11k
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
YJIT Makes Rails 1.7x faster / RubyKaigi 2024
k0kubun
7
10k
Ruby JIT Hacking Guide / RubyKaigi 2023
k0kubun
2
9.3k
YJIT: Dive into Ruby's JIT compiler written in Rust / Rust.Tokyo 2022
k0kubun
1
1.9k
Optimizing Production Performance with MRI JIT / RubyConf 2021
k0kubun
1
390
Why Ruby's JIT was slow / RubyKaigi Takeout 2021
k0kubun
3
1.8k
数時間かかる週一リリースを毎日何度も爆速でできるようにするまで / CI/CD Conference 2021
k0kubun
21
14k
Ruby 3 JIT's roadmap / RubyConf China 2020
k0kubun
0
730
Ruby 3.0 JIT on Rails
k0kubun
9
9k
JIT ロードマップ / Ruby 3 さみっと
k0kubun
2
1.3k
Other Decks in Programming
See All in Programming
dRuby 入門者によるあなたの身近にあるdRuby 入門
makicamel
4
340
What we keep in mind when migrating from Serverless Framework to AWS CDK and AWS SAM
kasacchiful
1
130
オートマトン学習しろ / Do automata learning
makenowjust
3
100
全力の跳躍を捉える計測アプリを作る
ogijun2018
0
1k
現代のVueとTypeScript - 型安全の活用術
minako__ph
4
3.1k
rbs-inlineを導入してYARDからRBSに移行する
euglena1215
1
200
iOSDC 2024 SMBファイル共有をSwiftで実装する
kishikawakatsumi
1
110
Hono・Prisma・AWSでGeoなAPI開発
nokonoko1203
5
610
ECMAScript仕様を読むのに必要な知識 - ダイジェスト版
syumai
4
2.5k
仮想ファイルシステムを導入して開発環境のストレージ課題を解消する
segadevtech
2
390
LangGraphでのHuman-in-the-Loopの実装
os1ma
3
550
詳解UIWindow
natmark
3
2k
Featured
See All Featured
[RailsConf 2023] Rails as a piece of cake
palkan
44
4.6k
Optimising Largest Contentful Paint
csswizardry
28
2.7k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
662
120k
Creatively Recalculating Your Daily Design Routine
revolveconf
215
12k
Writing Fast Ruby
sferik
623
60k
A better future with KSS
kneath
235
17k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
28
2.2k
ParisWeb 2013: Learning to Love: Crash Course in Emotional UX Design
dotmariusz
109
6.9k
What’s in a name? Adding method to the madness
productmarketing
PRO
21
3k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
27
7.4k
Principles of Awesome APIs and How to Build Them.
keavy
125
16k
Faster Mobile Websites
deanohume
304
30k
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