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
780
Ruby 3.0 JIT on Rails
k0kubun
9
9.2k
Other Decks in Programming
See All in Programming
AppRouterを用いた大規模サービス開発におけるディレクトリ構成の変遷と問題点
eiganken
1
410
PHPとAPI Platformで作る本格的なWeb APIアプリケーション(入門編) / phpcon 2024 Intro to API Platform
ttskch
0
360
短期間での新規プロダクト開発における「コスパの良い」Goのテスト戦略」 / kamakura.go
n3xem
2
210
Androidアプリのモジュール分割における:x:commonを考える
okuzawats
1
260
今年一番支援させていただいたのは認証系サービスでした
satoshi256kbyte
1
280
create_tableをしただけなのに〜囚われのuuid編〜
daisukeshinoku
0
330
fs2-io を試してたらバグを見つけて直した話
chencmd
0
280
見えないメモリを観測する: PHP 8.4 `pg_result_memory_size()` とSQL結果のメモリ管理
kentaroutakeda
0
890
return文におけるstd::moveについて
onihusube
1
1.4k
Compose UIテストを使った統合テスト
hiroaki404
0
120
週次リリースを実現するための グローバルアプリ開発
tera_ny
1
730
テストケースの名前はどうつけるべきか?
orgachem
PRO
1
180
Featured
See All Featured
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
3
230
Being A Developer After 40
akosma
89
590k
Designing for humans not robots
tammielis
250
25k
Embracing the Ebb and Flow
colly
84
4.5k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
2
160
Intergalactic Javascript Robots from Outer Space
tanoku
270
27k
We Have a Design System, Now What?
morganepeng
51
7.3k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.5k
How GitHub (no longer) Works
holman
312
140k
Faster Mobile Websites
deanohume
305
30k
Fashionably flexible responsive web design (full day workshop)
malarkey
406
66k
It's Worth the Effort
3n
183
28k
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ഒߴԽͨ͠
• খ͞ͳϝιου͕ͨ͘͞Μ͋Δ߹ͷੑೳվળத