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
Stackless и stackful? Корутины и асинхронность в Go
lamodatech
0
660
MCP with Cloudflare Workers
yusukebe
2
220
KMP와 kotlinx.rpc로 서버와 클라이언트 동기화
kwakeuijin
0
140
コンテナをたくさん詰め込んだシステムとランタイムの変化
makihiro
1
120
[JAWS-UG横浜 #76] イケてるアップデートを宇宙いち早く紹介するよ!
maroon1st
0
450
LLM Supervised Fine-tuningの理論と実践
datanalyticslabo
3
980
わたしの星のままで一番星になる ~ 出産を機にSIerからEC事業会社に転職した話 ~
kimura_m_29
0
180
Haze - Real time background blurring
chrisbanes
1
510
DevFest Tokyo 2025 - Flutter のアプリアーキテクチャ現在地点
wasabeef
5
900
useSyncExternalStoreを使いまくる
ssssota
6
1k
テストコード文化を0から作り、変化し続けた組織
kazatohiei
2
1.5k
数十万行のプロジェクトを Scala 2から3に完全移行した
xuwei_k
0
260
Featured
See All Featured
Fashionably flexible responsive web design (full day workshop)
malarkey
405
66k
What's in a price? How to price your products and services
michaelherold
243
12k
Thoughts on Productivity
jonyablonski
67
4.4k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
Producing Creativity
orderedlist
PRO
341
39k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
7k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
Building Applications with DynamoDB
mza
91
6.1k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
The Cost Of JavaScript in 2023
addyosmani
45
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ಛ༗ͬΆ͍ݴޠػೳઅͷ͋Δར༻Λ