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 3.0 JIT on Rails
Search
Takashi Kokubun
July 03, 2020
Programming
9
9.1k
Ruby 3.0 JIT on Rails
https://ginza-rails.connpass.com/event/178203/
Takashi Kokubun
July 03, 2020
Tweet
Share
More Decks by Takashi Kokubun
See All by Takashi Kokubun
YJIT Makes Rails 1.7x faster / RubyKaigi 2024
k0kubun
7
12k
Ruby JIT Hacking Guide / RubyKaigi 2023
k0kubun
2
9.4k
YJIT: Dive into Ruby's JIT compiler written in Rust / Rust.Tokyo 2022
k0kubun
1
2k
Towards Ruby 4 JIT / RubyKaigi 2022
k0kubun
3
11k
Optimizing Production Performance with MRI JIT / RubyConf 2021
k0kubun
1
400
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
760
JIT ロードマップ / Ruby 3 さみっと
k0kubun
2
1.4k
Other Decks in Programming
See All in Programming
Duckdb-Wasmでローカルダッシュボードを作ってみた
nkforwork
0
120
ECS Service Connectのこれまでのアップデートと今後のRoadmapを見てみる
tkikuc
2
250
リアーキテクチャxDDD 1年間の取り組みと進化
hsawaji
1
220
Tauriでネイティブアプリを作りたい
tsucchinoko
0
370
よくできたテンプレート言語として TypeScript + JSX を利用する試み / Using TypeScript + JSX outside of Web Frontend #TSKaigiKansai
izumin5210
6
1.7k
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
0
100
エンジニアとして関わる要件と仕様(公開用)
murabayashi
0
280
現場で役立つモデリング 超入門
masuda220
PRO
15
3.2k
WebフロントエンドにおけるGraphQL(あるいはバックエンドのAPI)との向き合い方 / #241106_plk_frontend
izumin5210
4
1.4k
Pinia Colada が実現するスマートな非同期処理
naokihaba
4
220
ヤプリ新卒SREの オンボーディング
masaki12
0
130
CSC509 Lecture 09
javiergs
PRO
0
140
Featured
See All Featured
Building a Modern Day E-commerce SEO Strategy
aleyda
38
6.9k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
How STYLIGHT went responsive
nonsquared
95
5.2k
Designing for Performance
lara
604
68k
Six Lessons from altMBA
skipperchong
27
3.5k
Designing on Purpose - Digital PM Summit 2013
jponch
115
7k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
27
4.3k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
6
410
Fashionably flexible responsive web design (full day workshop)
malarkey
405
65k
Transcript
Ruby 3.0 JIT on Rails ۜ࠲Rails#23 Takashi Kokubun / @k0kubun
ࣗݾհ • GitHub, Twitter: @k0kubun • Arm Treasure Data •
Backend, SRE, API • Ruby ίϛολ • JIT, VM, ERB, Πϯϑϥ, ϕϯνϚʔΫ
None
એ
એ
એ
ࠓ͢͜ͱ • Ruby 3.0 JIT ͷݱࡏͷੑೳ • Rails ͰͷϘτϧωοΫͷվળ •
C Λ Ruby ʹॻ͖͑ͯΔ • ࠓޙͷ JIT ΞʔΩςΫνϟ
Ruby 3.0 JIT ͷݱࡏͷੑೳ
mame/optcarrot 3VCZ 3VCZ 3VCZ GSBNFTTFD
7. +*5
benchmark-driver/sinatra 3VCZ 3VCZ 3VCZ SFRVFTUTTFD
7. +*5
k0kubun/railsbench 3VCZ 3VCZ 3VCZ SFRVFTUTTFD
7. +*5
͜Ε·Ͱͷੑೳվળͷ֓ཁ • 2.6: Optcarrot্ݱঢ়͜ͷόʔδϣϯͷΈɻJIT compactionRailsʹޮ͘ɻ • 2.7: Ұ෦ϝιουͷΠϯϥΠϯԽɻmax cacheσϑΥϧτมߋҰԠRailsʹޮ͘ɻ •
3.0: builtinϝιουͷΠϯϥΠϯԽɻRails͚ͷ࠷దԽ͕͍͔ͭ͘ (͜Ε͔Βઆ໌)
Rails ͰͷϘτϧωοΫͷվળ
Optcarrot ͱ Sinatra, Rails ͷҧ͍? • طͷࣄ࣮: Optcarrot͕͘Ͱ͖ΔͷΠϯελϯεมͷ࠷దԽ͕ Α͘ޮͨ͘Ί •
͔͠͠ɺSinatraRails͕͘ͳΔͷԿނ͔? • CPUͷͲͷϨΠϠʔͰϘτϧωοΫʹͳ͍ͬͯΔ?
VTune: mame/optcarrot - VM • VTune: VM, JIT
VTune: mame/optcarrot - JIT • VTune: VM, JIT
VTune: benchmark-driver/sinatra - VM
VTune: benchmark-driver/sinatra - JIT
VTune: k0kubun/railsbench - VM
VTune: k0kubun/railsbench - JIT
ϑϩϯτΤϯυͷׂ߹͕૿͍͑ͯΔ • ໋ྩͷ࣮ߦͰͳ͘ɺ໋ྩͷfetchͰstall͍ͯ͠Δ • CPU 1αΠΫϧ͋ͨΓʹ࣮ߦ͞ΕΔ໋ྩ͕গͳ͍ • VMͷ࣌ͰOptcarrotͱ͕ࠩ͋Δ͕ɺJITͰѱԽ͍ͯ͠Δͷ ICache Miss
Instruction Cache (ICache) Miss ͱ • Α͋͘ΔCPUͷΩϟογϡ: L1i, L1d, L2,
L3 (shared) • L1i: Level 1 Instruction Ωϟογϡ • CPU໋ྩͷΩϟογϡϛε • ಉ࣌ʹΞΫηε͢Δίʔυ͕ผͷcachelineϖʔδʹ͋ΔͳͲ (ίʔυͷαΠζ͔ϨΠΞτͷ)
ICache Miss ΛͲ͏ճආ͢Δ͔ • ίʔυαΠζͷݮ • ॏෳΛഉআ͢Δ • ࣮ߦ࣌ใʹج͍ͮͯෆཁͳύεΛΔ •
ϨΠΞτͷվળ • ϝιου͝ͱʹผϖʔδʹόϥόϥͳͷ2.6 JIT compactionͰղܾ • hot path ͷہॴੑΛ্͛Δ
ॏෳΛഉআ͢Δ • 3.0 ʹೖͬͨ࠷దԽ: • JIT compactionͰɺ.o Ͱͳ͘.c͔ΒίϯύΠϧ͢͠ (static͕ؔ dedup͞ΕΔ)
• ivar͕࠷దԽ͞Εͳ͔ͬͨ࣌ʹΘΕΔؔͷΠϯϥΠϯԽΛΊΔ • JIT cancel࣌ͷෳͷؔݺͼग़͠Λ1ͭʹ·ͱΊΔ
࣮ߦ࣌ใʹج͍ͮͯෆཁͳύεΛΔ • 3.0 ʹೖͬͨ࠷దԽ: • ϝιουݺͼग़͠ͰϨγʔόͷΫϥε͕ඇଈͳΒɺଈ͚ͷ ذΛআڈ͢Δ
hot path ͷہॴੑΛ্͛Δ • 3.0 ʹೖͬͨ࠷దԽ: • ϝιου୳ࡧͳͲͷύεʹcold attributeΛ͚ͭΔ •
stackoverflow࣌coldԽ • JITίϯύΠϧͷenqueuecoldԽ
͋ͱͲ͏͢Δ͔ • ·ͩ͘ͳΔݪҼʹͳͬͯͦ͏ͩ͠ɺॏෳ͋Γͦ͏ • ͔͠͠୯ʹΠϯϥΠϯԽΛແޮԽ͢ΔͱͦΕͰ͘ͳΔ͜ͱ͋Δ ͷͰɺ͜ΕҎ্৻ॏʹΔඞཁ͕͋Δ • ϝιου͋ͨΓͷίʔυαΠζ͕ҰؾʹݮΕɺJIT͍͍ͯ͠࠷େϝ ιου૿ͤΔ͔
C Λ Ruby ʹॻ͖͑ͯΔ
RubyKaigi 2018
RubyKaigi 2019
None
None
Builtin ϝιουͱ • RubyίΞͷ࣮Ͱ͔͑͠ͳ͍ػೳ • ISeqͷόΠφϦঢ়ଶͰอଘ͞Ε͓ͯΓɺϩʔυίετ͕͍ • ΩʔϫʔυҾΛऔΔϝιουɺRubyͰ࣮ͨ͠ํ͕ߴ • RubyͰϝιουΛఆٛ͢Δ͕ɺCͷ͕ؔίετͰݺͼग़ͤΔ
Builtin ϝιουͱ
Builtin attr ͱ • LLVMͳͲͳ͠ͰCͷؔͷڍಈΛղੳ͢Δͷ͍͠ • ͦ͜ͰɺϝιουͷಛੑΛΞϊςʔγϣϯ͓͖ͯ͠ɺJITͦΕΛ৴͡Δ • RubyͷCIͳͲͰΞϊςʔγϣϯ͕ਖ਼͍͔࣮͠ߦ࣌ʹݕ͍ࠪͯ͠Δ
Builtin attr ͷҰཡ • inline: ϝιουϑϨʔϜΛpush͠ͳͯ͘ྑ͍ • ݱঢ়͜Ε͚ͩɻJIT͕ϝιουϑϨʔϜͷpushΛεΩοϓ͍ͯ͠Δ
Builtin attr inline Λ͚ͭΔྫ
ϝιουϑϨʔϜεΩοϓͷޮՌ ,FSOFMDMBTT JT 7. +*5
+*5 GSBNFTLJQ 1.3x 1.7x
ϝιουϑϨʔϜεΩοϓʹҙຯ͋Δͷ͔? • Railsϝιουݺͼग़͕͠ଟͦ͏ • ͨͩ͠ɺ͋Δϝιου͋ͨΓͷݺͼग़͠ճͦΕ΄Ͳଟ͘ͳ͘ɺ ෆར • Cͷؔఆٛ͝ͱΠϯϥΠϯԽͨ࣌͠ʹ࠷దԽ࣌ͷϊΠζ͕ݮΔ • 1ճ͋ͨΓ
4ns / 16 clocks ఔޮՌ͕ݟΒΕΔ
ϝιουϑϨʔϜεΩοϓͷ݅ • ϝιουݺͼग़͠Λ͠ͳ͍ • ྫ֎Λ͛ͳ͍ • TypeError, ArgumentError, NoMemoryError ֘͢Δ
• όοΫτϨʔεΛݟͳ͍
ࠓޙͷ JIT ΞʔΩςΫνϟ
C2 Graal ͕ѻ͍ͬͯΔάϥϑ
MJIT͕͍ͬͯΔ͜ͱ • VMಛ༗ͷΦʔόʔϔουͷআڈ • VM໋ྩ୯ҐͰͷ࣮ߦ࣌ใΛͬͨ࠷దԽ • ϝιουͷΠϯϥΠϯԽ • Γͷ࠷దԽCίϯύΠϥͤ
JITʹظ͢Δ͜ͱ • ίϯύΠϧ࣌ܭࢉ • Constant folding • Dead code elimination
• Loop invariant motion • ϝϞϦΞΫηεͷ࠷దԽ
MJIT ͷݶք • CίϯύΠϥ Ruby VM ಛ༗ͷࣄΛߟྀͰ͖ͳ͍ • ΠϯϥΠϯԽͷൣғʹݶք͕͋Δ (C
ϝιου) • Γ͗͢ΔͱίʔυαΠζ͕૿͑ٯޮՌ • ਖ਼ৗܥΛ༏ઌͯ͠࠷దԽͤ͞Δͱ͍ͬͨࢦ͕͍ࣔ͠
ͱ͍͑ • JIT ແޮͷ࣌ʹ͘ͳΔखஈऔΓͨ͘ͳ͍ • JITͰ͍ͷ؆୯ͳίϚϯυͳͲͰศར • ϝϯς͕େมʹͳΔखஈऔΓͨ͘ͳ͍ • VM໋ྩ͕૿͑ͯجຊతʹJITಈ͖ଓ͚ͯཉ͍͠
• VMͷڍಈͱશ͘ಠཱͨ͠ίʔυੜͨ͘͠ͳ͍
͡Ό͋Ͳ͏͢Δ͔ 1. ίϯύΠϧ࣌ܭࢉʹඞཁͳͷΛἧ͑Δ 2. ୯७ͳಠࣗIR͔ΒVMʹ࠷దԽ͢Δج൫Λ͑Δ 3. ΑΓߴͳ࠷దԽʹదͨ͠IRߏʹม͑Δ
1. ίϯύΠϧ࣌ܭࢉʹඞཁͳͷΛἧ͑Δ • CϝιουͷΞϊςʔγϣϯΛ૿͢ (Τεέʔϓ͢Δ͔ɺ७ਮ͔) • ࣮ߦ࣌ͷܕใ͕ΑΓଟ͘ͷॴͰऔΕΔΑ͏ʹ͢Δ (opt໋ྩͳͲ) • Cϝιου,
super, yield, ఆ ͳͲͷΠϯϥΠϯԽରԠΛਐΊΔ
2. ୯७ͳಠࣗIR͔ΒVMʹ࠷దԽ͢Δج൫ • YARV໋ྩΛJITಠࣗͷIRʹղՄೳʹ͠ɺ໋ྩ/IRྻʹ࠷దԽΛ͔͚Δ • ࠷దԽͨ͠ޙͷ໋ྩ/IRྻͰ࠷దԽ͕ൃੜ͢Δҙͷॴ͔ΒVM ࣮ߦʹͤΔঢ়ଶΛҡ࣋͢Δ • On-Stack Replacement
(JIT → VM) ͦͷ͏࣮ͪ
3. ΑΓߴͳ࠷దԽʹదͨ͠IRߏʹม͑Δ • ࠷దԽͷج൫͕ͬͨΒɺͦΕΛҡ࣋͠ͳ͕Β࠷దԽʹదͨ͠IRά ϥϑʹม͍͑ͯ͘ • ίϯύΠϥRuby (on Ractor) Ͱ࣮͍ͨ͠
ߴͳ࠷దԽͷྫ (ࠓCͰॻ͍͍ͯΔ) • ෳ໋ྩؒͰॏෳ͍ͯ͠ΔܕνΣοΫͷϚʔδ (ࠓॻ͍ͨ) • Optcarrot fps: 83.3 →
93.0
ଞʹΓ͍ͨͱࢥ͍ͬͯΔ͜ͱ • ෭࡞༻ͷͳ͍ϝιουͷίϯύΠϧ࣌ݺͼग़͠ • [1, 2].min ͰΞϊςʔγϣϯʹج͍ͯ rb_ary_min ΛίϯύΠϧ࣌ʹݺͼग़͢ •
Τεέʔϓ͠ͳ͍ΦϒδΣΫτͷࣄલׂΓͯ • ҎલStringΛελοΫׂ͢ΔPoCΛ࡞͕ͬͨɺͦͦΞϩέʔγϣϯ͕ͳ ͯ͘ࡁΉ߹ͦΕ͕࠷ • Frozen string literal ͳͲΛෆཁʹ͍ͨ͠
·ͱΊ • ࠓJITʹΑΔ࠷దԽ͕ࠔͳͱ͜ΖͷճΓΛ͍͑ͯΔஈ֊ • Rails͕͘Ͱ͖Δ·ͰͷಓͷΓ͍͕ɺ3.0Ͱண࣮ʹਐḿ͍ͯ͠Δ