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
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
Ruby 3.0 JIT on Rails
k0kubun
9
9.1k
Other Decks in Programming
See All in Programming
Amazon S3 NYJavaSIG 2024-12-12
sullis
0
100
フロントエンドのディレクトリ構成どうしてる? Feature-Sliced Design 導入体験談
osakatechlab
8
4.1k
今年のアップデートで振り返るCDKセキュリティのシフトレフト/2024-cdk-security-shift-left
tomoki10
0
200
暇に任せてProxmoxコンソール 作ってみました
karugamo
2
720
Go の GC の不得意な部分を克服したい
taiyow
3
780
これでLambdaが不要に?!Step FunctionsのJSONata対応について
iwatatomoya
2
3.6k
Beyond ORM
77web
5
700
わたしの星のままで一番星になる ~ 出産を機にSIerからEC事業会社に転職した話 ~
kimura_m_29
0
180
クリエイティブコーディングとRuby学習 / Creative Coding and Learning Ruby
chobishiba
0
3.9k
責務を分離するための例外設計 - PHPカンファレンス 2024
kajitack
5
820
ブラウザ単体でmp4書き出すまで - muddy-web - 2024-12
yue4u
3
470
短期間での新規プロダクト開発における「コスパの良い」Goのテスト戦略」 / kamakura.go
n3xem
2
170
Featured
See All Featured
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
GraphQLとの向き合い方2022年版
quramy
44
13k
Building Applications with DynamoDB
mza
91
6.1k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
127
18k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
1.9k
Site-Speed That Sticks
csswizardry
2
190
Speed Design
sergeychernyshev
25
670
Dealing with People You Can't Stand - Big Design 2015
cassininazir
365
25k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
Adopting Sorbet at Scale
ufuk
73
9.1k
Raft: Consensus for Rubyists
vanstee
137
6.7k
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ಛ༗ͬΆ͍ݴޠػೳઅͷ͋Δར༻Λ