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
12k
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
760
Ruby 3.0 JIT on Rails
k0kubun
9
9.1k
Other Decks in Programming
See All in Programming
EventSourcingの理想と現実
wenas
6
2.3k
2024/11/8 関西Kaggler会 2024 #3 / Kaggle Kernel で Gemma 2 × vLLM を動かす。
kohecchi
5
910
OSSで起業してもうすぐ10年 / Open Source Conference 2024 Shimane
furukawayasuto
0
100
Realtime API 入門
riofujimon
0
150
Streams APIとTCPフロー制御 / Web Streams API and TCP flow control
tasshi
2
350
リアーキテクチャxDDD 1年間の取り組みと進化
hsawaji
1
220
C++でシェーダを書く
fadis
6
4.1k
どうして僕の作ったクラスが手続き型と言われなきゃいけないんですか
akikogoto
1
120
Amazon Bedrock Agentsを用いてアプリ開発してみた!
har1101
0
330
Less waste, more joy, and a lot more green: How Quarkus makes Java better
hollycummins
0
100
ローコードSaaSのUXを向上させるためのTypeScript
taro28
1
600
よくできたテンプレート言語として TypeScript + JSX を利用する試み / Using TypeScript + JSX outside of Web Frontend #TSKaigiKansai
izumin5210
6
1.7k
Featured
See All Featured
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
Fashionably flexible responsive web design (full day workshop)
malarkey
405
65k
The Cost Of JavaScript in 2023
addyosmani
45
6.7k
4 Signs Your Business is Dying
shpigford
180
21k
Art, The Web, and Tiny UX
lynnandtonic
297
20k
Designing the Hi-DPI Web
ddemaree
280
34k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
42
9.2k
Side Projects
sachag
452
42k
Automating Front-end Workflow
addyosmani
1366
200k
The Art of Programming - Codeland 2020
erikaheidi
52
13k
The World Runs on Bad Software
bkeepers
PRO
65
11k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
364
24k
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ಛ༗ͬΆ͍ݴޠػೳઅͷ͋Δར༻Λ