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
13k
Ruby JIT Hacking Guide / RubyKaigi 2023
k0kubun
2
9.5k
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
420
Why Ruby's JIT was slow / RubyKaigi Takeout 2021
k0kubun
3
1.9k
数時間かかる週一リリースを毎日何度も爆速でできるようにするまで / CI/CD Conference 2021
k0kubun
21
14k
Ruby 3 JIT's roadmap / RubyConf China 2020
k0kubun
0
770
Ruby 3.0 JIT on Rails
k0kubun
9
9.1k
Other Decks in Programming
See All in Programming
Effective Signals in Angular 19+: Rules and Helpers
manfredsteyer
PRO
0
180
為你自己學 Python
eddie
0
370
各クラウドサービスにおける.NETの対応と見解
ymd65536
0
210
Monixと常駐プログラムの勘どころ / Scalaわいわい勉強会 #4
stoneream
0
300
range over funcの使い道と非同期N+1リゾルバーの夢 / about a range over func
mackee
0
160
開発者とQAの越境で自動テストが増える開発プロセスを実現する
92thunder
1
210
선언형 UI에서의 상태관리
l2hyunwoo
0
230
React 19でお手軽にCSS-in-JSを自作する
yukukotani
5
450
今年のアップデートで振り返るCDKセキュリティのシフトレフト/2024-cdk-security-shift-left
tomoki10
0
280
Оптимизируем производительность блока Казначейство
lamodatech
0
770
PHPカンファレンス 2024|共創を加速するための若手の技術挑戦
weddingpark
0
100
ゆるやかにgolangci-lintのルールを強くする / Kyoto.go #56
utgwkk
2
510
Featured
See All Featured
Making Projects Easy
brettharned
116
6k
Testing 201, or: Great Expectations
jmmastey
41
7.2k
Producing Creativity
orderedlist
PRO
342
39k
YesSQL, Process and Tooling at Scale
rocio
170
14k
How GitHub (no longer) Works
holman
311
140k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
18
2.3k
4 Signs Your Business is Dying
shpigford
182
21k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.6k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
29
2k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
7k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.2k
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ഒߴԽͨ͠
• খ͞ͳϝιου͕ͨ͘͞Μ͋Δ߹ͷੑೳվળத