Pro Yearly is on sale from $80 to $50! »

Ruby 3.0 JIT on Rails

Ruby 3.0 JIT on Rails

08d5432a5bc31e6d9edec87b94cb1db1?s=128

Takashi Kokubun

July 03, 2020
Tweet

Transcript

  1. Ruby 3.0 JIT on Rails ۜ࠲Rails#23 Takashi Kokubun / @k0kubun

  2. ࣗݾ঺հ • GitHub, Twitter: @k0kubun • Arm Treasure Data •

    Backend, SRE, API • Ruby ίϛολ • JIT, VM, ERB, Πϯϑϥ, ϕϯνϚʔΫ
  3. None
  4. એ఻

  5. એ఻

  6. એ఻

  7. ࠓ೔࿩͢͜ͱ • Ruby 3.0 JIT ͷݱࡏͷੑೳ • Rails ͰͷϘτϧωοΫͷվળ •

    C Λ Ruby ʹॻ͖׵͑ͯΔ࿩ • ࠓޙͷ JIT ΞʔΩςΫνϟ
  8. Ruby 3.0 JIT ͷݱࡏͷੑೳ

  9. mame/optcarrot 3VCZ 3VCZ 3VCZ GSBNFTTFD     

          7. +*5
  10. benchmark-driver/sinatra 3VCZ 3VCZ 3VCZ SFRVFTUTTFD     

                7. +*5
  11. k0kubun/railsbench 3VCZ 3VCZ 3VCZ SFRVFTUTTFD     

          7. +*5
  12. ͜Ε·Ͱͷੑೳվળͷ֓ཁ • 2.6: Optcarrot޲্͸ݱঢ়͜ͷόʔδϣϯͷΈɻJIT compaction͸Railsʹޮ͘ɻ • 2.7: Ұ෦ϝιουͷΠϯϥΠϯԽɻmax cacheσϑΥϧτมߋ͸ҰԠRailsʹޮ͘ɻ •

    3.0: builtinϝιουͷΠϯϥΠϯԽɻRails޲͚ͷ࠷దԽ͕͍͔ͭ͘ (͜Ε͔Βઆ໌)
  13. Rails ͰͷϘτϧωοΫͷվળ

  14. Optcarrot ͱ Sinatra, Rails ͷҧ͍͸? • ط஌ͷࣄ࣮: Optcarrot͕଎͘Ͱ͖Δͷ͸Πϯελϯεม਺ͷ࠷దԽ͕ Α͘ޮͨ͘Ί •

    ͔͠͠ɺSinatra΍Rails͕஗͘ͳΔͷ͸Կނ͔? • CPUͷͲͷϨΠϠʔͰϘτϧωοΫʹͳ͍ͬͯΔ?
  15. VTune: mame/optcarrot - VM • VTune: VM, JIT

  16. VTune: mame/optcarrot - JIT • VTune: VM, JIT

  17. VTune: benchmark-driver/sinatra - VM

  18. VTune: benchmark-driver/sinatra - JIT

  19. VTune: k0kubun/railsbench - VM

  20. VTune: k0kubun/railsbench - JIT

  21. ϑϩϯτΤϯυͷׂ߹͕૿͍͑ͯΔ • ໋ྩͷ࣮ߦͰ͸ͳ͘ɺ໋ྩͷfetchͰstall͍ͯ͠Δ • CPU 1αΠΫϧ͋ͨΓʹ࣮ߦ͞ΕΔ໋ྩ਺͕গͳ͍ • VMͷ࣌఺ͰOptcarrotͱ͸͕ࠩ͋Δ͕ɺJITͰѱԽ͍ͯ͠Δͷ͸ ICache Miss

  22. Instruction Cache (ICache) Miss ͱ͸ • Α͋͘ΔCPUͷΩϟογϡ: L1i, L1d, L2,

    L3 (shared) • L1i: Level 1 Instruction Ωϟογϡ • CPU໋ྩͷΩϟογϡϛε • ಉ࣌ʹΞΫηε͢Δίʔυ͕ผͷcacheline΍ϖʔδʹ͋ΔͳͲ (ίʔυͷαΠζ͔ϨΠΞ΢τͷ໰୊)
  23. ICache Miss ΛͲ͏ճආ͢Δ͔ • ίʔυαΠζͷ࡟ݮ • ॏෳΛഉআ͢Δ • ࣮ߦ࣌৘ใʹج͍ͮͯෆཁͳύεΛ࡟Δ •

    ϨΠΞ΢τͷվળ • ϝιου͝ͱʹผϖʔδʹόϥόϥͳͷ͸2.6 JIT compactionͰղܾ • hot path ͷہॴੑΛ্͛Δ
  24. ॏෳΛഉআ͢Δ • 3.0 ʹೖͬͨ࠷దԽ: • JIT compactionͰɺ.o Ͱ͸ͳ͘.c͔ΒίϯύΠϧ͠௚͢ (staticؔ਺͕ dedup͞ΕΔ)

    • ivar͕࠷దԽ͞Εͳ͔ͬͨ࣌ʹ࢖ΘΕΔؔ਺ͷΠϯϥΠϯԽΛ΍ΊΔ • JIT cancel࣌ͷෳ਺ͷؔ਺ݺͼग़͠Λ1ͭʹ·ͱΊΔ
  25. ࣮ߦ࣌৘ใʹج͍ͮͯෆཁͳύεΛ࡟Δ • 3.0 ʹೖͬͨ࠷దԽ: • ϝιουݺͼग़͠ͰϨγʔόͷΫϥε͕ඇଈ஋ͳΒɺଈ஋޲͚ͷ෼ ذΛআڈ͢Δ

  26. hot path ͷہॴੑΛ্͛Δ • 3.0 ʹೖͬͨ࠷దԽ: • ϝιου୳ࡧͳͲͷύεʹcold attributeΛ͚ͭΔ •

    stackoverflow࣌΋coldԽ • JITίϯύΠϧͷenqueue΋coldԽ
  27. ͋ͱ͸Ͳ͏͢Δ͔ • ·ͩ஗͘ͳΔݪҼʹͳͬͯͦ͏ͩ͠ɺॏෳ΋͋Γͦ͏ • ͔͠͠୯ʹΠϯϥΠϯԽΛແޮԽ͢ΔͱͦΕͰ஗͘ͳΔ͜ͱ΋͋Δ ͷͰɺ͜ΕҎ্͸৻ॏʹ΍Δඞཁ͕͋Δ • ϝιου͋ͨΓͷίʔυαΠζ͕ҰؾʹݮΕ͹ɺJIT͍͍ͯ͠࠷େϝ ιου਺΋૿΍ͤΔ͔΋

  28. C Λ Ruby ʹॻ͖׵͑ͯΔ࿩

  29. RubyKaigi 2018

  30. RubyKaigi 2019

  31. None
  32. None
  33. Builtin ϝιουͱ͸ • RubyίΞͷ࣮૷Ͱ͔͠࢖͑ͳ͍ػೳ • ISeqͷόΠφϦঢ়ଶͰอଘ͞Ε͓ͯΓɺϩʔυίετ͕௿͍ • ΩʔϫʔυҾ਺ΛऔΔϝιου͸ɺRubyͰ࣮૷ͨ͠ํ͕ߴ଎ • RubyͰϝιουΛఆٛ͢Δ͕ɺCͷؔ਺͕௿ίετͰݺͼग़ͤΔ

  34. Builtin ϝιουͱ͸

  35. Builtin attr ͱ͸ • LLVMͳͲͳ͠ͰCͷؔ਺ͷڍಈΛղੳ͢Δͷ͸೉͍͠ • ͦ͜ͰɺϝιουͷಛੑΛΞϊςʔγϣϯ͓͖ͯ͠ɺJIT͸ͦΕΛ৴͡Δ • RubyͷCIͳͲͰΞϊςʔγϣϯ͕ਖ਼͍͔͠͸࣮ߦ࣌ʹݕ͍ࠪͯ͠Δ

  36. Builtin attr ͷҰཡ • inline: ϝιουϑϨʔϜΛpush͠ͳͯ͘΋ྑ͍ • ݱঢ়͜Ε͚ͩɻJIT͕ϝιουϑϨʔϜͷpushΛεΩοϓ͍ͯ͠Δ

  37. Builtin attr inline Λ͚ͭΔྫ

  38. ϝιουϑϨʔϜεΩοϓͷޮՌ ,FSOFMDMBTT JT      7. +*5

    +*5 GSBNFTLJQ 1.3x 1.7x
  39. ϝιουϑϨʔϜεΩοϓʹҙຯ͸͋Δͷ͔? • Rails͸ϝιουݺͼग़͕͠ଟͦ͏ • ͨͩ͠ɺ͋Δϝιου͋ͨΓͷݺͼग़͠ճ਺͸ͦΕ΄Ͳଟ͘ͳ͘ɺ ΍΍ෆར • Cͷؔ਺ఆٛ͝ͱΠϯϥΠϯԽͨ࣌͠ʹ࠷దԽ࣌ͷϊΠζ͕ݮΔ • 1ճ͋ͨΓ

    4ns / 16 clocks ఔ౓ޮՌ͕ݟΒΕΔ
  40. ϝιουϑϨʔϜεΩοϓͷ৚݅ • ϝιουݺͼग़͠Λ͠ͳ͍ • ྫ֎Λ౤͛ͳ͍ • TypeError, ArgumentError, NoMemoryError ΋֘౰͢Δ

    • όοΫτϨʔεΛݟͳ͍
  41. ࠓޙͷ JIT ΞʔΩςΫνϟ

  42. C2 ΍ Graal ͕ѻ͍ͬͯΔάϥϑ

  43. MJIT͕΍͍ͬͯΔ͜ͱ • VMಛ༗ͷΦʔόʔϔουͷআڈ • VM໋ྩ୯ҐͰͷ࣮ߦ࣌৘ใΛ࢖ͬͨ࠷దԽ • ϝιουͷΠϯϥΠϯԽ • ࢒Γͷ࠷దԽ͸CίϯύΠϥ೚ͤ

  44. JITʹظ଴͢Δ͜ͱ • ίϯύΠϧ࣌ܭࢉ • Constant folding • Dead code elimination

    • Loop invariant motion • ϝϞϦΞΫηεͷ࠷దԽ
  45. MJIT ͷݶք • CίϯύΠϥ͸ Ruby VM ಛ༗ͷࣄ৘ΛߟྀͰ͖ͳ͍ • ΠϯϥΠϯԽͷൣғʹݶք͕͋Δ (C

    ϝιου౳) • ΍Γ͗͢ΔͱίʔυαΠζ͕૿͑ٯޮՌ • ਖ਼ৗܥΛ༏ઌͯ͠࠷దԽͤ͞Δͱ͍ͬͨࢦ͕ࣔ೉͍͠
  46. ͱ͸͍͑ • JIT ແޮͷ࣌ʹ஗͘ͳΔखஈ͸औΓͨ͘ͳ͍ • JITͰ΋଎͍ͷ͸؆୯ͳίϚϯυͳͲͰศར • ϝϯς͕େมʹͳΔखஈ΋औΓͨ͘ͳ͍ • VM໋ྩ͕૿͑ͯ΋جຊతʹ͸JIT͸ಈ͖ଓ͚ͯཉ͍͠

    • VMͷڍಈͱ͸શ͘ಠཱͨ͠ίʔυੜ੒΋ͨ͘͠ͳ͍
  47. ͡Ό͋Ͳ͏͢Δ͔ 1. ίϯύΠϧ࣌ܭࢉʹඞཁͳ΋ͷΛἧ͑Δ 2. ୯७ͳಠࣗIR͔ΒVMʹ୤࠷దԽ͢Δج൫Λ੔͑Δ 3. ΑΓߴ౓ͳ࠷దԽʹదͨ͠IRߏ଄ʹม͑Δ

  48. 1. ίϯύΠϧ࣌ܭࢉʹඞཁͳ΋ͷΛἧ͑Δ • CϝιουͷΞϊςʔγϣϯΛ૿΍͢ (Τεέʔϓ͢Δ͔ɺ७ਮ͔) • ࣮ߦ࣌ͷܕ৘ใ͕ΑΓଟ͘ͷ৔ॴͰऔΕΔΑ͏ʹ͢Δ (opt໋ྩͳͲ) • Cϝιου,

    super, yield, ఆ਺ ͳͲͷΠϯϥΠϯԽରԠΛਐΊΔ
  49. 2. ୯७ͳಠࣗIR͔ΒVMʹ୤࠷దԽ͢Δج൫ • YARV໋ྩΛJITಠࣗͷIRʹ෼ղՄೳʹ͠ɺ໋ྩ/IRྻʹ࠷దԽΛ͔͚Δ • ࠷దԽͨ͠ޙͷ໋ྩ/IRྻͰ୤࠷దԽ͕ൃੜ͢Δ೚ҙͷ৔ॴ͔ΒVM ࣮ߦʹ໭ͤΔঢ়ଶΛҡ࣋͢Δ • On-Stack Replacement

    (JIT → VM) ΋ͦͷ͏࣮ͪ૷
  50. 3. ΑΓߴ౓ͳ࠷దԽʹదͨ͠IRߏ଄ʹม͑Δ • ୤࠷దԽͷج൫͕੔ͬͨΒɺͦΕΛҡ࣋͠ͳ͕Β࠷దԽʹదͨ͠IRά ϥϑʹม͍͑ͯ͘ • ίϯύΠϥ͸Ruby (on Ractor) Ͱ࣮૷͍ͨ͠

  51. ߴ౓ͳ࠷దԽͷྫ (ࠓ͸CͰॻ͍͍ͯΔ) • ෳ਺໋ྩؒͰॏෳ͍ͯ͠ΔܕνΣοΫͷϚʔδ (ࠓ೔ॻ͍ͨ) • Optcarrot fps: 83.3 →

    93.0
  52. ଞʹ΍Γ͍ͨͱࢥ͍ͬͯΔ͜ͱ • ෭࡞༻ͷͳ͍ϝιουͷίϯύΠϧ࣌ݺͼग़͠ • [1, 2].min ͰΞϊςʔγϣϯʹج͍ͯ rb_ary_min ΛίϯύΠϧ࣌ʹݺͼग़͢౳ •

    Τεέʔϓ͠ͳ͍ΦϒδΣΫτͷࣄલׂΓ౰ͯ • ҎલStringΛελοΫׂ෇͢ΔPoCΛ࡞͕ͬͨɺͦ΋ͦ΋Ξϩέʔγϣϯ͕ͳ ͯ͘ࡁΉ৔߹͸ͦΕ͕࠷଎ • Frozen string literal ͳͲΛෆཁʹ͍ͨ͠
  53. ·ͱΊ • ࠓ͸JITʹΑΔ࠷దԽ͕ࠔ೉ͳͱ͜Ζͷ଍ճΓΛ੔͍͑ͯΔஈ֊ • Rails͕଎͘Ͱ͖Δ·ͰͷಓͷΓ͸௕͍͕ɺ3.0Ͱ΋ண࣮ʹਐḿ͍ͯ͠Δ