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
410
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
Hotwire or React? ~アフタートーク・本編に含めなかった話~ / Hotwire or React? after talk
harunatsujita
1
120
聞き手から登壇者へ: RubyKaigi2024 LTでの初挑戦が 教えてくれた、可能性の星
mikik0
1
130
Better Code Design in PHP
afilina
PRO
0
130
Tauriでネイティブアプリを作りたい
tsucchinoko
0
370
C++でシェーダを書く
fadis
6
4.1k
初めてDefinitelyTypedにPRを出した話
syumai
0
420
Streams APIとTCPフロー制御 / Web Streams API and TCP flow control
tasshi
2
350
距離関数を極める! / SESSIONS 2024
gam0022
0
280
Pinia Colada が実現するスマートな非同期処理
naokihaba
4
230
Kaigi on Rails 2024 〜運営の裏側〜
krpk1900
1
230
受け取る人から提供する人になるということ
little_rubyist
0
230
【Kaigi on Rails 2024】YOUTRUST スポンサーLT
krpk1900
1
330
Featured
See All Featured
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
6
410
Keith and Marios Guide to Fast Websites
keithpitt
409
22k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
47
2.1k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
Faster Mobile Websites
deanohume
305
30k
Side Projects
sachag
452
42k
Building a Scalable Design System with Sketch
lauravandoore
459
33k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
How to train your dragon (web standard)
notwaldorf
88
5.7k
Docker and Python
trallard
40
3.1k
Imperfection Machines: The Place of Print at Facebook
scottboms
265
13k
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Ͱண࣮ʹਐḿ͍ͯ͠Δ