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
Rubyの会社でRustを書くということ
Search
KOBA789
February 10, 2018
Technology
3
3k
Rubyの会社でRustを書くということ
Cookpad TechConf 2018 (
https://techconf.cookpad.com/2018/
) で発表した資料です。
KOBA789
February 10, 2018
Tweet
Share
More Decks by KOBA789
See All by KOBA789
ついに生産が終わったけどZ80でまだまだ遊びたい
koba789
5
3.8k
ISUCONの練習を楽しくたくさんやる方法
koba789
1
260
Z80をRustで動かすまで年が越せない
koba789
0
150
ISUCON練習環境を最も簡単に用意する方法
koba789
2
3k
音をアレする
koba789
0
770
メガネとBluetooth Low Energyでお喋りする
koba789
0
1.5k
Rustでon-diskなB+Treeを作ったときの細かな話
koba789
11
1.7k
音の出るCPUを作っている途中
koba789
5
4.6k
自宅サーバーでbottlerocketを動かしてk8sクラスタを組む
koba789
1
1.1k
Other Decks in Technology
See All in Technology
New Relicを活用したSREの最初のステップ / NRUG OKINAWA VOL.3
isaoshimizu
2
590
Security-JAWS【第35回】勉強会クラウドにおけるマルウェアやコンテンツ改ざんへの対策
4su_para
0
180
Exadata Database Service on Dedicated Infrastructure(ExaDB-D) UI スクリーン・キャプチャ集
oracle4engineer
PRO
2
3.2k
社内で最大の技術的負債のリファクタリングに取り組んだお話し
kidooonn
1
550
【令和最新版】AWS Direct Connectと愉快なGWたちのおさらい
minorun365
PRO
5
750
初心者向けAWS Securityの勉強会mini Security-JAWSを9ヶ月ぐらい実施してきての近況
cmusudakeisuke
0
120
Terraform CI/CD パイプラインにおける AWS CodeCommit の代替手段
hiyanger
1
240
OCI Vault 概要
oracle4engineer
PRO
0
9.7k
SREによる隣接領域への越境とその先の信頼性
shonansurvivors
2
520
SREが投資するAIOps ~ペアーズにおけるLLM for Developerへの取り組み~
takumiogawa
1
190
データプロダクトの定義からはじめる、データコントラクト駆動なデータ基盤
chanyou0311
2
310
【Startup CTO of the Year 2024 / Audience Award】アセンド取締役CTO 丹羽健
niwatakeru
0
990
Featured
See All Featured
Producing Creativity
orderedlist
PRO
341
39k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
Fireside Chat
paigeccino
34
3k
How to Think Like a Performance Engineer
csswizardry
20
1.1k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.4k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
169
50k
Gamification - CAS2011
davidbonilla
80
5k
The Cost Of JavaScript in 2023
addyosmani
45
6.7k
Writing Fast Ruby
sferik
627
61k
Keith and Marios Guide to Fast Websites
keithpitt
409
22k
Navigating Team Friction
lara
183
14k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
Transcript
KOBA789ʢখྛलʣ ΠϯϑϥετϥΫνϟʔ෦ RubyͷձࣾͰ RustΛॻ͘ͱ͍͏͜ͱ Cookpad Inc. Feb 10th, 2018
ࣗݾհ 2 ɾ2017৽ଔ ɾΠϯϑϥετϥΫνϟʔ෦ σʔλج൫άϧʔϓ KOBA789ʢখྛलʣ
RubyͷձࣾͰ RustΛॻ͘ͱ͍͏͜ͱ
4 Ruby
5 Go Java Ruby
6 Go Java Hako (DockerσϓϩΠج൫) Ruby
7 Go Java Hako (DockerσϓϩΠج൫) Ruby
8 Go Java Ruby Rust Hello !
9 Ruby͚ͩͷձࣾ Ͱͳ͍
10 Push௨৴ج൫
ɾΠϕϯτൃੜ͝ͱʹૹ৴ ɾʮͭ͘ΕΆ͕ಧ͍ͨʯ ɾ৴গͳ͍ ɾಛఆͷʹҰ੪৴ ɾٳϢʔβʔͷૌٻ ɾ৴ଟ͍ ɾඦສ௨ن ɾݟੵΓ͕େ ϓογϡ௨ 11
৴ Ұ੪৴
12 App S3 ج൫ SNS 1. ड৴ઃఆΛΫΤϦ 2. ARNΛΫΤϦ 3.
S3ʹॻ͖ࠐΈ 4. S3͔ΒಡΈग़͢ 5. SNSૹ৴ ᶃ ᶄ ᶅ ᶇ ᶆ MySQL
13 ड৴ઃఆΛΫΤϦ ARNΛΫΤϦ SNSૹ৴ S3ʹॻ͖ࠐΉ S3͔ΒಡΈग़͢
14 ड৴ઃఆΛΫΤϦ ARNΛΫΤϦ SNSʹૹ৴
15 ड৴ઃఆΛΫΤϦ ARNΛΫΤϦ SNSʹૹ৴ { چج൫ { App
16 World's Largest Rails Monolith
S3 ج൫ SNS 17 App New! MySQL
S3 ج൫ SNS New! DBڞ༗ 18 ίϐϖϩδοΫ App MySQL
19 ཧͷPush௨৴ج൫
20 ड৴ઃఆΛΫΤϦ ARNΛΫΤϦ SNSʹૹ৴ { } چج൫ ৽ج൫
21 ϢʔβʔIDࢦఆ ARNࢦఆ λʔήοτ
22 ड৴ઃఆͰϑΟϧλ
23 S3 ج൫ SNS MySQL େྔ৴ ৴ HTTP API
24 S3 ج൫ SNS MySQL େྔ৴ ৴ HTTP API ϢʔβʔIDͱϝοηʔδ͑͞
์ΓࠐΊ৴Ͱ͖Δ
ɾΠϕϯτൃੜ͝ͱʹૹ৴ ɾʮͭ͘ΕΆ͕ಧ͍ͨʯ ɾ৴গͳ͍ ɾಛఆͷʹҰ੪৴ ɾٳϢʔβʔͷૌٻ ɾ৴ଟ͍ ɾඦສ௨ن ɾݟੵΓ͕େ ϓογϡ௨ 25
৴ Ұ੪৴
ɾΠϕϯτൃੜ͝ͱʹૹ৴ ɾʮͭ͘ΕΆ͕ಧ͍ͨʯ ɾ৴গͳ͍ ɾಛఆͷʹҰ੪ૹ৴ ɾٳϢʔβʔͷૌٻ ɾ৴ଟ͍ ɾඦສ௨ن ɾݟੵΓ͕େ ϓογϡ௨ 26
৴ ΞϓϦϚʔέςΟϯά
ɾΠϕϯτൃੜ͝ͱʹૹ৴ ɾʮͭ͘ΕΆ͕ಧ͍ͨʯ ɾ৴গͳ͍ ɾಛఆͷʹҰ੪ૹ৴ ɾٳϢʔβʔͷૌٻ ɾ৴ଟ͍ ɾඦສ௨ن ɾݟੵΓ͕େ ϓογϡ௨ 27
৴ ΞϓϦϚʔέςΟϯά ߴͳdry-run ඦສ௨ΛͰ
28 RubyͰ࡞Δͷେม
Ruby or else 29 ɾɾ҆શੑɾฒߦੑʹϑΥʔΧε ɾίϯύΠϥ͕σʔλڝ߹Λݕࠪ ɾτϨΠτʹΑΔδΣωϦΫε Rust
30 Rust is not magic
31 ड৴ઃఆΛΫΤϦ ARNΛΫΤϦ SNSʹૹ৴
32 ड৴ઃఆΛΫΤϦ ARNΛΫΤϦ SNSʹૹ৴ 6ms 6ms }✕ 100ສ = 3࣌ؒ
33 Inspired by facebook/dataloader https://github.com/facebook/dataloader
34 ΫΤϦΛ·ͱΊΔ
35 ... WHERE user_id = 1; ... WHERE user_id =
2; ... WHERE user_id = 3; ... WHERE user_id IN (1, 2, 3);
ΫΤϦΛ·ͱΊΔ 36 WHERE user_id = ? →6ms (only 1 id)
WHERE user_id IN (?, ?, ... ?) →11ms (1000 ids) ϕϯνϚʔΫͷ݁Ռɺઍ͘Β͍͕Α͍ ࠷దͳόοναΠζ
ΫΤϦΛ·ͱΊΔ 37 ɾΩϡʔʹuser_idΛೖΕΔ ɾΩϡʔʹཷ·͍ͬͯΔuser_idΛҰؾʹऔΓग़͢ ɾIN (...) ͷதʹల։ͯ͠DBΫΤϦ ΞϧΰϦζϜ
38 KOBA789/batch-recv-rs https://github.com/KOBA789/batch-recv-rs
KOBA789/batch-recv-rs 39 MFU UY SY DSPTTCFBN@DIBOOFMVOCPVOE MFUDIVOL7FD@SY CBUDI@SFDW
VOXSBQ DPMMFDU ͍ํ
εϨουؒͷϝοηʔδύογϯάʹ͑Δ Multi-producer multi-consumerͳFIFOΩϡʔ SenderͱReceiver͔ΒͳΔ MFU UY SY DSPTTCFBN@DIBOOFMVOCPVOE KOBA789/batch-recv-rs
40 crossbeam_channel
KOBA789/batch-recv-rs 41 fn recv(&self) -> Result<T, RecvError> Ωϡʔͷத͔Β࠷ॳͷཁૉΛऔΓग़͢ϝιου ཁૉ͕ͳ͚ΕεϨουΛϒϩοΫ͢Δʢॏཁʣ crossbeam_channel::Receiver
KOBA789/batch-recv-rs 42 fn try_iter(&self) -> TryIter<T> ΩϡʔͷதΛΠςϨʔλͱͯ͠ಘΔϝιου impl<'a, T> Iterator
for TryIter<'a, T> ͪΖΜIteratorτϨΠτΛ࣮͍ͯ͠Δ crossbeam_channel::Receiver
KOBA789/batch-recv-rs 43 fn take(self, n: usize) -> Take<Self> ΠςϨʔλ͔Βઌ಄nݸͷཁૉ͚ͩͷΠςϨʔλΛ࡞Δϝιου std::iter::Iterator
KOBA789/batch-recv-rs 44 MFUGJSTUTFMGSFDW MFUSFTUTFMGUSZ@JUFS UBLF O MFUJUFSWFD<GJSTU> JOUP@JUFS
DIBJO SFTU 0L JUFS Έ߹ΘͤΔ
45 Ωϡʔ ϝΠϯ εϨου ΫΤϦ εϨου
batch_recv(3) 46 1 Ωϡʔ ϝΠϯ εϨου ΫΤϦ εϨου
47 1 working... Ωϡʔ ϝΠϯ εϨου ΫΤϦ εϨου
48 1 working... 2 Ωϡʔ ϝΠϯ εϨου ΫΤϦ εϨου
49 1 working... 2 Ωϡʔ ϝΠϯ εϨου ΫΤϦ εϨου
50 1 working... 2 5 4 3 Ωϡʔ ϝΠϯ εϨου
ΫΤϦ εϨου
51 1 working... 2 4 3 5 Ωϡʔ ϝΠϯ εϨου
ΫΤϦ εϨου
52 2 4 3 5 Ωϡʔ ϝΠϯ εϨου ΫΤϦ εϨου
batch_recv(3) 53 2 4 3 5 Ωϡʔ ϝΠϯ εϨου ΫΤϦ
εϨου
54 2 4 3 5 Ωϡʔ ϝΠϯ εϨου ΫΤϦ εϨου
55 2 4 3 5 working... } batched Ωϡʔ ϝΠϯ
εϨου
56 Q. ΫΤϦ݁Ռ Ͳ͏ͬͯฦ͢?
ΫΤϦ݁ՌͲ͏ͬͯฦ͢? 57 futures::sync::oneshot Ұ͚͔ͩ͠ૹΕͳ͍ΩϡʔͷΑ͏ͳͷ SenderɹɹͱReceiverɹɹ͔ΒͳΔ ೖ ग़ ʹΛೖΕΔͱ ͔Βग़ͯ͘Δ ೖ
ग़
58 ೖ 1 ग़ Ωϡʔ ϝΠϯ εϨου ΫΤϦ εϨου
59 ೖ 1 ग़ Ωϡʔ ϝΠϯ εϨου ΫΤϦ εϨου
60 ೖ 1 ग़ A Ωϡʔ ϝΠϯ εϨου ΫΤϦ εϨου
61 ೖ 1 ग़ A A Ωϡʔ ϝΠϯ εϨου ΫΤϦ
εϨου
62 Q.ΫΤϦ͕ ͨ͘͞Μ͋ͬͨΒ?
63 ೖ 2 ग़ ೖ 1 ೖ 3 ग़ ग़
64 ೖ 2 ग़ ೖ 1 ೖ 3 ग़ ग़
όοναΠζͷ εϨου͕ඞཁ ͜ΕͰࠔΔ
65 Tokio + futures-rs https://tokio.rs/
Tokio + futures-rs 66 ɾΠϕϯτϧʔϓͷ࣮ ɾ1ݸҎ্ͷFutureΛ࣮ߦ ɾϊϯϒϩοΩϯά I/O API ɾFutureτϨΠτ
ɾJSͷPromiseͱ ಉ༷ͷ֓೦ ɾFutureͷίϯϏωʔλ ɾɹɹɹɹ͜Εͷػೳ Tokio futures-rs ೖ ग़
Tokio + futures-rs 67 futures::sync::oneshot Ұ͚͔ͩ͠ૹΕͳ͍ΩϡʔͷΑ͏ͳͷ ɹɹΠϕϯτϧʔϓͰࢹͰ͖Δ ग़
68 ΠϕϯτϧʔϓΛ͑ 1εϨουͰͨ͘͞Μͷ "ग़"Λͪ߹ΘͤͰ͖Δ
69 ೖ 2 ग़ ೖ 1 ೖ 3 ग़ ग़
Tokio + futures-rs 70 ɾΩϡʔͷ͜͏ͷεϨου͔ΒΛฦͯ͠Β͏͜ͱ͕Ͱ͖Δ ɾϒϩοΩϯάͳॲཧΛFutureʹม͍ͯ͠ΔͱΈͳͤΔ oneshot::Senderɹ ΛΩϡʔೖΕΔύλʔϯ ೖ
71 KOBA789/batch-loader-rs https://github.com/KOBA789/batch-loader-rs
KOBA789/batch-loader-rs 72 ɾKeyͷΠςϨʔλ͔ΒValueͷྻΛฦ͢ϝιουΛ ࣮Ͱ͖ΕSQLҎ֎ʹద༻Մೳ ɾࠓճͷྫͰKey=ϢʔβʔID, Value=ςʔϒϧͷߦ Key-ValueతͳҙͷΫΤϦΛόονԽ
࠷ޙʹ 73 ɾͱʹ͔͘ϚϧνεϨου͕҆શ ɾϚϧνεϨουͱΠϕϯτۦಈͷ͍͍ͱ͜औΓ͕Ͱ͖Δ ɾ҆৺ͯ͠ߴԽʹऔΓΊΔ ɾ൚༻ϥΠϒϥϦΛ࡞Γ͍͢ܕγεςϜ Rustͷ͍͍ͱ͜Ζ
74 ͜͏ͯ͠ ϓογϡ௨৴ج൫ RustͰॻ͖͞Ε·ͨ͠
75 Ruby͚ͩ͡Όͳ͍ దͳݴޠͰ Ϋʔϧʹ՝ղܾ
76 RustΛॻ͘Πϯλʔϯγοϓ https://internship.cookpad.com/2018/spring/