$30 off During Our Annual Pro Sale. View Details »
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.5k
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
物流システムにおけるリファクタリングとアーキテクチャの再構築 〜依存関係とモジュール分割の重要性〜
deeprain
1
250
「今のプロジェクトいろいろ大変なんですよ、app/services とかもあって……」/After Kaigi on Rails 2024 LT Night
junk0612
5
2.2k
5分ぐらいで分かる、トリミング機能の作り方
tsutsuitakumi
0
140
Cursorでアプリケーションの追加開発や保守をどこまでできるか試したら得るものが多かった話
drumnistnakano
0
230
カンファレンスの「アレ」Webでなんとかしませんか? / Conference “thing” Why don't you do something about it on the Web?
dero1to
1
150
Laravel や Symfony で手っ取り早く OpenAPI のドキュメントを作成する
azuki
2
140
大規模サイトリビルドの現場から:成功と失敗のリアルな教訓 / Site Rebuild,Real Lessons Learned from Successes and Failures_JJUG Fall 2024
techtekt
0
190
[Do iOS '24] Ship your app on a Friday...and enjoy your weekend!
polpielladev
0
200
距離関数を極める! / SESSIONS 2024
gam0022
0
340
flutterkaigi_2024.pdf
kyoheig3
0
350
layerx_20241129.pdf
kyoheig3
1
180
「天気予報があなたに届けられるまで」 - NIFTY Tech Talk #22
niftycorp
PRO
0
130
Featured
See All Featured
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
KATA
mclloyd
29
14k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Building Your Own Lightsaber
phodgson
103
6.1k
Faster Mobile Websites
deanohume
305
30k
The Cult of Friendly URLs
andyhume
78
6.1k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
6
470
Adopting Sorbet at Scale
ufuk
73
9.1k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
The Pragmatic Product Professional
lauravandoore
32
6.3k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
28
2k
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Ͱண࣮ʹਐḿ͍ͯ͠Δ