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
Optimizing Ruby with JIT
Search
Takashi Kokubun
August 30, 2019
Programming
13
13k
Optimizing Ruby with JIT
Builderscon 2019
http://builderscon.io/builderscon/tokyo/2019
Takashi Kokubun
August 30, 2019
Tweet
Share
More Decks by Takashi Kokubun
See All by Takashi Kokubun
YJIT Makes Rails 1.7x faster / RubyKaigi 2024
k0kubun
7
10k
Ruby JIT Hacking Guide / RubyKaigi 2023
k0kubun
2
9.3k
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
390
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
730
Ruby 3.0 JIT on Rails
k0kubun
9
9k
Other Decks in Programming
See All in Programming
LangGraphでのHuman-in-the-Loopの実装
os1ma
3
970
大公開!iOS開発の悩みトップ5 〜iOSDC Japan 2024〜
ryunakayama
0
190
The Sequel to a Dream of Ruby Parser's Grammar
ydah
1
200
令和トラベルにおけるLLM活用事例:社内ツール開発から得た学びと実践
ippo012
0
120
RAGの回答精度評価用のQAデータセットを生成AIに作らせた話
kurahara
0
240
Scala におけるコンパイラエラーとの付き合い方
chencmd
2
390
今インフラ技術をイチから学び直すなら
yuhta28
1
120
ECMAScript、Web標準の型はどう管理されているか / How ECMAScript and Web standards types are maintained
petamoriken
3
380
rbs-inlineを導入してYARDからRBSに移行する
euglena1215
1
230
ブラウザ互換の重要性 - あらゆるユーザーに価値を届けるために必要なこと
yamanoku
0
110
The Shape of a Service Object
inem
0
410
Architecture Decision Record (ADR)
nearme_tech
PRO
1
650
Featured
See All Featured
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
29
2.6k
Robots, Beer and Maslow
schacon
PRO
157
8.1k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
43
2k
Optimising Largest Contentful Paint
csswizardry
29
2.8k
Statistics for Hackers
jakevdp
793
220k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
32k
Intergalactic Javascript Robots from Outer Space
tanoku
268
26k
Happy Clients
brianwarren
96
6.6k
Building Your Own Lightsaber
phodgson
101
6k
From Idea to $5000 a Month in 5 Months
shpigford
379
46k
Typedesign – Prime Four
hannesfritz
39
2.3k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
190
16k
Transcript
Optimizing Ruby with JIT Builderscon 2019 / Takashi Kokubun ࠷ͷݴޠΛࢦͯ͠
None
Ruby 2.6 ͔ΒJITΛ։ൃ
Γ·ͨ͠ [Misc #16094]
None
(࠷దԽࢹͰͷ) Rubyͷಛ
1. ϝιουఆ࠶ఆٛ͠์ 1 + 1 #=> 100
1. ϝιουఆ࠶ఆٛ͠์ 1 + 1 #=> 100
1. ϝιουఆ࠶ఆٛ͠์ 1 + 1 #=> 100
2. ߦ͕ਐΉͱҙͷ͜ͱ͕ى͖Δ a = 1 + 1
2. ߦ͕ਐΉͱҙͷ͜ͱ͕ى͖Δ a = 1 + 1 a #=> 2
2. ߦ͕ਐΉͱҙͷ͜ͱ͕ى͖Δ a = 1 + 1 a #=> 2
a #=> 100 !?
2. ߦ͕ਐΉͱҙͷ͜ͱ͕ى͖Δ
2. ߦ͕ਐΉͱҙͷ͜ͱ͕ى͖Δ ʮ3ߦʹୡͨ͠Β a = 100ʯ
3. ଞʹָ͍͠ػೳ͕͍ͬͺ͍ • eval • ଋറڥͷΦϒδΣΫτԽ (binding, Proc) • ҙΦϒδΣΫτͷྻڍػೳ
(ObjectSpace) • Φʔόʔϑϩʔ͢Δͱܕ͕มΘΔ • ΞυϨεมΘΓ์ͷώʔϓྖҬ (GC.compact)
ຊͷΰʔϧ ͜ͷ࠷దԽ͕ࠔͳݴޠΛ ݶք·ͰߴԽ͢Δ ָ͠͞Λମݧ͍ͯͨͩ͘͠
RubyΠϯλϓϦλͷ͘͠Έ
1. RubyͷίʔυΛಡΈࠐΉ
2. ύʔεͯ͠ߏจʹ͢Δ EFGUXP
3. ίϯύΠϧ໋ͯ͠ྩྻʹ͢Δ EFGUXP UXP QVUPCKFDU QVUPCKFDU TFOE MFBWF
4. VMͰ໋ྩྻΛ࣮ߦ͢Δ EFGUXP UXP QVUPCKFDU QVUPCKFDU TFOE MFBWF
7JSUVBM.BDIJOF ελοΫ
4. VMͰ໋ྩྻΛ࣮ߦ͢Δ EFGUXP UXP QVUPCKFDU QVUPCKFDU TFOE MFBWF
7JSUVBM.BDIJOF ελοΫ
4. VMͰ໋ྩྻΛ࣮ߦ͢Δ EFGUXP UXP QVUPCKFDU TFOE MFBWF 7JSUVBM.BDIJOF
ελοΫ
4. VMͰ໋ྩྻΛ࣮ߦ͢Δ EFGUXP UXP TFOE MFBWF 7JSUVBM.BDIJOF ελοΫ
4. VMͰ໋ྩྻΛ࣮ߦ͢Δ EFGUXP UXP MFBWF 7JSUVBM.BDIJOF ελοΫ
͜ͷϑΣʔζͷڍಈΛ͍ͨ͘͠ EFGUXP UXP QVUPCKFDU QVUPCKFDU TFOE MFBWF 7JSUVBM.BDIJOF
ελοΫ
5. JIT ίϯύΠϥͰߴԽ͢Δ UXP QVUPCKFDU QVUPCKFDU TFOE MFBWF 7JSUVBM.BDIJOF ελοΫ
+*5
5. JIT ίϯύΠϥͰߴԽ͢Δ UXP QVUPCKFDU QVUPCKFDU TFOE MFBWF 7JSUVBM.BDIJOF ελοΫ
+*5 ࠓͷͱ͜Ζ *OUFHFS ࠶ఆٛ͞Εͯͳ͍ͳ
5. JIT ίϯύΠϥͰߴԽ͢Δ UXP QVUPCKFDU QVUPCKFDU TFOE MFBWF 7JSUVBM.BDIJOF ελοΫ
+*5 ͳͷͰ
5. JIT ίϯύΠϥͰߴԽ͢Δ UXP QVUPCKFDU QVUPCKFDU TFOE MFBWF 7JSUVBM.BDIJOF ελοΫ
+*5 ͍ɺ ͕࠶ఆٛ ͞ΕͨΒ͔
5. JIT ίϯύΠϥͰߴԽ͢Δ UXP QVUPCKFDU QVUPCKFDU TFOE MFBWF 7JSUVBM.BDIJOF ελοΫ
+*5
5. JIT ίϯύΠϥͰߴԽ͢Δ UXP QVUPCKFDU QVUPCKFDU TFOE MFBWF 7JSUVBM.BDIJOF ελοΫ
+*5
JITͰͲͷ͘Β͍͘ͳΔͷ͔
JITʹ͓͚Δੑೳ্ NES emulator: github.com/mame/optcarrot fps 0 23 46 69 92
2.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 34.2 Details: https://gist.github.com/k0kubun/0337e1f40b146b2cae7b7c8e6a79973a 1.0x Version
JITʹ͓͚Δੑೳ্ NES emulator: github.com/mame/optcarrot fps 0 23 46 69 92
2.0 2.1 2.2 2.3 0.0 0.0 0.0 0.0 39.1 37.9 37.8 34.2 Details: https://gist.github.com/k0kubun/0337e1f40b146b2cae7b7c8e6a79973a 1.1x Version
JITʹ͓͚Δੑೳ্ NES emulator: github.com/mame/optcarrot fps 0 23 46 69 92
2.0 2.1 2.2 2.3 2.4 2.5 0.0 0.0 49.0 46.2 39.1 37.9 37.8 34.2 Details: https://gist.github.com/k0kubun/0337e1f40b146b2cae7b7c8e6a79973a 1.4x Version
JITʹ͓͚Δੑೳ্ NES emulator: github.com/mame/optcarrot fps 0 23 46 69 92
2.0 2.1 2.2 2.3 2.4 2.5 2.6 0.0 55.9 49.0 46.2 39.1 37.9 37.8 34.2 Details: https://gist.github.com/k0kubun/0337e1f40b146b2cae7b7c8e6a79973a 1.6x Version
JITʹ͓͚Δੑೳ্ NES emulator: github.com/mame/optcarrot fps 0 23 46 69 92
2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.6 JIT 85.8 55.9 49.0 46.2 39.1 37.9 37.8 34.2 Details: https://gist.github.com/k0kubun/0337e1f40b146b2cae7b7c8e6a79973a 2.5x Version
͜ͷ͕ࠩग़Δཧ༝Λݟ͍͖ͯ·͢ NES emulator: github.com/mame/optcarrot fps 0 23 46 69 92
2.6 2.6 JIT 85.8 55.9 0.0 0.0 0.0 0.0 0.0 0.0 Details: https://gist.github.com/k0kubun/0337e1f40b146b2cae7b7c8e6a79973a Version
JITίϯύΠϥͲͷΑ͏ʹ ߴԽΛ͢Δ͔?
JITͷجૅ0 ػցޠʹ͚ͨͩ͠Ͱ ͘ͳΒͳ͍
VM࣮ߦ → ػցޠ ͰͷੑೳมԽ NES emulator: github.com/mame/optcarrot fps 0 23
46 69 92 2.6 native 2.6 JIT 85.8 55.9 ? (-O3 w/ VM໋ྩΠϯϥΠϯԽͳ͠)
VM࣮ߦ → ػցޠ ͰͷੑೳมԽ NES emulator: github.com/mame/optcarrot fps 0 23
46 69 92 2.6 native 2.6 JIT 85.8 56.2 55.9 (-O3 w/ VM໋ྩΠϯϥΠϯԽͳ͠)
ػցޠʹ͚ͨͩ͠Ͱ͘ͳΒͳ͍ • VMͷ࣮͕ͦͦػցޠͰಈ͍͍ͯΔ • VMͰ࣮͞Ε͍ͯΔෳࡶͳॲཧΛ؆ུԽͰ͖ͳ ͚Ε͘ͳΒͳ͍
VMͷதͰԿ͕ى͖͍ͯΔ͔ (Ͳ͕͘͜Ͱ͖ͦ͏Ͱ͠ΐ͏͔?)
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
RubyͷJITCίϯύΠϥΛ͏ • ࣮ߦ࣌ʹCίϯύΠϥΛ࣮ߦͯ͠ಈతϥΠϒϥϦ Λੜɺಈతϩʔυ • ϝϦοτ: Ҡ২ੑͷ୲อ͕ͦΕͳΓʹָɺෳ ͷ࠷దԽόοΫΤϯυΛ͑Δ • σϝϦοτ:
JITίϯύΠϧத·͋·͋Ϧιʔε Λ͏ɺϓϩηεγάφϧͷѻ͍͕ࡶʹͳ Δ
RubyͷJITCίϯύΠϥΛ͏ • CίϯύΠϥී௨ͷCͷίʔυͰ࠷దԽͰ͖Δൣ ғ͔͠࠷దԽͰ͖ͳ͍ • Rubyಛ༗ͷಛผͳॲཧΛऔΓআ͘ͷେମࣗ Ͱ࣮͢Δ͔͠ͳ͍
ίϯύΠϥͷ࠷దԽʹΑΔߴԽ NES emulator: github.com/mame/optcarrot fps 0 23 46 69 92
2.6 native com piler 2.6 JIT 85.8 62.5 56.2 55.9 (-O3 w/ VM໋ྩΠϯϥΠϯԽ͋Γ)
JITͷجૅ1 ϓϩάϥϜΧϯλͷ࠷దԽ
None
ϓϩάϥϜΧϯλͷ࠷దԽ • VMͷϓϩάϥϜΧϯλͷҙٛͷҰͭVM໋ྩ σΟεύον͕ͩɺ͜Εෆཁʹͳ͍ͬͯΔ • ͰɺϓϩάϥϜΧϯλಈ͔͞ͳ͍͍͔ͯ͘?
ϓϩάϥϜΧϯλͷ࠷దԽ • ଞͷϓϩάϥϜΧϯλͷ༻్: • ྫ֎ͷΩϟονςʔϒϧʹΘΕΔ • ྫ֎͕ͳ͘ͱߦ൪߸ͷܭࢉʹΘΕΔ
None
None
ϓϩάϥϜΧϯλͷ࠷దԽ NES emulator: github.com/mame/optcarrot fps 0 23 46 69 92
2.6 native com piler pc 2.6 JIT 85.8 62.7 62.5 56.2 55.9
JITͷجૅ2 ελοΫϙΠϯλͷ࠷దԽ
None
ελοΫϙΠϯλͷ࠷దԽ • VMͷελοΫϙΠϯλͷҙٛVMͷελοΫͷ ૢ࡞ʹ͋Δ • ωΠςΟϒͳελοΫϨδελͰಉ͡ૢ࡞Λ࣮ ݱͯ͠ελοΫϙΠϯλΛΊΒΕͳ͍͔?
ελοΫϙΠϯλͷ࠷దԽ • ϝιουݺͼग़͢͠ΔࡍʹVMͷελοΫʹ ͞ͳ͍ͱݺͼग़ͨ͠ϝιου͔ΒࢀরͰ͖ͳ͍ • େҬग़ޙʹΘΕΔ
None
None
None
None
ελοΫϙΠϯλͷ࠷దԽ NES emulator: github.com/mame/optcarrot fps 0 23 46 69 92
2.6 native com piler pc sp 2.6 JIT 85.8 66.4 62.7 62.5 56.2 55.9
·͔ͩͳΓ͕ࠩ͋Δ NES emulator: github.com/mame/optcarrot fps 0 23 46 69 92
2.6 native com piler pc sp 2.6 JIT 85.8 66.4 62.7 62.5 56.2 55.9
Ͳ͏ͨ͠Βߋʹ͘ͳΔͷ͔? • PC/SPͷૢ࡞ผʹϘτϧωοΫͰͳ͍ͷͰ͜ ΕͰ͘ͳΒͳ͍ • Ή͠ΖRubyͷಈతͳಛੑͷαϙʔτͷͨΊʹ͠ ͘ͳ͍ͬͯΔ࠷దԽΛ࣮ݱ͢Δํ͕ॏཁ
JITͷڧΈΛੜ͔ͨ͠࠷దԽ
JITͷԠ༻1 ϝιουΠϯϥΠϯԽ
None
ϝιουΠϯϥΠϯԽ • 1+1Λܭࢉ͢ΔΑΓRubyͷϝιουݺͼग़͠ॲ ཧͷํ͕ෳࡶ • ϝιουݺͼग़͠ͷΦʔόʔϔουΛແʹͰ͖ ͳ͍͔? => ΠϯϥΠϯԽ
ϝιουΠϯϥΠϯԽ • ΠϯϥΠϯԽ͢ΔͱɺίʔϧελοΫͷૢ࡞͕ͳ ͘ͳΔ͚ͩͰͳ͘ɺϝιουݺͼग़͠Λӽ͑ͯ࠷ దԽ͕ޮ͘ • ͔͠͠ΠϯϥΠϯԽͨ͠ޙਖ਼͍͠ڍಈʹదʹ෮ ؼͰ͖Δඞཁ͕͋Δ • “Deoptimization”
ͱ͍͏
Rubyͷϝιουݺͼग़͠ͷ͘͠Έ • άϩʔόϧϝιουΩϟογϡͱΠϯϥΠϯϝ ιουΩϟογϡ͕͋Δ • ΠϯϥΠϯϝιουΩϟογϡશͯͷcall siteʹ ରͯ͠ଘࡏ͢Δ • ͜ΕΛDeoptimizationʹ͏
ϝιουΠϯϥΠϯԽ • ΠϯϥΠϯԽ͢ΔϝιουͰߦ൪߸Λऔಘͨ͠ Γྫ֎͕ඈΜͩΓ͠ͳ͍͜ͱΛอূ͠ͳ͍ͱ͍͚ ͳ͍ • VM໋ྩͷϝλσʔλΛ༻ҙ͓ͯ͘͠
ϝιουΠϯϥΠϯԽ
None
None
ϝιουΠϯϥΠϯԽ NES emulator: github.com/mame/optcarrot fps 0 23 46 69 92
2.6 native com piler pc sp inline 2.6 JIT 85.8 72.4 66.4 62.7 62.5 56.2 55.9
JITͷԠ༻2 Πϯελϯεมͷ࠷దԽ
Πϯελϯεมͷ࠷దԽ • ΠϯελϯεมΛಡΉʹԿ͔ϝϞϦͷϩʔ υΛ͠ͳ͍ͱ͍͚ͳ͍ => ͍ • ຊདྷϩʔυ͠ͳ͍ͱ͍͚ͳ͍ΛJITੜίʔυ ͰଈʹͰ͖ͳ͍͔?
RubyͷΠϯελϯεมͷ͘͠Έ • Ϋϥε͝ͱʹɺ͋ΔΠϯελϯεม͕֨ೲ͞Ε ͍ͯΔΠϯσοΫε͕ҟͳΔ • ܧঝͯ͠ಉ໋͡ྩྻ͕ڞ༗͞Ε͍ͯΔͱյΕΔ
Πϯελϯεมͷ࠷దԽ • ϝιουΠϯϥΠϯԽͱಉ༷ͷํ๏Ͱ Deoptimization͢Δ • Ճ͑ͯɺ༨ܭͳϒϥϯνΛΔ͜ͱͰίϯύΠϥ ͷ࠷దԽ͕ޮ͖͘͢͢Δ
Πϯελϯεมͷ࠷దԽ
Πϯελϯεมͷ࠷దԽ NES emulator: github.com/mame/optcarrot fps 0 23 46 69 92
2.6 native com piler pc sp inline ivar 2.6 JIT 85.8 85.8 72.4 66.4 62.7 62.5 56.2 55.9
Ruby·ͩਐԽΛ͍ͯ͠Δ (TruffleRuby 170fps ग़Δ)
·ͱΊ • Ruby࠷ͷݴޠͱͳΓಘΔ͔ʁ • ଞͷݴޠͰॻ͚ΔίʔυɺେͦΕ૬ ʹ࠷దԽͰ͖Δ • Rubyಛ༗ͬΆ͍ݴޠػೳઅͷ͋Δར༻Λ