Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Cコンパイラを利用したRubyのJITコンパイラ / Programming Symposiu...
Search
Takashi Kokubun
January 12, 2019
Programming
6
2.9k
Cコンパイラを利用したRubyのJITコンパイラ / Programming Symposium 60
第60回 プログラミング・シンポジウム
Takashi Kokubun
January 12, 2019
Tweet
Share
More Decks by Takashi Kokubun
See All by Takashi Kokubun
YJIT Makes Rails 1.7x faster / RubyKaigi 2024
k0kubun
7
12k
Ruby JIT Hacking Guide / RubyKaigi 2023
k0kubun
2
9.4k
YJIT: Dive into Ruby's JIT compiler written in Rust / Rust.Tokyo 2022
k0kubun
1
2k
Towards Ruby 4 JIT / RubyKaigi 2022
k0kubun
3
11k
Optimizing Production Performance with MRI JIT / RubyConf 2021
k0kubun
1
410
Why Ruby's JIT was slow / RubyKaigi Takeout 2021
k0kubun
3
1.8k
数時間かかる週一リリースを毎日何度も爆速でできるようにするまで / CI/CD Conference 2021
k0kubun
21
14k
Ruby 3 JIT's roadmap / RubyConf China 2020
k0kubun
0
760
Ruby 3.0 JIT on Rails
k0kubun
9
9.1k
Other Decks in Programming
See All in Programming
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
0
100
TypeScript Graph でコードレビューの心理的障壁を乗り越える
ysk8hori
2
1.1k
Tauriでネイティブアプリを作りたい
tsucchinoko
0
370
Hotwire or React? ~アフタートーク・本編に含めなかった話~ / Hotwire or React? after talk
harunatsujita
1
120
距離関数を極める! / SESSIONS 2024
gam0022
0
280
Remix on Hono on Cloudflare Workers
yusukebe
1
280
ローコードSaaSのUXを向上させるためのTypeScript
taro28
1
610
Quine, Polyglot, 良いコード
qnighy
4
640
LLM生成文章の精度評価自動化とプロンプトチューニングの効率化について
layerx
PRO
2
190
TypeScriptでライブラリとの依存を限定的にする方法
tutinoko
2
660
エンジニアとして関わる要件と仕様(公開用)
murabayashi
0
280
Ethereum_.pdf
nekomatu
0
460
Featured
See All Featured
GraphQLとの向き合い方2022年版
quramy
43
13k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
364
24k
Agile that works and the tools we love
rasmusluckow
327
21k
Speed Design
sergeychernyshev
24
610
YesSQL, Process and Tooling at Scale
rocio
169
14k
What's new in Ruby 2.0
geeforr
343
31k
Building Your Own Lightsaber
phodgson
103
6.1k
Fashionably flexible responsive web design (full day workshop)
malarkey
405
65k
Building Adaptive Systems
keathley
38
2.3k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
16
2.1k
Embracing the Ebb and Flow
colly
84
4.5k
Keith and Marios Guide to Fast Websites
keithpitt
409
22k
Transcript
CίϯύΠϥΛར༻ͨ͠ RubyͷJITίϯύΠϥ ࠃਸࢤ / Arm Treasure Data ୈ60ճ ϓϩάϥϛϯά γϯϙδϜ
ࣗݾհ • ࠃ ਸࢤ (@k0kubun) • ॴଐ: τϨδϟʔσʔλגࣜձࣾ (Arm͕ങऩ) •
Rubyίϛολʔ 3 • JITίϯύΠϥΛ։ൃ • ςϯϓϨʔτΤϯδϯͷϝϯςφ
LLRB - LLVMϕʔεͷRubyͷJIT
Ruby 2.6ͰͷJIT։ൃ
ΞδΣϯμ • RubyͷJITख๏ͷ֓ཁ • རɺܽɺ࣮ݱՄೳͳ࠷దԽྫ • ੑೳධՁ
RubyͷJITख๏ͷ֓ཁ
RubyͱJITʹ͍ͭͯ • Ruby: ಈతܕ͚ͷΦϒδΣΫτࢦεΫϦϓτݴޠ • ελοΫϕʔεͷόΠτίʔυΛ࣋ͭVirtual Machine • JITίϯύΠϧ: ࣮ߦ࣌ίϯύΠϧ
• ຊൃදͰɺRuby VMͷόΠτίʔυͷ͔ΘΓʹɺJITͰੜ ͨ͠ػցޠΛݺͼग़͢͜ͱͰ࠷దԽ͢Δతʹ͏
RubyʹJITΛಋೖ͢Δഎܠͱత എܠ: • RubyWebΞϓϦέʔγϣϯʹΑ͘ΘΕΔ త: • WebΞϓϦέʔγϣϯαʔόʔͳͲظ࣮ߦ͢Δϓϩ άϥϜͰੑೳΛ্͛ɺϢʔβʔମݧΛ্ͤͨ͞Γɺ αʔόʔΛݮΒͨ͠Γ͍ͨ͠
ຊख๏ͷ֓ཁ • ΠϯλϓϦλىಈ࣌ʹJIT༻ͷεϨουΛىಈ • Ruby 2.6ͷ࣌ͰσϑΥϧτͰͳ͍ͷͰɺཁ --jit • ϝΠϯͷεϨουͰ5ճҎ্ݺͼग़͞Εͨϝιου͕ Ωϡʔʹੵ·ΕΔ
• JIT༻ͷεϨου͕ϝιου୯ҐͰόΠτίʔυΛػցޠ ʹίϯύΠϧ
ຊख๏ͷ֓ཁ 1. όΠτίʔυΛCͷίʔυʹม + CͷϨϕϧͰ࠷దԽ 2. σΟεΫ (/tmp) ʹ .c
ͷϑΝΠϧΛॻ͖ग़͢ 3. RubyΠϯλϓϦλ͕࣮ߦ࣌ʹCίϯύΠϥϓϩηεΛىಈ 4. ੜͨ͠ .so ϑΝΠϧΛಈతϩʔυ 5. όΠτίʔυͷΘΓʹϩʔυͨؔ͠ΛݺͿΑ͏ʹมߋ
ຊख๏ͷ֓ཁ
ຊख๏ͷ֓ཁ
None
RubyͷJITख๏ͷར
ར 1: ΞηϯϒϦίʔυੜ͔Βͷ։์ • ؆୯: ςϯϓϨʔτΤϯδϯͰಈతͳCίʔυੜ • VMΛ࣮͢Δ͚ͩͰσϑΥϧτͷJIT࣮ΛࣗಈੜͰ͖Δ • ϙʔλϒϧ:
ΠϯλϓϦλ͕ϏϧυͰ͖ΔڥͰ͑Δͣ • Cͷ༷͕มΘΒͳ͍ͷͰLLVMͷΑ͏ʹඇޓʹৼΓճ͞Εͳ͍
ར 2: σόοΨΛͬͨղੳίετͷԼ • gdbͰσόοά͢ΔࡍɺVMͷ࣮ͱಉ͡C͚ͩΛݟΔ͜ ͱʹͳΔ • ίʔυੜ͕ࣗલͳΒɺσόοάใೖΕΔͷࣗલʹͳ Γେมͦ͏
ར 3: ϨΠϠʔͷ࠷దԽͷ CίϯύΠϥͷҠৡ • ༷ʑͳ࠷దԽςΫχοΫΛ࠶ൃ໌͢ΔϦιʔεͳ͍ • ੜίʔυ͔ΒVMͷؔΛ༰қʹΠϯϥΠϯԽՄೳ
ར 4: ෳͷίϯύΠϥج൫͕׆༻Մೳ • GCCͱClang/LLVMͷ྆ํ͕ར༻ՄೳͰɺVisual Studioʹ ରԠ • Ζ͏ͱࢥ͑LLVM PassΛࣗ࡞ͯ͠
opt(1) ͔Β͑Δ ͔͠Εͳ͍
RubyͷJITख๏ͷܽ
ܽ 1: ຊख๏ͷϩʔυํ๏ʹΑΔ Φʔόʔϔου • dlopen ͢Δͱ֤ϝιου(.soϑΝΠϧ)͕3ͭͷϖʔδʹׂ ͞Εͯϩʔυ͞ΕɺΩϟογϡʹѱӨڹ • ͳͷͰɺޙͰෳͷϝιουΛ·ͱΊͯ࠶ίϯύΠϧ͍ͯ͠
Δ͕ɺ໘
ܽ 1: ຊख๏ͷϩʔυํ๏ʹΑΔ Φʔόʔϔου ৄࡉ: http://shinh.hatenablog.com/entry/2018/06/10/235314
ܽ 1: ຊख๏ͷϩʔυํ๏ʹΑΔ Φʔόʔϔου https://github.com/ruby/ruby/pull/1921
ܽ 2: ࣮ߦ࣌ґଘͷෳࡶԽ - CίϯύΠϥ • ͜Ε·ͰɺCίϯύΠϥΠϯλϓϦλ͔Β࣮ߦՄೳͳඞ ཁͳ͔ͬͨ • ϓϦίϯύΠϧυϔομCίϯύΠϥͷόʔδϣϯʹґ
ଘ͢ΔͷͰɺຖճίϯύΠϧ
ܽ 3: ίϯύΠϥ͝ͱͷࠩҟͷରԠ • CίϯύΠϥ͝ͱʹҟͳΔΦϓγϣϯʹରԠ͢Δඞཁ͕͋Δ • جຊతʹΠϯλϓϦλͷϏϧυͱಉ͡CFLAGS͕ඞਢͩ ͕ɺҰ෦ٯʹΒͳ͍ͱಈ͔ͳ͍ • GCCʹউखͳύον͕ͨͬͯΔOSͷσΟετϦϏϡʔ
γϣϯ͋Δ • Visual StudioϓϦϓϩηε͚ͩͯ͠ग़ྗ͕Ͱ͖ͣશ͘ผͷ ϏϧυγεςϜ
ܽ 4: ϓϩηεཧ෦ͷ อकίετͷ૿େ • Ruby͕ىಈͨ͠ࢠϓϩηεΛશͯwait͢Δϝιου͕͋ Δ͕ɺવJIT༻GCCͪͨ͘ͳ͍ • ͳͷͰɺϩοΫΛऔΓͭͭpidͷϦετΛཧ͢Δ •
ϚϧνεϨου + Ϛϧνϓϩηε + γάφϧϋϯυϥ = ແݶͷrace condition • JITεϨουGVLͳ͠ɺϝΠϯͷεϨουJITεϨο υfork͠·͘ΓɺSIGCHLDདྷ·͘Γ
ܽ 5: ηΩϡϦςΟ্ͷݒ೦ • CίϯύΠϥϔομɺϥΠϒϥϦ͕ࠩ͠ସ͑ΒΕΔͱࠔΔ • CίϯύΠϥϏϧυ࣌ͱશ͘ಉ͡ϑϧύεΛ͏ • /tmp ʹݩ͔ΒϑΝΠϧ͕͋Δ߹͘
• Έ্Ͳ͏͠Α͏ͳ͍ͷׂΓΔ • Ϣʔβʔ͕GCCΛࣗ༝ʹ࣮ߦͰ͖ΔΑ͏ͳϓϩάϥϜͦΕࣗମ͕੬ऑ • GCCϔομɺੜίʔυ͕ࠩ͠ସ͑ΒΕΔύʔϛογϣϯΛऔΒΕͯෛ͚
RubyͷJITख๏Ͱ ࣮ݱՄೳͳ࠷దԽྫ
࠷దԽ 1: ελοΫϙΠϯλɺ ϓϩάϥϜΧϯλͷҠಈݮ • ελοΫϕʔεͷVM͕͏ελοΫϙΠϯλͷૢ࡞͕ෆཁ • ྫ֎ͰେҬग़ͯ͠ελοΫͤͳ͍ͱ͍͚ͳ͍ͷ Ͱɺྫ֎Λcatch͠ͳ͍ϝιουʹݶΔ •
όΠτίʔυͷσΟεύονʹ͏ϓϩάϥϜΧϯλͷ ૢ࡞͕ෆཁ • ߦ൪߸ΛऔΔϝιουݺͼग़͠ͷखલͰͲ͏ʹ͔෮׆ ͤ͞Δඞཁ͕͋Δ
࠷దԽ 2: ΠϯϥΠϯԽΛར༻ͨ͠ ίϯύΠϥʹΑΔ࠷దԽ • ݱࡏओʹόΠτίʔυ໋ྩͷ࣮͕ੜίʔυ͔ΒΠ ϯϥΠϯԽՄೳ • ϦςϥϧͷͱόΠτίʔυ໋ྩͷ࣮ΛΠϯϥΠϯ Խ͢Δ͚ͩͰɺ1
+ 2 ʹର͠ 3 ΛੜͰ͖Δ • CͷίϯύΠϥؔαΠζ͕େ͖͍ͱΠϯϥΠϯԽ͠ ͳ͘ͳΔͷͰɺ໌ࣔతͳΠϯϥΠϯԽࢦఆ͕ඞཁͳ͜ ͱ
࠷దԽ 2: ΠϯϥΠϯԽΛར༻ͨ͠ ίϯύΠϥʹΑΔ࠷దԽ ࣮ݧஈ֊ͷͷ: • RubyͰهड़͞ΕͨϝιουΠϯϥΠϯԽ͕؆୯ͳ͜ͱ Θ͔͍ͬͯΔ • શͯΛΠϯϥΠϯԽ͢ΔͱίϯύΠϧ͕࣌ؒ৳ͼΔͷͰɺ
Ͳ͜·ͰίϯύΠϧ͢Δ͔ݕ౼த • ϧʔϓͷΠϯϥΠϯԽ͕Ͱ͖Δͱޮ͕͘ɺϧʔϓ༻ϝιο υCͰ࣮͞Ε͓ͯΓɺগ࣮͕͠େม
࠷దԽ 3: ࣮ߦ࣌ͷใʹґଘͨ͠ ίʔυੜ • Կ͔ݺͼग़͠ࡁͷϝιουʹରͯ͠JITΛߦͳ͏ͷͰɺ JITίϯύΠϧ࣌ʹ࣮ߦ࣌ͷܕใ͕खʹೖΔ • ྫ: ϝιουݺͼग़͠ͷ໋ྩʹɺϝιουΩϟογϡ
ʹϨγʔόͷΫϥεใ͕͋Δɺ • ͦͷܕʹಛԽͯ͠ແବͳذΛআڈͨ͠ΓɺؒࢀরΛΠ ϯϥΠϯԽ͢ΔͳͲ͢ΔͱߴʹͳΔ
ੑೳධՁ
ϚΠΫϩϕϯνϚʔΫ: 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
ϚΠΫϩϕϯνϚʔΫ: ϝϞϦ 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
ϚΠΫϩϕϯνϚʔΫ: ϝϞϦ 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
ΞϓϦέʔγϣϯੑೳ 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
ΞϓϦέʔγϣϯੑೳ 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
ΞϓϦέʔγϣϯੑೳ 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
ΞϓϦέʔγϣϯੑೳ 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
ੑೳิ 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
ࠓޙͷల WebΞϓϦέʔγϣϯʹ͓͚Δੑೳ্ʹϑΥʔΧε: • ࡉ͔͘ίϯύΠϧ͢ΔͱVMىಈճ͕૿͑ΔͷͰɺେ͖͘ ΠϯϥΠϯԽͯ͠·ͱΊͯίϯύΠϧ • খ͞ͳϝιουcaller͔ΒίϯύΠϧͯ͠ΠϯϥΠϯԽ • ͘Ͱ͖ͳͦ͞͏ͳϝιουίϯύΠϧ͠ͳ͍ɺ࠷దԽ ରͷબઓུΛվળ͢Δ
·ͱΊ • CίϯύΠϥΛͬͨJITख๏ͷτϨʔυΦϑΛઆ໌ͨ͠ • ίϯύΠϥࣗମͷϝϯςφϯε؆୯ʹͳΔ • ͦΕΛࢧ͑Δج൫ͷอक࣮ߦ࣌ͷڍಈෳࡶԽ͢Δ • Ruby 2.6JITʹΑΓCPUܭࢉத৺ͷ༻్Ͱ1.6ഒߴԽͨ͠
• খ͞ͳϝιου͕ͨ͘͞Μ͋Δ߹ͷੑೳվળத