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

JITコンパイラはいかにRailsを速くするか / Rails Developers Meet...

JITコンパイラはいかにRailsを速くするか / Rails Developers Meetup 2017

Rails Developers Meetup 2017 / k0kubun

Takashi Kokubun

December 09, 2017
Tweet

More Decks by Takashi Kokubun

Other Decks in Programming

Transcript

  1. RailsͰग़དྷ͍ͯΔίϯϙʔωϯτୡ • ϓϩμΫτ • Treasure Data Service • Customer Data

    Platform • Enterprise Fluentd Management Console • ϚΠΫϩαʔϏε • Embulk config management API
  2. CRuby޲͚ͷJITͨͪ • RuJIT: TraceϕʔεɺϝϞϦ࢖༻ྔ͕໰୊ʹͳͬͨ໛༷ • Eclipse OMR: Optcarrotͩͱ଎͘ͳΒͳ͍Β͍͠ • LLRB:

    LLVMਏ͍ɺͪ͜Β͔Β͸Ҏ্ • MJIT: ଎͍ɺগͳ͍ϝϞϦফඅɺมͳґଘ΋ͳ͍ • YARV-MJIT: ͪΐͬͱMJITΑΓ஗͍͕ɺVMଆ͕όάΒͳ͍
  3. ࠓ೔͸MJIT, YARV-MJITͷ࿩͚ͩ͠·͢ • RuJIT: TraceϕʔεɺϝϞϦ࢖༻ྔ͕໰୊ʹͳͬͨ໛༷ • Eclipse OMR: Optcarrotͩͱ଎͘ͳΒͳ͍Β͍͠ •

    LLRB: LLVMਏ͍ɺͪ͜Β͔Β͸Ҏ্ • MJIT: ଎͍ɺগͳ͍ϝϞϦফඅɺมͳґଘ΋ͳ͍ • YARV-MJIT: ͪΐͬͱMJITΑΓ஗͍͕ɺVMଆ͕όάΒͳ͍
  4. MJITͱ͸ • GitHub: vnmakarov/ruby • CRubyͷͨΊͷϝιουJITίϯύΠϥ • JITίϯύΠϥ͚ͩͰͳ͘ɺϕʔεͷVM໋ྩ΋ஔ͖׵͑ • JIT༻pthread͕gcc΍clangΛfork,

    execͯ͠dlopen, dlsym • ͜ͷͨΊWindowsͰ͸ಈ͔ͳ͍ • http://rubykaigi.org/2017/presentations/vnmakarov.html
  5. YARV-MJITͱ͸ • GitHub: k0kubun/yarv-mjit • MJITΛΑΓอकతʹͨ͠ϑΥʔΫ, trunkʹ௥ैத • JITج൫Λվળͭͭ͠ɺVM໋ྩ͸ஔ͖׵͑ͳ͍ •

    ଎౓͸MJITʹগ͠ྼΔ͕ɺRuby 2.5ΑΓे෼ߴ଎ • pthread෦෼ΛWindows޲͚ʹҠ২ࡁɺMinGWͰ͸׬શʹಈ࡞
  6. ԿނYARV-MJITΛ։ൃ͍ͯ͠Δͷ͔ • RubyΛόάͳ͘ϦϦʔε͍ͯͨ͘͠Ίʹ͸ɺ͍͖ͳΓMJITΛೖΕΔͷ͸೉ͦ͠͏ • VM໋ྩͷஔ͖׵͑͸ϦεΩʔ (YARV͕ೖͬͨRuby 1.9΋࠷ॳ͸͔ͳΓյΕ͍ͯͨ໛༷) • JIT͸ΦϓγϣφϧͳͷͰࢼݧతʹಋೖ͠΍͍͕͢ɺVMͷมߋ͸όάΔͱճආͰ͖ͳ͍ •

    ͦΕͱಉ࣌ʹҠ২ੑΛղܾ͠ͳ͕ΒJITج൫΋ಋೖ͢Δඞཁ͕͋Δ • VMͦͷ··Ͱ΋ಈ͘γϯϓϧͳJIT (YARV-MJIT) ΛͱΓ͋͑ͣೖΕͯΈͨΒͲ͏͔ • ෦෼తʹupstreamʹ͋ΔͱMJIT͕ίϯϑϦΫτ͠ʹ͘͘ͳΓɺڞ༗͍ͯ͠ΔJITج൫΋ڠ ྗͯ͠։ൃ͠΍͘͢ͳΔ
  7. Ruby Grant 2017΍ͬͯ·͢ • Ruby Grant ͱ͸: 50ສԁΛ͍͍ͨͩͯRubyʹߩݙ͢Δౕ • ๻͸ϕϯνϚʔΫ؀ڥͷ੔උΛ΍͍ͬͯ·͢

    • benchmark_driver.gem • ैདྷͷϕϯνϚʔΧʔʹଘࡏ͢ΔProcݺͼग़͠ͷΦʔόʔϔου͕ͳͤ͘Δ • ϝϞϦ࢖༻ྔΛܭଌͨ͠ΓɺΞ΢τϓοτ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ϑΥʔΧε: • ؔ਺ͷΠϯϥΠϯԽ • ෆཁॲཧͷলུ • ίϯύΠϧ࣌ؒͷ୹ॖ •

    ͦΕͧΕRailsʹޮՌ͕͋Γͦ͏͔ߟ࡯Λ͍Ε·͢ • ΋͠શ෦ޮՌ͋ΔͳΒɺRailsͷRubyͷϨΠϠʔ͕ಉ͘͡Β͍଎͘ͳΔ͸ͣ
  12. ઓུ1: VMͷίʔυΛͳΔ΂͘ϔομʹؚΊΠϯϥΠϯԽ͢Δ • ର৅: ؔ਺ͷΠϯϥΠϯԽ (MJIT͔ΒͷҠ২) • ίϯύΠϥ͸ίϯύΠϥʹ౉͞Εͨίʔυ͔͠࠷దԽͰ͖ͳ͍ͷͰɺఆٛΛ౉͢ • ୯ʹ

    vm.c ΛϓϦϓϩηεͨ͠΋ͷΛϔομͱͯ͠Πϯετʔϧ • Railsʹޮ͔͘: ◦ • VM࣮ߦ͸໋ྩΛލ͍ͩ࠷దԽ͕Ͱ͖ͳ͍ͷͰɺେମ͋Δఔ౓͸ޮՌ͕͋Δ • ͨͩ͠ɺಛԽ໋ྩͷ͋ΔҰ෦ͷϝιου͸࠶ఆٛ͞Ε͍ͯͳ͍৔߹ʹྑ͘ޮ͘ͷͰɺASͷ ͍ͤͰ஗͘ͳΔౕ΋͋Δ͔΋
  13. ઓུ2: ϔομͷඇstaticؔ਺Λstaticʹ͢Δ • ର৅: ίϯύΠϧ࣌ؒͷ୹ॖ (MJIT͔ΒͷҠ২) • ະ࢖༻ͷstaticؔ਺͸ී௨ͷ -O2 ͳΒίϯύΠϧ͕ແࢹ͞ΕΔ͜ͱΛར༻

    • RubyͷεΫϦϓτ͕CͷίʔυΛਖ਼نදݱͰؾ߹Ͱม׵ (!?) • Railsʹޮ͔͘: ˕ • ίϯύΠϧ͕࣌ؒ଎͍ํ͕ͨ͘͞ΜίϯύΠϧͰ͖ΔͷͰɺ࣮֬ʹޮ͘
  14. ઓུ3: setjmpݺͼग़͠ΛεΩοϓͨ͠JITίʔυ௚઀ݺͼग़͠ • ର৅: ෆཁॲཧͷলུ (MJIT͔ΒͷҠ২) • Rubyͷྫ֎͸"setjmp", “longjmp"Ͱ࣮૷͞Ε͍ͯΔ͕ɺϚγϯͷίϯςΩετͷอଘ͕ඞཁͰ஗ ͍

    • ͜Ε͕ඞཁͳέʔεͷͨΊɺجຊతʹJITίʔυ͸VMΛىಈ͢Δؔ਺ʹϥοϓͯ͠ݺͼग़͍ͯ͠ Δ • ྫ֎͕ग़ͳͦ͏ͳόΠτίʔυͷ৔߹ʹɺͦͷϥοϓΛεΩοϓ͢Δ • Railsʹޮ͔͘: ◦ • ΞϓϦͰྫ֎Λ࢖͏ʹͯ͠΋RailsͷϨΠϠʔͰޮ͘৔ॴ͸͋Δఔ౓͋Γͦ͏
  15. ઓུ4: ࠷దԽʹ࣌ؒͷ͔͔Δؔ਺ͷΠϯϥΠϯԽΛආ͚Δ • ର৅: ίϯύΠϧ࣌ؒͷ୹ॖ (MJIT͔ΒͷҠ২) • CͷίʔυͷίϯύΠϧ࣌ؒ͸ɺύʔεͱ͔ڱٛͷίϯύΠϧ͸ҰॠͰɺ࠷దԽ͕஗͍ • ͳͷͰɺ࠷దԽ͕େมͳౕΛexternͰࢀর͢ΔΑ͏ʹ͢Ε͹ίϯύΠϧ͸଎͘ͳΔ

    • େมͳౕ: vm_search_method (ϝιου୳ࡧ) • ݺͼग़͢ͷ͕ඞཁͳ৔߹VM࣮ߦʹdeoptimizationΛ͢Δ • Railsʹޮ͔͘: ˚ • ͜Ε͸τϨʔυΦϑͷ͋Δ࠷దԽͰɺ͔ͳΓ௕͍࣌ؒ૸ΒͤΔRailsʹ͸ඍົͰ͢Ͷ
  16. ઓུ5: ؀ڥϙΠϯλͷॳظ஋ͷอଘ • ର৅: ෆཁॲཧͷলུ (MJIT͔ΒͷҠ২) • ͔ͳΓ͜Έ͍ͬͨ࿩ͳͷͰฉ͖ྲྀ͍ͯͩ͘͠͞ (Ͱ΋ޮՌ͋Δ) •

    YARV͸؀ڥϙΠϯλ(ep)ͱελοΫϙΠϯλ(sp)Λ࣋ͪɺͦΕΒ͸࠷ॳ͸ಉ͡஋ • deoptimization࣌spͷૢ࡞͕ཁΔ͕ɺϝιου಺ͰProcੜ੒ͳͲͰ؀ڥ͕࡞ΒΕͨ࣌ep͸ಈ͍ͯ͠·͏ • ͦͷࡍep͔Βͷspͷܭࢉ͕େมʹͳΔͷͰɺepͷॳظ஋Λอଘ͓ͯ͘͠ͱ଎͍ • Railsʹޮ͔͘: ˕ • Railsͩͱdeoptimizationͷػձଟͦ͏ͩ͠ɺޮ͖ͦ͏
  17. ઓུ6: VMʹؚ·Εͳ͍ϝιουͷΠϯϥΠϯԽ • ର৅: ؔ਺ͷΠϯϥΠϯԽ (MJIT͔ΒͷҠ২) • ϕʔεͷϔομ͸ vm.c ͔ΒͷΈ࡞͍ͬͯΔ͕ɺ౰વଞͷఆٛ΋͋ͬͨํ͕ྑ͍

    • optcarrotʹޮ͘ͷͰͱΓ͋͑ͣ Array#[] ͱ͔͚͖ͩ࣋ͬͯͯΈͨΒޮ͍ͨ • Railsʹޮ͔͘: ˕ • ͪΌΜͱ࣮૷ͷ࢓૊Έ͕൚༻ԽͰ͖Ε͹ޮ͖ͦ͏
  18. ઓུ7: ϝιουͷछྨ͝ͱʹҟͳΔϝιουݺͼग़͠ॲཧͷΠϯϥΠϯԽ • ର৅: ؔ਺ͷΠϯϥΠϯԽ (MJIT͔ΒͷҠ২) • CͰ࣮૷͞ΕͨϝιουͱRubyͰ࣮૷͞Εͨϝιουͷݺͼग़͠ॲཧ͸౰વҧ͏ • ͳͷͰVM໋ྩ͔ΒݟΔͱந৅Խ͞Ε͍ͯΔ

    • YARV͸όΠτίʔυʹΠϯϥΠϯΩϟογϡΛ͏Ί͜ΈɺͦΕ͕ϝιουͷλΠϓΛ ͍࣋ͬͯΔͷͰɺͦΕʹԠͯ͡ΠϯϥΠϯԽ͢Δ • Railsʹޮ͔͘: ˕ • ϝιουݺͼग़͕͠ଟ͍΄ͲޮՌ͕͋Δ