Optimizing Ruby with JIT

Optimizing Ruby with JIT

08d5432a5bc31e6d9edec87b94cb1db1?s=128

Takashi Kokubun

August 30, 2019
Tweet

Transcript

  1. Optimizing Ruby with JIT Builderscon 2019 / Takashi Kokubun ࠷଎ͷݴޠΛ໨ࢦͯ͠

  2. None
  3. Ruby 2.6 ͔ΒJITΛ։ൃ

  4. ΍Γ·ͨ͠ [Misc #16094]

  5. None
  6. (࠷దԽࢹ఺Ͱͷ) Rubyͷಛ௃

  7. 1. ϝιου΋ఆ਺΋࠶ఆٛ͠์୊ 1 + 1 #=> 100

  8. 1. ϝιου΋ఆ਺΋࠶ఆٛ͠์୊ 1 + 1 #=> 100

  9. 1. ϝιου΋ఆ਺΋࠶ఆٛ͠์୊ 1 + 1 #=> 100

  10. 2. ߦ͕ਐΉͱ೚ҙͷ͜ͱ͕ى͖Δ a = 1 + 1

  11. 2. ߦ͕ਐΉͱ೚ҙͷ͜ͱ͕ى͖Δ a = 1 + 1 a #=> 2

  12. 2. ߦ͕ਐΉͱ೚ҙͷ͜ͱ͕ى͖Δ a = 1 + 1 a #=> 2

    a #=> 100 !?
  13. 2. ߦ͕ਐΉͱ೚ҙͷ͜ͱ͕ى͖Δ

  14. 2. ߦ͕ਐΉͱ೚ҙͷ͜ͱ͕ى͖Δ ʮ3ߦ໨ʹୡͨ͠Β a = 100ʯ

  15. 3. ଞʹ΋ָ͍͠ػೳ͕͍ͬͺ͍ • eval • ଋറ΍؀ڥͷΦϒδΣΫτԽ (binding, Proc) • ೚ҙΦϒδΣΫτͷྻڍػೳ

    (ObjectSpace) • Φʔόʔϑϩʔ͢Δͱܕ͕มΘΔ੔਺ • ΞυϨεมΘΓ์୊ͷώʔϓྖҬ (GC.compact)
  16. ຊ೔ͷΰʔϧ ͜ͷ࠷దԽ͕ࠔ೉ͳݴޠΛ ݶք·Ͱߴ଎Խ͢Δ ָ͠͞Λମݧ͍ͯͨͩ͘͠

  17. RubyΠϯλϓϦλͷ͘͠Έ

  18. 1. RubyͷίʔυΛಡΈࠐΉ

  19. 2. ύʔεͯ͠ߏจ໦ʹ͢Δ EFGUXP  

  20. 3. ίϯύΠϧ໋ͯ͠ྩྻʹ͢Δ EFGUXP   UXP QVUPCKFDU QVUPCKFDU TFOE MFBWF

  21. 4. VMͰ໋ྩྻΛ࣮ߦ͢Δ EFGUXP   UXP QVUPCKFDU QVUPCKFDU TFOE MFBWF

    7JSUVBM.BDIJOF ελοΫ
  22. 4. VMͰ໋ྩྻΛ࣮ߦ͢Δ EFGUXP   UXP QVUPCKFDU QVUPCKFDU TFOE MFBWF

    7JSUVBM.BDIJOF ελοΫ 
  23. 4. VMͰ໋ྩྻΛ࣮ߦ͢Δ EFGUXP   UXP QVUPCKFDU TFOE MFBWF 7JSUVBM.BDIJOF

    ελοΫ  
  24. 4. VMͰ໋ྩྻΛ࣮ߦ͢Δ EFGUXP   UXP TFOE MFBWF 7JSUVBM.BDIJOF ελοΫ

      
  25. 4. VMͰ໋ྩྻΛ࣮ߦ͢Δ EFGUXP   UXP MFBWF 7JSUVBM.BDIJOF ελοΫ 

  26. ͜ͷϑΣʔζͷڍಈΛ଎͍ͨ͘͠ EFGUXP   UXP QVUPCKFDU QVUPCKFDU TFOE MFBWF 7JSUVBM.BDIJOF

    ελοΫ
  27. 5. JIT ίϯύΠϥͰߴ଎Խ͢Δ UXP QVUPCKFDU QVUPCKFDU TFOE MFBWF 7JSUVBM.BDIJOF ελοΫ

    +*5
  28. 5. JIT ίϯύΠϥͰߴ଎Խ͢Δ UXP QVUPCKFDU QVUPCKFDU TFOE MFBWF 7JSUVBM.BDIJOF ελοΫ

    +*5 ࠓͷͱ͜Ζ *OUFHFS  ࠶ఆٛ͞Εͯͳ͍ͳ
  29. 5. JIT ίϯύΠϥͰߴ଎Խ͢Δ UXP QVUPCKFDU QVUPCKFDU TFOE MFBWF 7JSUVBM.BDIJOF ελοΫ

    +*5   ͳͷͰ͸
  30. 5. JIT ίϯύΠϥͰߴ଎Խ͢Δ UXP QVUPCKFDU QVUPCKFDU TFOE MFBWF 7JSUVBM.BDIJOF ελοΫ

    +*5 ͍΍ɺ ͕࠶ఆٛ ͞ΕͨΒ͔΋
  31. 5. JIT ίϯύΠϥͰߴ଎Խ͢Δ UXP QVUPCKFDU QVUPCKFDU TFOE MFBWF 7JSUVBM.BDIJOF ελοΫ

    +*5
  32. 5. JIT ίϯύΠϥͰߴ଎Խ͢Δ UXP QVUPCKFDU QVUPCKFDU TFOE MFBWF 7JSUVBM.BDIJOF ελοΫ

    +*5
  33. JITͰͲͷ͘Β͍଎͘ͳΔͷ͔

  34. 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
  35. 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
  36. 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
  37. 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
  38. 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
  39. ͜ͷ͕ࠩग़Δཧ༝Λݟ͍͖ͯ·͢ 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
  40. JITίϯύΠϥ͸ͲͷΑ͏ʹ ߴ଎ԽΛ͢Δ͔?

  41. JITͷجૅ0 ػցޠʹ͚ͨͩ͠Ͱ͸ ଎͘ͳΒͳ͍

  42. 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໋ྩΠϯϥΠϯԽͳ͠)
  43. 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໋ྩΠϯϥΠϯԽͳ͠)
  44. ػցޠʹ͚ͨͩ͠Ͱ͸଎͘ͳΒͳ͍ • VMͷ࣮૷͕ͦ΋ͦ΋ػցޠͰಈ͍͍ͯΔ • VMͰ࣮૷͞Ε͍ͯΔෳࡶͳॲཧΛ؆ུԽͰ͖ͳ ͚Ε͹଎͘ͳΒͳ͍

  45. VMͷதͰ͸Կ͕ى͖͍ͯΔ͔ (Ͳ͕͜଎͘Ͱ͖ͦ͏Ͱ͠ΐ͏͔?)

  46. None
  47. None
  48. None
  49. None
  50. None
  51. None
  52. None
  53. None
  54. None
  55. None
  56. None
  57. None
  58. None
  59. None
  60. None
  61. None
  62. None
  63. RubyͷJIT͸CίϯύΠϥΛ࢖͏ • ࣮ߦ࣌ʹCίϯύΠϥΛ࣮ߦͯ͠ಈతϥΠϒϥϦ Λੜ੒ɺಈతϩʔυ • ϝϦοτ: Ҡ২ੑͷ୲อ͕ͦΕͳΓʹָɺෳ਺ ͷ࠷దԽόοΫΤϯυΛ࢖͑Δ • σϝϦοτ:

    JITίϯύΠϧத·͋·͋Ϧιʔε Λ࢖͏ɺϓϩηε΍γάφϧͷѻ͍͕൥ࡶʹͳ Δ
  64. RubyͷJIT͸CίϯύΠϥΛ࢖͏ • CίϯύΠϥ͸ී௨ͷCͷίʔυͰ࠷దԽͰ͖Δൣ ғ͔͠࠷దԽͰ͖ͳ͍ • Rubyಛ༗ͷಛผͳॲཧΛऔΓআ͘ͷ͸େମࣗ෼ Ͱ࣮૷͢Δ͔͠ͳ͍

  65. ίϯύΠϥͷ࠷దԽʹΑΔߴ଎Խ 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໋ྩΠϯϥΠϯԽ͋Γ)
  66. JITͷجૅ1 ϓϩάϥϜΧ΢ϯλͷ࠷దԽ

  67. None
  68. ϓϩάϥϜΧ΢ϯλͷ࠷దԽ • VMͷϓϩάϥϜΧ΢ϯλͷҙٛͷҰͭ͸VM໋ྩ σΟεύον͕ͩɺ͜Ε͸ෆཁʹͳ͍ͬͯΔ • Ͱ͸ɺϓϩάϥϜΧ΢ϯλಈ͔͞ͳͯ͘΋͍͍͔?

  69. ϓϩάϥϜΧ΢ϯλͷ࠷దԽ • ଞͷϓϩάϥϜΧ΢ϯλͷ༻్: • ྫ֎ͷΩϟονςʔϒϧʹ࢖ΘΕΔ • ྫ֎͕ͳ͘ͱ΋ߦ൪߸ͷܭࢉʹ࢖ΘΕΔ

  70. None
  71. None
  72. ϓϩάϥϜΧ΢ϯλͷ࠷దԽ 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
  73. JITͷجૅ2 ελοΫϙΠϯλͷ࠷దԽ

  74. None
  75. ελοΫϙΠϯλͷ࠷దԽ • VMͷελοΫϙΠϯλͷҙٛ͸VMͷελοΫͷ ૢ࡞ʹ͋Δ • ωΠςΟϒͳελοΫ΍ϨδελͰಉ͡ૢ࡞Λ࣮ ݱͯ͠ελοΫϙΠϯλΛ΍ΊΒΕͳ͍͔?

  76. ελοΫϙΠϯλͷ࠷దԽ • ϝιουݺͼग़͢͠Δࡍʹ͸VMͷελοΫʹ໭ ͞ͳ͍ͱݺͼग़ͨ͠ϝιου͔ΒࢀরͰ͖ͳ͍ • େҬ୤ग़ޙʹ࢖ΘΕΔ

  77. None
  78. None
  79. None
  80. None
  81. ελοΫϙΠϯλͷ࠷దԽ 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
  82. ·͔ͩͳΓ͕ࠩ͋Δ 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
  83. Ͳ͏ͨ͠Βߋʹ଎͘ͳΔͷ͔? • PC/SPͷૢ࡞͸ผʹϘτϧωοΫͰ͸ͳ͍ͷͰ͜ ΕͰ͸଎͘ͳΒͳ͍ • Ή͠ΖRubyͷಈతͳಛੑͷαϙʔτͷͨΊʹ೉͠ ͘ͳ͍ͬͯΔ࠷దԽΛ࣮ݱ͢Δํ͕ॏཁ

  84. JITͷڧΈΛੜ͔ͨ͠࠷దԽ

  85. JITͷԠ༻1 ϝιουΠϯϥΠϯԽ

  86. None
  87. ϝιουΠϯϥΠϯԽ • 1+1Λܭࢉ͢ΔΑΓ΋Rubyͷϝιουݺͼग़͠ॲ ཧͷํ͕ෳࡶ • ϝιουݺͼग़͠ͷΦʔόʔϔουΛແʹͰ͖ ͳ͍͔? => ΠϯϥΠϯԽ

  88. ϝιουΠϯϥΠϯԽ • ΠϯϥΠϯԽ͢ΔͱɺίʔϧελοΫͷૢ࡞͕ͳ ͘ͳΔ͚ͩͰͳ͘ɺϝιουݺͼग़͠Λӽ͑ͯ࠷ దԽ͕ޮ͘ • ͔͠͠ΠϯϥΠϯԽͨ͠ޙਖ਼͍͠ڍಈʹద੾ʹ෮ ؼͰ͖Δඞཁ͕͋Δ • “Deoptimization”

    ͱ͍͏
  89. Rubyͷϝιουݺͼग़͠ͷ͘͠Έ • άϩʔόϧϝιουΩϟογϡͱΠϯϥΠϯϝ ιουΩϟογϡ͕͋Δ • ΠϯϥΠϯϝιουΩϟογϡ͸શͯͷcall siteʹ ରͯ͠ଘࡏ͢Δ • ͜ΕΛDeoptimizationʹ࢖͏

  90. ϝιουΠϯϥΠϯԽ • ΠϯϥΠϯԽ͢Δϝιου಺Ͱߦ൪߸Λऔಘͨ͠ Γྫ֎͕ඈΜͩΓ͠ͳ͍͜ͱΛอূ͠ͳ͍ͱ͍͚ ͳ͍ • VM໋ྩͷϝλσʔλΛ༻ҙ͓ͯ͘͠

  91. ϝιουΠϯϥΠϯԽ

  92. None
  93. None
  94. ϝιουΠϯϥΠϯԽ 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
  95. JITͷԠ༻2 Πϯελϯεม਺ͷ࠷దԽ

  96. Πϯελϯεม਺ͷ࠷దԽ • Πϯελϯεม਺ΛಡΉʹ͸Կ౓͔ϝϞϦͷϩʔ υΛ͠ͳ͍ͱ͍͚ͳ͍ => ஗͍ • ຊདྷϩʔυ͠ͳ͍ͱ͍͚ͳ͍஋ΛJITੜ੒ίʔυ಺ Ͱ͸ଈ஋ʹͰ͖ͳ͍͔?

  97. RubyͷΠϯελϯεม਺ͷ͘͠Έ • Ϋϥε͝ͱʹɺ͋ΔΠϯελϯεม਺͕֨ೲ͞Ε ͍ͯΔΠϯσοΫε͕ҟͳΔ • ܧঝͯ͠ಉ໋͡ྩྻ͕ڞ༗͞Ε͍ͯΔͱյΕΔ

  98. Πϯελϯεม਺ͷ࠷దԽ • ϝιουΠϯϥΠϯԽͱಉ༷ͷํ๏Ͱ Deoptimization͢Δ • Ճ͑ͯɺ༨ܭͳϒϥϯνΛ࡟Δ͜ͱͰίϯύΠϥ ͷ࠷దԽ͕ޮ͖΍͘͢͢Δ

  99. Πϯελϯεม਺ͷ࠷దԽ

  100. Πϯελϯεม਺ͷ࠷దԽ 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
  101. Ruby͸·ͩਐԽΛ࢒͍ͯ͠Δ (TruffleRuby ͸ 170fps ग़Δ)

  102. ·ͱΊ • Ruby͸࠷଎ͷݴޠͱͳΓಘΔ͔ʁ • ଞͷݴޠͰ΋ॻ͚Δίʔυ͸ɺେ఍͸ͦΕ૬౰ ʹ࠷దԽͰ͖Δ • Rubyಛ༗ͬΆ͍ݴޠػೳ͸અ౓ͷ͋Δར༻Λ