Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ruby 3.0 JIT on Rails

Ruby 3.0 JIT on Rails

Takashi Kokubun

July 03, 2020
Tweet

More Decks by Takashi Kokubun

Other Decks in Programming

Transcript

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

    Backend, SRE, API • Ruby ίϛολ • JIT, VM, ERB, Πϯϑϥ, ϕϯνϚʔΫ
  2. ࠓ೔࿩͢͜ͱ • Ruby 3.0 JIT ͷݱࡏͷੑೳ • Rails ͰͷϘτϧωοΫͷվળ •

    C Λ Ruby ʹॻ͖׵͑ͯΔ࿩ • ࠓޙͷ JIT ΞʔΩςΫνϟ
  3. mame/optcarrot 3VCZ 3VCZ 3VCZ GSBNFTTFD     

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

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

    ͔͠͠ɺSinatra΍Rails͕஗͘ͳΔͷ͸Կނ͔? • CPUͷͲͷϨΠϠʔͰϘτϧωοΫʹͳ͍ͬͯΔ?
  6. Instruction Cache (ICache) Miss ͱ͸ • Α͋͘ΔCPUͷΩϟογϡ: L1i, L1d, L2,

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

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

    • ivar͕࠷దԽ͞Εͳ͔ͬͨ࣌ʹ࢖ΘΕΔؔ਺ͷΠϯϥΠϯԽΛ΍ΊΔ • JIT cancel࣌ͷෳ਺ͷؔ਺ݺͼग़͠Λ1ͭʹ·ͱΊΔ
  9. MJIT ͷݶք • CίϯύΠϥ͸ Ruby VM ಛ༗ͷࣄ৘ΛߟྀͰ͖ͳ͍ • ΠϯϥΠϯԽͷൣғʹݶք͕͋Δ (C

    ϝιου౳) • ΍Γ͗͢ΔͱίʔυαΠζ͕૿͑ٯޮՌ • ਖ਼ৗܥΛ༏ઌͯ͠࠷దԽͤ͞Δͱ͍ͬͨࢦ͕ࣔ೉͍͠
  10. ଞʹ΍Γ͍ͨͱࢥ͍ͬͯΔ͜ͱ • ෭࡞༻ͷͳ͍ϝιουͷίϯύΠϧ࣌ݺͼग़͠ • [1, 2].min ͰΞϊςʔγϣϯʹج͍ͯ rb_ary_min ΛίϯύΠϧ࣌ʹݺͼग़͢౳ •

    Τεέʔϓ͠ͳ͍ΦϒδΣΫτͷࣄલׂΓ౰ͯ • ҎલStringΛελοΫׂ෇͢ΔPoCΛ࡞͕ͬͨɺͦ΋ͦ΋Ξϩέʔγϣϯ͕ͳ ͯ͘ࡁΉ৔߹͸ͦΕ͕࠷଎ • Frozen string literal ͳͲΛෆཁʹ͍ͨ͠