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
13k
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
770
JIT ロードマップ / Ruby 3 さみっと
k0kubun
2
1.4k
Other Decks in Programming
See All in Programming
第5回日本眼科AI学会総会_AIコンテスト_3位解法
neilsaw
0
170
Keeping it Ruby: Why Your Product Needs a Ruby SDK - RubyWorld 2024
envek
0
180
ゆるやかにgolangci-lintのルールを強くする / Kyoto.go #56
utgwkk
1
350
StarlingMonkeyを触ってみた話 - 2024冬
syumai
3
270
talk-with-local-llm-with-web-streams-api
kbaba1001
0
170
クリエイティブコーディングとRuby学習 / Creative Coding and Learning Ruby
chobishiba
0
3.9k
複雑な仕様に立ち向かうアーキテクチャ
myohei
0
170
競技プログラミングへのお誘い@阪大BOOSTセミナー
kotamanegi
0
350
선언형 UI에서의 상태관리
l2hyunwoo
0
130
Haze - Real time background blurring
chrisbanes
1
500
わたしの星のままで一番星になる ~ 出産を機にSIerからEC事業会社に転職した話 ~
kimura_m_29
0
180
htmxって知っていますか?次世代のHTML
hiro_ghap1
0
330
Featured
See All Featured
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
48
2.2k
A designer walks into a library…
pauljervisheath
204
24k
Become a Pro
speakerdeck
PRO
26
5k
Product Roadmaps are Hard
iamctodd
PRO
49
11k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
For a Future-Friendly Web
brad_frost
175
9.4k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
32
2.7k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
Speed Design
sergeychernyshev
25
670
A Philosophy of Restraint
colly
203
16k
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Ͱண࣮ʹਐḿ͍ͯ͠Δ