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
Rustのパフォーマンスに関するTips
Search
Osuke
November 24, 2020
Programming
3
3.3k
Rustのパフォーマンスに関するTips
Osuke
November 24, 2020
Tweet
Share
More Decks by Osuke
See All by Osuke
特許データを使ったマルチモーダルAIの検証事例@LLMProd#4
osuke
0
220
dbtを中心に据えた データ分析とプロダクト開発
osuke
1
1k
LayerX Privacy Tech事業部紹介 Tech編
osuke
0
190
(SCIS2021) Anonify: プライバシーを保護した 検証可能な状態遷移モジュール
osuke
1
370
Rustで実装された AWS Nitro Enclaves CLIを読む
osuke
0
350
ARM TrustZone入門 / ARM TrustZone intro
osuke
3
8.5k
Anonify
osuke
3
1k
Rustのasync/awaitとスケジューラの話 / rust-async-await
osuke
9
3.9k
Privacy on Blockchain
osuke
1
1.2k
Other Decks in Programming
See All in Programming
七輪ライブラリー: Claude AI で作る Next.js アプリ
suneo3476
1
190
flutter_kaigi_mini_4.pdf
nobu74658
0
150
大LLM時代にこの先生きのこるには-ITエンジニア編
fumiyakume
8
3.3k
On-the-fly Suggestions of Rewriting Method Deprecations
ohbarye
3
5k
note の Elasticsearch 更新系を支える技術
tchov
9
3.5k
Rubyの!メソッドをちゃんと理解する
alstrocrack
1
130
Enterprise Web App. Development (1): Build Tool Training Ver. 5
knakagawa
1
120
eBPF超入門「o11yに使える」とは (20250424_eBPF_o11y)
thousanda
1
110
開発者フレンドリーで顧客も満足?Platformの秘密
algoartis
0
190
Ruby で作る RISC-V CPU エミュレーター / RISC-V CPU emulator made with Ruby
hayaokimura
5
790
カウシェで Four Keys の改善を試みた理由
ike002jp
1
130
generative-ai-use-cases(GenU)の推しポイント ~2025年4月版~
hideg
1
380
Featured
See All Featured
Product Roadmaps are Hard
iamctodd
PRO
53
11k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
47
2.7k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
160
15k
Designing Experiences People Love
moore
142
24k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
3.8k
Navigating Team Friction
lara
185
15k
Intergalactic Javascript Robots from Outer Space
tanoku
271
27k
Statistics for Hackers
jakevdp
798
220k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
29
1.7k
How GitHub (no longer) Works
holman
314
140k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
137
33k
The Power of CSS Pseudo Elements
geoffreycrofte
75
5.8k
Transcript
1 Rustのパフォーマンスに関するTips Osuke Sudo 2020/11/24 @下町.rs#4
2 @zoom_zoomzo Osuke ソフトウェアエンジニア@LayerX • 暗号技術・TEEを用いたデータのプライバシー保護 ・改ざん耐性手法の研究開発 • Anonifyの開発
3 アジェンダ • Inlining • Smaller enum size • SmallVec
/ ArrayVec • Cow
4 Inlining
5 • インライン展開: ◦ 呼び出される関数のコードをインラインで展開することで関数呼び出しのオーバーヘッドを 削減 ◦ バイナリコードサイズは増加 • 頻繁に呼び出す小さい関数はインライン展開するとパフォーマンスが改善することもある
• 最適化レベルや関数のサイズなどに基づきコンパイラがインライン展開を最適化 Inlining https://github.com/rust-lang/rust/pull/50564/commits/77c40f8c6f8cc472f6438f7724d60bf3b7718a0c
6 • アノテーションをつけることで特定の関数をマニュアルでインライン展開 • #[inline] ◦ crate境界内で、その関数を(できるだけ)インライン展開 • #[inline(always)] ◦
crate境界内で、ほとんどの場合でその関数をインライン展開 • #[inline(never)] ◦ その関数を(できるだけ)インライン展開しない Inlining
7 Inlining 同じ関数でも頻繁に呼ばれる箇所はインライン展開、逆にあまり呼ばれない箇所はインライン展開しない https://github.com/rust-lang/rust/pull/64420/commits/a2261ad66400c3145f96ebff0d9b75e910fa89dd
8 Smaller enums
9 • Enumの要素のうち、他の要素と比べて大きなサイズの型をBox化 ◦ A::Zにヒープ割り当てが必要になる代わりにAのサイズが削減 ▪ assert_eq!(std::mem::size_of::<A>(), 108); ▪ assert_eq!(std::mem::size_of::<B>(),
16); ◦ 特にA::Zの使用が比較的少ない場合、不要なアロケーションを避けることができ るのパフォーマンス向上しやすい Smaller Enums
10 Smaller Enums • x86-64上でSubtypeだけのサイズが大きく120bytes → Box化して32bytes。 https://github.com/rust-lang/rust/pull/64394/commits/7f0637da5144c7435e88ea3805021882f077d50c
11 SmallVec
12 SmallVec: https://github.com/servo/rust-smallvec • Vec<T>の代わりにSmallVec<[T; N]>を使うと、N個の要素はスタックに保持され、 N+1個以降の要素はヒープに保持される • 比較的少数の要素を持つVecがたくさんあるようなケースでアロケーションコストを削 減することが可能
• 一方、SmallVecはアクセス時に特定の要素がアロケーションされているか、していな いかチェックする必要があるので、通常の操作はVecよりわずかにコスト増 • 要素数(N)が多かったり、型(T)のサイズが大きかったりするとコピーコストが増 えることがあるので要ベンチマーク
13 SmallVec 滅多に要素数が3以上にならないので、SmallVec<[_; 4]>に変更することでベンチマーク4%改善 https://github.com/rust-lang/rust/pull/55383/commits/526dc1421b48e3ee8357d58d997e7a0f4bb26915
14 ArrayVec: https://github.com/bluss/arrayvec たくさんの小さいベクタとその最大長が分かるケースでは、アロケーションへのフォールバックがない 分、SmallVecよりArrayVecの方が良い https://github.com/rust-lang/rust/pull/74310/commits/c492ca40a288d8a85353ba112c4d38fe87ef453e
15 Cow
16 Cow • 多くの場合read-onlyだが、たまに mutable、毎回Cloneするのは不要なコ スト • 参照型の状態で所有型やmutationが必 要になったときに、はじめてcloneする ◦
Clone-on-write
17 Cow • 参照型と所有型のEnumであるCowを使うと不要なアロケーションを削減できることが ある • ライフタイムなどコードの複雑性が上がってしまうこともあるので注意
18 Cow • &strを引数にとって、関数内でStringに変換するのは毎回アロケーションが発生 • 一方、実際に引数に渡すのは&’staticかStringがほとんどのケース https://github.com/rust-lang/rust/pull/56336/commits/787959c20d062d396b97a5566e0a766d963af022
19 • Nicholas Nethercote ◦ The Rust Performance Book ▪
https://nnethercote.github.io/perf-book/ ◦ Blog posts ▪ https://blog.mozilla.org/nnethercote/ • Rust Performance Pitfalls ◦ https://llogiq.github.io/2017/06/01/perf-pitfalls.html References