rust-optimise について
Rust 開発者向けのパフォーマンスガイドです。8 分野のルールを影響度順に整理し、メモリ、借用、非同期処理、アルゴリズムの最適化を支援します。
主な機能
- 018 分野の最適化項目
- 02影響度順のルール整理
- 03割り当てとクローンの削減
- 04非同期・並行処理の指針
- 05アルゴリズムとイテレーターの指針
活用シーン
- 01Rust の性能ボトルネック調査
- 02不要なメモリ割り当ての削減
- 03適切なデータ構造の選択
- 04非同期コードの性能レビュー
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ pproenca/ dot-skills --skill rust-optimiseインストール後、Claude Code で/rust-optimiseを入力して読み込まれたか確認します。
同梱ファイル
SKILL.md と一緒にインストールされる参考資料やスクリプト。
AGENTS.mdREADME.mdmetadata.jsonassets/templates/_template.mdreferences/_sections.mdalgo-avoid-nested-loops-for-lookup.mdalgo-use-binary-search-for-sorted-data.mdalgo-use-chunks-for-batch-processing.mdalgo-use-select-nth-unstable-for-partial-sort.mdalgo-use-sort-unstable-when-order-irrelevant.mdasync-avoid-blocking-in-async-context.mdasync-avoid-holding-lock-across-await.mdasync-minimize-lock-scope.mdasync-use-buffered-for-bounded-concurrency.mdasync-use-join-for-concurrent-futures.mdasync-use-rwlock-over-mutex-for-read-heavy.mdcomp-avoid-repeated-parsing-of-static-data.mdcomp-prefer-static-dispatch.mdcomp-reduce-monomorphization-bloat.mdcomp-use-const-for-compile-time-computation.mdcomp-use-const-generics-for-array-sizes.mdds-use-btreemap-for-sorted-iteration.mdds-use-entry-api-for-conditional-insert.mdds-use-hashmap-for-key-lookup.mdds-use-hashset-for-membership.mdds-use-vecdeque-for-queue-operations.mditer-chain-instead-of-intermediate-collect.mditer-use-extend-for-bulk-append.mditer-use-filter-map-for-combined-operations.mditer-use-flat-map-for-nested-iteration.md詳細
Rust の最適化に関するベストプラクティス
Rust アプリケーションのパフォーマンス最適化ガイドです。影響度の高いもの(メモリ割り当て、所有権)から段階的なもの(マイクロ最適化)まで、優先度順に並べた 8 カテゴリー、42 個のルールを掲載しています。
適用する場面
- Rust コードのパフォーマンスを最適化する場合、またはメモリ割り当てを削減する場合
- 最適なアルゴリズム計算量を実現するデータ構造を選ぶ場合
- 不要な中間メモリ割り当てを避けるためにイテレーターを使用する場合
- Tokio やその他のランタイムで非同期コードを書く場合
- ホットパスをプロファイリングし、パフォーマンスのボトルネックを解消する場合
- パフォーマンスに関するアンチパターンがないかコードをレビューする場合
優先度別のルールカテゴリー
| 優先度 | カテゴリー | 影響度 | プレフィックス |
|---|---|---|---|
| 1 | メモリ割り当て | 重大 | mem- |
| 2 | 所有権と借用 | 重大 | own- |
| 3 | データ構造の選択 | 高 | ds- |
| 4 | イテレーターとコレクションのパターン | 高 | iter- |
| 5 | 非同期処理と並行処理 | 中〜高 | async- |
| 6 | アルゴリズム計算量 | 中 | algo- |
| 7 | コンパイル時の最適化 | 中 | comp- |
| 8 | マイクロ最適化 | 低 | micro- |
クイックリファレンス
1. メモリ割り当て(重大)
mem-avoid-unnecessary-clone- 不要な clone 呼び出しを避けるmem-preallocate-vec-capacity- Vec の容量を事前に確保するmem-use-cow-for-conditional-ownership- 条件付きの所有権には Cow を使用するmem-use-arc-for-shared-immutable-data- 共有する不変データには Arc を使用するmem-avoid-format-for-simple-concatenation- 単純な連結には format! を使用しないmem-use-smallvec-for-small-collections- 小さなコレクションには SmallVec を使用する
2. 所有権と借用(重大)
own-accept-str-slice-not-string- &String ではなく &str を受け取るown-accept-slice-not-vec- &Vec ではなく &[T] を受け取るown-use-into-for-flexible-ownership- 柔軟な所有権のために Into を使用するown-return-borrowed-when-possible- 可能な場合は借用データを返すown-use-asref-for-generic-borrows- 汎用的な借用には AsRef を使用する
3. データ構造の選択(高)
ds-use-hashset-for-membership- 要素の所属判定には HashSet を使用するds-use-hashmap-for-key-lookup- キーと値の検索には HashMap を使用するds-use-btreemap-for-sorted-iteration- ソート順の反復処理には BTreeMap を使用するds-use-vecdeque-for-queue-operations- キュー操作には VecDeque を使用するds-use-entry-api-for-conditional-insert- 条件付きの挿入には Entry API を使用する
4. イテレーターとコレクションのパターン(高)
iter-chain-instead-of-intermediate-collect- 中間の collect を使わず、イテレーターを連結するiter-use-iter-over-into-iter-when-borrowing- 借用する場合は into_iter() ではなく iter() を使うiter-use-filter-map-for-combined-operations- 複合操作には filter_map を使うiter-use-flat-map-for-nested-iteration- ネストした反復処理には flat_map を使うiter-use-extend-for-bulk-append- 一括追加には extend() を使うiter-use-fold-for-accumulation- 複雑な集約には fold() を使う
5. 非同期処理と並行性(中〜高)
async-avoid-blocking-in-async-context- 非同期コンテキストでのブロッキングを避けるasync-use-join-for-concurrent-futures- 並行する future には join! を使うasync-use-rwlock-over-mutex-for-read-heavy- 読み取りが多い場合は Mutex より RwLock を使うasync-minimize-lock-scope- ロックの範囲を最小限にするasync-use-buffered-for-bounded-concurrency- 並行性を制限するには buffered() を使うasync-avoid-holding-lock-across-await- await をまたいでロックを保持しない
6. アルゴリズムの計算量(中)
algo-avoid-nested-loops-for-lookup- 検索にネストしたループを使わないalgo-use-binary-search-for-sorted-data- ソート済みデータには二分探索を使うalgo-use-sort-unstable-when-order-irrelevant- 順序が重要でない場合は sort_unstable を使うalgo-use-select-nth-unstable-for-partial-sort- 部分ソートには select_nth_unstable を使うalgo-use-chunks-for-batch-processing- バッチ処理には chunks() を使う
7. コンパイル時の最適化(中)
comp-use-const-for-compile-time-computation- コンパイル時の計算には const を使うcomp-prefer-static-dispatch- 動的ディスパッチより静的ディスパッチを優先するcomp-reduce-monomorphization-bloat- モノモーフィゼーションによる肥大化を抑えるcomp-use-const-generics-for-array-sizes- 配列サイズには const ジェネリクスを使うcomp-avoid-repeated-parsing-of-static-data- 静的データの繰り返しパースを避ける
8. マイクロ最適化(低)
micro-use-inline-for-small-functions- 小さく、頻繁に実行される関数には inline 属性を適用するmicro-avoid-bounds-checks-in-hot-loops- ホットループでの境界チェックを避けるmicro-use-wrapping-arithmetic-when-safe- 安全な場合はラッピング演算を使うmicro-use-byte-literals-for-ascii- ASCII にはバイトリテラルを使う
参考資料
- https://nnethercote.github.io/perf-book/
- https://rust-lang.github.io/api-guidelines/
- https://doc.rust-lang.org/nomicon/
- https://tokio.rs/tokio/tutorial
関連スキル
- 慣用的なパターン、アーキテクチャ、コードの整理については、
rust-refactorスキルを参照してください
コメント
Markdown 対応