Optimizing Ruby with JIT
by
Takashi Kokubun
Link
Embed
Share
Beginning
This slide
Copy link URL
Copy link URL
Copy iframe embed code
Copy iframe embed code
Copy javascript embed code
Copy javascript embed code
Share
Tweet
Share
Tweet
Slide 1
Slide 1 text
Optimizing Ruby with JIT Builderscon 2019 / Takashi Kokubun ࠷ͷݴޠΛࢦͯ͠
Slide 2
Slide 2 text
No content
Slide 3
Slide 3 text
Ruby 2.6 ͔ΒJITΛ։ൃ
Slide 4
Slide 4 text
Γ·ͨ͠ [Misc #16094]
Slide 5
Slide 5 text
No content
Slide 6
Slide 6 text
(࠷దԽࢹͰͷ) Rubyͷಛ
Slide 7
Slide 7 text
1. ϝιουఆ࠶ఆٛ͠์ 1 + 1 #=> 100
Slide 8
Slide 8 text
1. ϝιουఆ࠶ఆٛ͠์ 1 + 1 #=> 100
Slide 9
Slide 9 text
1. ϝιουఆ࠶ఆٛ͠์ 1 + 1 #=> 100
Slide 10
Slide 10 text
2. ߦ͕ਐΉͱҙͷ͜ͱ͕ى͖Δ a = 1 + 1
Slide 11
Slide 11 text
2. ߦ͕ਐΉͱҙͷ͜ͱ͕ى͖Δ a = 1 + 1 a #=> 2
Slide 12
Slide 12 text
2. ߦ͕ਐΉͱҙͷ͜ͱ͕ى͖Δ a = 1 + 1 a #=> 2 a #=> 100 !?
Slide 13
Slide 13 text
2. ߦ͕ਐΉͱҙͷ͜ͱ͕ى͖Δ
Slide 14
Slide 14 text
2. ߦ͕ਐΉͱҙͷ͜ͱ͕ى͖Δ ʮ3ߦʹୡͨ͠Β a = 100ʯ
Slide 15
Slide 15 text
3. ଞʹָ͍͠ػೳ͕͍ͬͺ͍ • eval • ଋറڥͷΦϒδΣΫτԽ (binding, Proc) • ҙΦϒδΣΫτͷྻڍػೳ (ObjectSpace) • Φʔόʔϑϩʔ͢Δͱܕ͕มΘΔ • ΞυϨεมΘΓ์ͷώʔϓྖҬ (GC.compact)
Slide 16
Slide 16 text
ຊͷΰʔϧ ͜ͷ࠷దԽ͕ࠔͳݴޠΛ ݶք·ͰߴԽ͢Δ ָ͠͞Λମݧ͍ͯͨͩ͘͠
Slide 17
Slide 17 text
RubyΠϯλϓϦλͷ͘͠Έ
Slide 18
Slide 18 text
1. RubyͷίʔυΛಡΈࠐΉ
Slide 19
Slide 19 text
2. ύʔεͯ͠ߏจʹ͢Δ EFGUXP
Slide 20
Slide 20 text
3. ίϯύΠϧ໋ͯ͠ྩྻʹ͢Δ EFGUXP UXP QVUPCKFDU QVUPCKFDU TFOE MFBWF
Slide 21
Slide 21 text
4. VMͰ໋ྩྻΛ࣮ߦ͢Δ EFGUXP UXP QVUPCKFDU QVUPCKFDU TFOE MFBWF 7JSUVBM.BDIJOF ελοΫ
Slide 22
Slide 22 text
4. VMͰ໋ྩྻΛ࣮ߦ͢Δ EFGUXP UXP QVUPCKFDU QVUPCKFDU TFOE MFBWF 7JSUVBM.BDIJOF ελοΫ
Slide 23
Slide 23 text
4. VMͰ໋ྩྻΛ࣮ߦ͢Δ EFGUXP UXP QVUPCKFDU TFOE MFBWF 7JSUVBM.BDIJOF ελοΫ
Slide 24
Slide 24 text
4. VMͰ໋ྩྻΛ࣮ߦ͢Δ EFGUXP UXP TFOE MFBWF 7JSUVBM.BDIJOF ελοΫ
Slide 25
Slide 25 text
4. VMͰ໋ྩྻΛ࣮ߦ͢Δ EFGUXP UXP MFBWF 7JSUVBM.BDIJOF ελοΫ
Slide 26
Slide 26 text
͜ͷϑΣʔζͷڍಈΛ͍ͨ͘͠ EFGUXP UXP QVUPCKFDU QVUPCKFDU TFOE MFBWF 7JSUVBM.BDIJOF ελοΫ
Slide 27
Slide 27 text
5. JIT ίϯύΠϥͰߴԽ͢Δ UXP QVUPCKFDU QVUPCKFDU TFOE MFBWF 7JSUVBM.BDIJOF ελοΫ +*5
Slide 28
Slide 28 text
5. JIT ίϯύΠϥͰߴԽ͢Δ UXP QVUPCKFDU QVUPCKFDU TFOE MFBWF 7JSUVBM.BDIJOF ελοΫ +*5 ࠓͷͱ͜Ζ *OUFHFS ࠶ఆٛ͞Εͯͳ͍ͳ
Slide 29
Slide 29 text
5. JIT ίϯύΠϥͰߴԽ͢Δ UXP QVUPCKFDU QVUPCKFDU TFOE MFBWF 7JSUVBM.BDIJOF ελοΫ +*5 ͳͷͰ
Slide 30
Slide 30 text
5. JIT ίϯύΠϥͰߴԽ͢Δ UXP QVUPCKFDU QVUPCKFDU TFOE MFBWF 7JSUVBM.BDIJOF ελοΫ +*5 ͍ɺ͕࠶ఆٛ ͞ΕͨΒ͔
Slide 31
Slide 31 text
5. JIT ίϯύΠϥͰߴԽ͢Δ UXP QVUPCKFDU QVUPCKFDU TFOE MFBWF 7JSUVBM.BDIJOF ελοΫ +*5
Slide 32
Slide 32 text
5. JIT ίϯύΠϥͰߴԽ͢Δ UXP QVUPCKFDU QVUPCKFDU TFOE MFBWF 7JSUVBM.BDIJOF ελοΫ +*5
Slide 33
Slide 33 text
JITͰͲͷ͘Β͍͘ͳΔͷ͔
Slide 34
Slide 34 text
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
Slide 35
Slide 35 text
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
Slide 36
Slide 36 text
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
Slide 37
Slide 37 text
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
Slide 38
Slide 38 text
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
Slide 39
Slide 39 text
͜ͷ͕ࠩग़Δཧ༝Λݟ͍͖ͯ·͢ 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
Slide 40
Slide 40 text
JITίϯύΠϥͲͷΑ͏ʹ ߴԽΛ͢Δ͔?
Slide 41
Slide 41 text
JITͷجૅ0 ػցޠʹ͚ͨͩ͠Ͱ ͘ͳΒͳ͍
Slide 42
Slide 42 text
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໋ྩΠϯϥΠϯԽͳ͠)
Slide 43
Slide 43 text
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໋ྩΠϯϥΠϯԽͳ͠)
Slide 44
Slide 44 text
ػցޠʹ͚ͨͩ͠Ͱ͘ͳΒͳ͍ • VMͷ࣮͕ͦͦػցޠͰಈ͍͍ͯΔ • VMͰ࣮͞Ε͍ͯΔෳࡶͳॲཧΛ؆ུԽͰ͖ͳ ͚Ε͘ͳΒͳ͍
Slide 45
Slide 45 text
VMͷதͰԿ͕ى͖͍ͯΔ͔ (Ͳ͕͘͜Ͱ͖ͦ͏Ͱ͠ΐ͏͔?)
Slide 46
Slide 46 text
No content
Slide 47
Slide 47 text
No content
Slide 48
Slide 48 text
No content
Slide 49
Slide 49 text
No content
Slide 50
Slide 50 text
No content
Slide 51
Slide 51 text
No content
Slide 52
Slide 52 text
No content
Slide 53
Slide 53 text
No content
Slide 54
Slide 54 text
No content
Slide 55
Slide 55 text
No content
Slide 56
Slide 56 text
No content
Slide 57
Slide 57 text
No content
Slide 58
Slide 58 text
No content
Slide 59
Slide 59 text
No content
Slide 60
Slide 60 text
No content
Slide 61
Slide 61 text
No content
Slide 62
Slide 62 text
No content
Slide 63
Slide 63 text
RubyͷJITCίϯύΠϥΛ͏ • ࣮ߦ࣌ʹCίϯύΠϥΛ࣮ߦͯ͠ಈతϥΠϒϥϦ Λੜɺಈతϩʔυ • ϝϦοτ: Ҡ২ੑͷ୲อ͕ͦΕͳΓʹָɺෳ ͷ࠷దԽόοΫΤϯυΛ͑Δ • σϝϦοτ: JITίϯύΠϧத·͋·͋Ϧιʔε Λ͏ɺϓϩηεγάφϧͷѻ͍͕ࡶʹͳ Δ
Slide 64
Slide 64 text
RubyͷJITCίϯύΠϥΛ͏ • CίϯύΠϥී௨ͷCͷίʔυͰ࠷దԽͰ͖Δൣ ғ͔͠࠷దԽͰ͖ͳ͍ • Rubyಛ༗ͷಛผͳॲཧΛऔΓআ͘ͷେମࣗ Ͱ࣮͢Δ͔͠ͳ͍
Slide 65
Slide 65 text
ίϯύΠϥͷ࠷దԽʹΑΔߴԽ 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໋ྩΠϯϥΠϯԽ͋Γ)
Slide 66
Slide 66 text
JITͷجૅ1 ϓϩάϥϜΧϯλͷ࠷దԽ
Slide 67
Slide 67 text
No content
Slide 68
Slide 68 text
ϓϩάϥϜΧϯλͷ࠷దԽ • VMͷϓϩάϥϜΧϯλͷҙٛͷҰͭVM໋ྩ σΟεύον͕ͩɺ͜Εෆཁʹͳ͍ͬͯΔ • ͰɺϓϩάϥϜΧϯλಈ͔͞ͳ͍͍͔ͯ͘?
Slide 69
Slide 69 text
ϓϩάϥϜΧϯλͷ࠷దԽ • ଞͷϓϩάϥϜΧϯλͷ༻్: • ྫ֎ͷΩϟονςʔϒϧʹΘΕΔ • ྫ֎͕ͳ͘ͱߦ൪߸ͷܭࢉʹΘΕΔ
Slide 70
Slide 70 text
No content
Slide 71
Slide 71 text
No content
Slide 72
Slide 72 text
ϓϩάϥϜΧϯλͷ࠷దԽ 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
Slide 73
Slide 73 text
JITͷجૅ2 ελοΫϙΠϯλͷ࠷దԽ
Slide 74
Slide 74 text
No content
Slide 75
Slide 75 text
ελοΫϙΠϯλͷ࠷దԽ • VMͷελοΫϙΠϯλͷҙٛVMͷελοΫͷ ૢ࡞ʹ͋Δ • ωΠςΟϒͳελοΫϨδελͰಉ͡ૢ࡞Λ࣮ ݱͯ͠ελοΫϙΠϯλΛΊΒΕͳ͍͔?
Slide 76
Slide 76 text
ελοΫϙΠϯλͷ࠷దԽ • ϝιουݺͼग़͢͠ΔࡍʹVMͷελοΫʹ ͞ͳ͍ͱݺͼग़ͨ͠ϝιου͔ΒࢀরͰ͖ͳ͍ • େҬग़ޙʹΘΕΔ
Slide 77
Slide 77 text
No content
Slide 78
Slide 78 text
No content
Slide 79
Slide 79 text
No content
Slide 80
Slide 80 text
No content
Slide 81
Slide 81 text
ελοΫϙΠϯλͷ࠷దԽ 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
Slide 82
Slide 82 text
·͔ͩͳΓ͕ࠩ͋Δ 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
Slide 83
Slide 83 text
Ͳ͏ͨ͠Βߋʹ͘ͳΔͷ͔? • PC/SPͷૢ࡞ผʹϘτϧωοΫͰͳ͍ͷͰ͜ ΕͰ͘ͳΒͳ͍ • Ή͠ΖRubyͷಈతͳಛੑͷαϙʔτͷͨΊʹ͠ ͘ͳ͍ͬͯΔ࠷దԽΛ࣮ݱ͢Δํ͕ॏཁ
Slide 84
Slide 84 text
JITͷڧΈΛੜ͔ͨ͠࠷దԽ
Slide 85
Slide 85 text
JITͷԠ༻1 ϝιουΠϯϥΠϯԽ
Slide 86
Slide 86 text
No content
Slide 87
Slide 87 text
ϝιουΠϯϥΠϯԽ • 1+1Λܭࢉ͢ΔΑΓRubyͷϝιουݺͼग़͠ॲ ཧͷํ͕ෳࡶ • ϝιουݺͼग़͠ͷΦʔόʔϔουΛແʹͰ͖ ͳ͍͔? => ΠϯϥΠϯԽ
Slide 88
Slide 88 text
ϝιουΠϯϥΠϯԽ • ΠϯϥΠϯԽ͢ΔͱɺίʔϧελοΫͷૢ࡞͕ͳ ͘ͳΔ͚ͩͰͳ͘ɺϝιουݺͼग़͠Λӽ͑ͯ࠷ దԽ͕ޮ͘ • ͔͠͠ΠϯϥΠϯԽͨ͠ޙਖ਼͍͠ڍಈʹదʹ෮ ؼͰ͖Δඞཁ͕͋Δ • “Deoptimization” ͱ͍͏
Slide 89
Slide 89 text
Rubyͷϝιουݺͼग़͠ͷ͘͠Έ • άϩʔόϧϝιουΩϟογϡͱΠϯϥΠϯϝ ιουΩϟογϡ͕͋Δ • ΠϯϥΠϯϝιουΩϟογϡશͯͷcall siteʹ ରͯ͠ଘࡏ͢Δ • ͜ΕΛDeoptimizationʹ͏
Slide 90
Slide 90 text
ϝιουΠϯϥΠϯԽ • ΠϯϥΠϯԽ͢ΔϝιουͰߦ൪߸Λऔಘͨ͠ Γྫ֎͕ඈΜͩΓ͠ͳ͍͜ͱΛอূ͠ͳ͍ͱ͍͚ ͳ͍ • VM໋ྩͷϝλσʔλΛ༻ҙ͓ͯ͘͠
Slide 91
Slide 91 text
ϝιουΠϯϥΠϯԽ
Slide 92
Slide 92 text
No content
Slide 93
Slide 93 text
No content
Slide 94
Slide 94 text
ϝιουΠϯϥΠϯԽ 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
Slide 95
Slide 95 text
JITͷԠ༻2 Πϯελϯεมͷ࠷దԽ
Slide 96
Slide 96 text
Πϯελϯεมͷ࠷దԽ • ΠϯελϯεมΛಡΉʹԿ͔ϝϞϦͷϩʔ υΛ͠ͳ͍ͱ͍͚ͳ͍ => ͍ • ຊདྷϩʔυ͠ͳ͍ͱ͍͚ͳ͍ΛJITੜίʔυ ͰଈʹͰ͖ͳ͍͔?
Slide 97
Slide 97 text
RubyͷΠϯελϯεมͷ͘͠Έ • Ϋϥε͝ͱʹɺ͋ΔΠϯελϯεม͕֨ೲ͞Ε ͍ͯΔΠϯσοΫε͕ҟͳΔ • ܧঝͯ͠ಉ໋͡ྩྻ͕ڞ༗͞Ε͍ͯΔͱյΕΔ
Slide 98
Slide 98 text
Πϯελϯεมͷ࠷దԽ • ϝιουΠϯϥΠϯԽͱಉ༷ͷํ๏Ͱ Deoptimization͢Δ • Ճ͑ͯɺ༨ܭͳϒϥϯνΛΔ͜ͱͰίϯύΠϥ ͷ࠷దԽ͕ޮ͖͘͢͢Δ
Slide 99
Slide 99 text
Πϯελϯεมͷ࠷దԽ
Slide 100
Slide 100 text
Πϯελϯεมͷ࠷దԽ 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
Slide 101
Slide 101 text
Ruby·ͩਐԽΛ͍ͯ͠Δ (TruffleRuby 170fps ग़Δ)
Slide 102
Slide 102 text
·ͱΊ • Ruby࠷ͷݴޠͱͳΓಘΔ͔ʁ • ଞͷݴޠͰॻ͚ΔίʔυɺେͦΕ૬ ʹ࠷దԽͰ͖Δ • Rubyಛ༗ͬΆ͍ݴޠػೳઅͷ͋Δར༻Λ