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.2k
Rustのパフォーマンスに関するTips
Osuke
November 24, 2020
Tweet
Share
More Decks by Osuke
See All by Osuke
特許データを使ったマルチモーダルAIの検証事例@LLMProd#4
osuke
0
210
dbtを中心に据えた データ分析とプロダクト開発
osuke
1
1k
LayerX Privacy Tech事業部紹介 Tech編
osuke
0
180
(SCIS2021) Anonify: プライバシーを保護した 検証可能な状態遷移モジュール
osuke
1
360
Rustで実装された AWS Nitro Enclaves CLIを読む
osuke
0
350
ARM TrustZone入門 / ARM TrustZone intro
osuke
3
8.4k
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
AI Agents with JavaScript
slobodan
0
210
小田原でみんなで一句詠みたいな #phpcon_odawara
stefafafan
0
320
Code smarter, not harder - How AI Coding Tools Boost Your Productivity | Webinar 2025
danielsogl
0
120
ミリしらMCP勉強会
watany
4
740
PHPバージョンアップから始めるOSSコントリビュート / how2oss-contribute
dmnlk
1
970
新卒から4年間、20年もののWebサービスと 向き合って学んだソフトウェア考古学
oguri
8
7.2k
Agentic Applications with Symfony
el_stoffel
2
270
いまさら聞けない生成AI入門: 「生成AIを高速キャッチアップ」
soh9834
15
4.5k
Going Structural with Named Tuples
bishabosha
0
200
フロントエンドテストの育て方
quramy
11
2.9k
Empowering Developers with HTML-Aware ERB Tooling @ RubyKaigi 2025, Matsuyama, Ehime
marcoroth
1
220
マルチアカウント環境での、そこまでがんばらない RI/SP 運用設計
wa6sn
0
710
Featured
See All Featured
The Cult of Friendly URLs
andyhume
78
6.3k
Git: the NoSQL Database
bkeepers
PRO
430
65k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
34
2.2k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
29
1.6k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
9
740
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
The Power of CSS Pseudo Elements
geoffreycrofte
75
5.7k
Reflections from 52 weeks, 52 projects
jeffersonlam
349
20k
GitHub's CSS Performance
jonrohan
1030
460k
Into the Great Unknown - MozCon
thekraken
37
1.7k
Why You Should Never Use an ORM
jnunemaker
PRO
55
9.3k
The Cost Of JavaScript in 2023
addyosmani
49
7.7k
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