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

Cコンパイラを利用したRubyのJITコンパイラ / Programming Symposiu...

Cコンパイラを利用したRubyのJITコンパイラ / Programming Symposium 60

第60回 プログラミング・シンポジウム

Takashi Kokubun

January 12, 2019
Tweet

More Decks by Takashi Kokubun

Other Decks in Programming

Transcript

  1. ࣗݾ঺հ • ࠃ෼ ਸࢤ (@k0kubun) • ॴଐ: τϨδϟʔσʔλגࣜձࣾ (Arm͕ങऩ) •

    Rubyίϛολʔ 3೥໨ • JITίϯύΠϥΛ։ൃ • ςϯϓϨʔτΤϯδϯͷϝϯςφ
  2. RubyͱJITʹ͍ͭͯ • Ruby: ಈతܕ෇͚ͷΦϒδΣΫτࢦ޲εΫϦϓτݴޠ • ελοΫϕʔεͷόΠτίʔυΛ࣋ͭVirtual Machine • JITίϯύΠϧ: ࣮ߦ࣌ίϯύΠϧ

    • ຊൃදͰ͸ɺRuby VMͷόΠτίʔυͷ͔ΘΓʹɺJITͰੜ੒ ͨ͠ػցޠΛݺͼग़͢͜ͱͰ࠷దԽ͢Δ໨తʹ࢖͏
  3. ຊख๏ͷ֓ཁ 1. όΠτίʔυΛCͷίʔυʹม׵ + CͷϨϕϧͰ࠷దԽ 2. σΟεΫ (/tmp) ʹ .c

    ͷϑΝΠϧΛॻ͖ग़͢ 3. RubyΠϯλϓϦλ͕࣮ߦ࣌ʹCίϯύΠϥϓϩηεΛىಈ 4. ੜ੒ͨ͠ .so ϑΝΠϧΛಈతϩʔυ 5. όΠτίʔυͷ୅ΘΓʹϩʔυͨؔ͠਺ΛݺͿΑ͏ʹมߋ
  4. ܽ఺ 4: ϓϩηε؅ཧ෦෼ͷ อकίετͷ૿େ • Ruby͕ىಈͨ͠ࢠϓϩηεΛશͯwait͢Δϝιου͕͋ Δ͕ɺ౰વJIT༻GCC͸଴ͪͨ͘ͳ͍ • ͳͷͰɺϩοΫΛऔΓͭͭpidͷϦετΛ؅ཧ͢Δ •

    ϚϧνεϨου + Ϛϧνϓϩηε + γάφϧϋϯυϥ = ແݶͷrace condition • JITεϨου͸GVLͳ͠ɺϝΠϯͷεϨου΋JITεϨο υ΋fork͠·͘ΓɺSIGCHLDདྷ·͘Γ
  5. ܽ఺ 5: ηΩϡϦςΟ্ͷݒ೦ • CίϯύΠϥ΍ϔομɺϥΠϒϥϦ౳͕ࠩ͠ସ͑ΒΕΔͱࠔΔ • CίϯύΠϥ͸Ϗϧυ࣌ͱશ͘ಉ͡ϑϧύεΛ࢖͏ • /tmp ʹݩ͔ΒϑΝΠϧ͕͋Δ৔߹͸஄͘

    • ࢓૊Έ্Ͳ͏͠Α͏΋ͳ͍΋ͷ͸ׂΓ੾Δ • Ϣʔβʔ͕GCCΛࣗ༝ʹ࣮ߦͰ͖ΔΑ͏ͳϓϩάϥϜ͸ͦΕࣗମ͕੬ऑ • GCC΍ϔομɺੜ੒ίʔυ͕ࠩ͠ସ͑ΒΕΔύʔϛογϣϯΛऔΒΕͯ΋ෛ͚
  6. ࠷దԽ 1: ελοΫϙΠϯλɺ ϓϩάϥϜΧ΢ϯλͷҠಈ࡟ݮ • ελοΫϕʔεͷVM͕࢖͏ελοΫϙΠϯλͷૢ࡞͕ෆཁ • ྫ֎ͰେҬ୤ग़ͯ͠΋ελοΫ͸໭ͤͳ͍ͱ͍͚ͳ͍ͷ Ͱɺྫ֎Λcatch͠ͳ͍ϝιουʹݶΔ •

    όΠτίʔυͷσΟεύονʹ࢖͏ϓϩάϥϜΧ΢ϯλͷ ૢ࡞͕ෆཁ • ߦ൪߸ΛऔΔϝιουݺͼग़͠ͷखલͰ͸Ͳ͏ʹ͔෮׆ ͤ͞Δඞཁ͕͋Δ
  7. ࠷దԽ 2: ΠϯϥΠϯԽΛར༻ͨ͠ ίϯύΠϥʹΑΔ࠷దԽ • ݱࡏ͸ओʹόΠτίʔυ໋ྩͷ࣮૷͕ੜ੒ίʔυ಺͔ΒΠ ϯϥΠϯԽՄೳ • Ϧςϥϧͷ஋ͱόΠτίʔυ໋ྩͷ࣮૷ΛΠϯϥΠϯ Խ͢Δ͚ͩͰɺ1

    + 2 ʹର͠ 3 Λੜ੒Ͱ͖Δ • CͷίϯύΠϥ͸ؔ਺αΠζ͕େ͖͍ͱΠϯϥΠϯԽ͠ ͳ͘ͳΔͷͰɺ໌ࣔతͳΠϯϥΠϯԽࢦఆ͕ඞཁͳ͜ ͱ΋
  8. ϚΠΫϩϕϯνϚʔΫ: ଎౓ Ruby 2.6.0 w/ https://github.com/benchmark-driver/mjit-benchmarks JIT off ͱൺֱͨ͠଎౓ൺ཰ 0

    2 4 5 7 aread aref aset aw rte call const2 const fannk fib ivread ivw rite m andelbrot m eteor nbody nest-ntim es nest-w rite norm nsvb red-black sieve trees w hile 6.6 1.2 1.7 1.1 1.0 1.4 2.4 1.3 1.2 1.0 1.3 4.6 3.6 2.8 1.0 5.5 5.5 0.9 2.6 2.7 4.6 2.8 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 JIT off JIT on Intel 4.0GHz i7-4790K 8ίΞ, ϝϞϦ16GB, x86-64 Ubuntu
  9. ϚΠΫϩϕϯνϚʔΫ: ϝϞϦ Ruby 2.6.0 w/ https://github.com/benchmark-driver/mjit-benchmarks Max Resident Set Size

    (MB) 0 13 25 38 50 aread aref aset aw rte call const2 const fannk fib ivread ivw rite m andelbrot m eteor nest-ntim es nest-w hile norm nsvb w hile 38.2 43.7 38.3 42.1 38.3 44.2 45.1 38.3 38.2 38.3 38.3 38.3 38.2 38.3 38.3 38.3 38.3 38.3 14.4 14.5 14.9 14.2 14.4 15.3 14.2 14.3 14.4 14.3 15.0 14.2 14.2 14.8 14.2 14.3 14.3 14.4 JIT off JIT on Intel 4.0GHz i7-4790K 8ίΞ, ϝϞϦ16GB, x86-64 Ubuntu
  10. ϚΠΫϩϕϯνϚʔΫ: ϝϞϦ Ruby 2.6.0 w/ https://github.com/benchmark-driver/mjit-benchmarks Max Resident Set Size

    (MB) 0 100 200 300 400 red-black sieve trees 89.4 193.9 299.4 89.4 193.5 301.7 JIT off JIT on Intel 4.0GHz i7-4790K 8ίΞ, ϝϞϦ16GB, x86-64 Ubuntu
  11. ΞϓϦέʔγϣϯੑೳ 1: ଎౓ Intel 4.0GHz i7-4790K 8ίΞ, ϝϞϦ16GB, x86-64 Ubuntu

    Ruby 2.6.0 w/ NESΤϛϡϨʔλ Optcarrot Frames Per Second 0 23 45 68 90 86.6 53.8 JIT off JIT on
  12. ΞϓϦέʔγϣϯੑೳ 1: ϝϞϦ Intel 4.0GHz i7-4790K 8ίΞ, ϝϞϦ16GB, x86-64 Ubuntu

    Ruby 2.6.0 w/ NESΤϛϡϨʔλ Optcarrot Max Resident Set Size (MB) 0 16 32 48 64 63.7 62.8 JIT off JIT on
  13. ΞϓϦέʔγϣϯੑೳ 2: ଎౓ Intel 4.0GHz i7-4790K 8ίΞ, ϝϞϦ16GB, x86-64 Ubuntu

    Ruby 2.6.0 w/ https://github.com/benchmark-driver/sinatra Request Per Second 0 2,125 4,250 6,375 8,500 7,466.5 8,442.1 JIT off JIT on
  14. ΞϓϦέʔγϣϯੑೳ 2: ϝϞϦ Intel 4.0GHz i7-4790K 8ίΞ, ϝϞϦ16GB, x86-64 Ubuntu

    Ruby 2.6.0 w/ https://github.com/benchmark-driver/sinatra Max Resident Set Size (MB) 0 18 35 53 70 64.4 23.7 JIT off JIT on
  15. ੑೳิ଍ Intel 4.0GHz i7-4790K 8ίΞ, ϝϞϦ16GB, x86-64 Ubuntu ʹ͓͍ͯɺ •

    1ճͷJITίϯύΠϧʹ͔͔Δ࣌ؒ͸ 60ʙ250 ms ఔ౓ • JIT༗ޮʹ͢Δͱɺ࣮ࡍʹJIT͍ͯ͠ͳͯ͘΋ϐʔΫ࣌ϝϞϦফඅྔ ͕24ʙ28MBఔ౓૿͑Δ͕ɺϝιου1ͭ͸8KBͱ͔ͳͷͰޡࠩ • 24ʙ28MB෦෼͸ະௐࠪ: pthread, mutex, ϑϥάͷจࣈྻ, ...? • GCCͷϝϞϦফඅ: 4.6MBఔ౓, ClangͷϝϞϦফඅ: 24.1MBఔ౓ • /tmp ͷফඅ͸1ϝιου͋ͨΓ࠷௿ .c 12KBɺ.o 4KBɺ.so 8KB