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
CSC509 Lecture 12
javiergs
PRO
0
160
Click-free releases & the making of a CLI app
oheyadam
2
120
Why Jakarta EE Matters to Spring - and Vice Versa
ivargrimstad
0
1.1k
どうして僕の作ったクラスが手続き型と言われなきゃいけないんですか
akikogoto
1
120
AWS IaCの注目アップデート 2024年10月版
konokenj
3
3.3k
エンジニアとして関わる要件と仕様(公開用)
murabayashi
0
290
watsonx.ai Dojo #4 生成AIを使ったアプリ開発、応用編
oniak3ibm
PRO
1
140
CSC509 Lecture 13
javiergs
PRO
0
110
Kaigi on Rails 2024 〜運営の裏側〜
krpk1900
1
230
見せてあげますよ、「本物のLaravel批判」ってやつを。
77web
7
7.8k
ActiveSupport::Notifications supporting instrumentation of Rails apps with OpenTelemetry
ymtdzzz
1
230
Duckdb-Wasmでローカルダッシュボードを作ってみた
nkforwork
0
130
Featured
See All Featured
Building Your Own Lightsaber
phodgson
103
6.1k
[RailsConf 2023] Rails as a piece of cake
palkan
52
4.9k
Code Reviewing Like a Champion
maltzj
520
39k
Become a Pro
speakerdeck
PRO
25
5k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
44
2.2k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
4 Signs Your Business is Dying
shpigford
180
21k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
93
16k
A Philosophy of Restraint
colly
203
16k
Imperfection Machines: The Place of Print at Facebook
scottboms
265
13k
KATA
mclloyd
29
14k
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ഒߴԽͨ͠
• খ͞ͳϝιου͕ͨ͘͞Μ͋Δ߹ͷੑೳվળத