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
9k
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
11k
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
740
JIT ロードマップ / Ruby 3 さみっと
k0kubun
2
1.4k
Other Decks in Programming
See All in Programming
Vue :: Better Testing 2024
up1
1
400
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
1.2k
Integrating AI in Your Enterprise Java Applications
ivargrimstad
0
160
自分だけの世界を創るクリエイティブコーディング / Creative Coding: Creating Your Own World
chobishiba
2
1.4k
Micro Frontends for Java Microservices - dev2next 2024
mraible
PRO
0
210
CSC509 Lecture 03
javiergs
PRO
0
140
Pythonによるイベントソーシングへの挑戦と現状に対する考察 / Challenging Event Sourcing with Python and Reflections on the Current State
nrslib
3
1.3k
Modern Functional Fluent CFML REST by Luis Majano
ortus24
0
140
個人開発で使ってるやつを紹介する回
yohfee
1
700
データフレームライブラリ徹底比較
daikikatsuragawa
2
100
複数プロダクトの技術改善・クラウド移行に向き合うチームのフレキシブルなペア・モブプログラミングの実践 / Flexible Pair Programming And Mob Programming
honyanya
0
230
CSC509 Lecture 04
javiergs
PRO
0
150
Featured
See All Featured
Happy Clients
brianwarren
97
6.7k
KATA
mclloyd
27
13k
The Mythical Team-Month
searls
218
43k
Building Applications with DynamoDB
mza
90
6k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
3
230
Art, The Web, and Tiny UX
lynnandtonic
296
20k
[RailsConf 2023] Rails as a piece of cake
palkan
49
4.7k
In The Pink: A Labor of Love
frogandcode
139
22k
Unsuck your backbone
ammeep
668
57k
Designing the Hi-DPI Web
ddemaree
280
34k
For a Future-Friendly Web
brad_frost
174
9.3k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
327
21k
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Ͱண࣮ʹਐḿ͍ͯ͠Δ