Marketplace

Performance Analysis and Optimization

CPU profiling, benchmarking, and memory analysis for Rust applications. Use when code is slow, memory usage is high, or optimization is needed.

allowed_tools: Bash, Read, Grep, Glob

$ インストール

git clone https://github.com/ShunsukeHayashi/Miyabi /tmp/Miyabi && cp -r /tmp/Miyabi/packages/mcp-bundle/claude-plugins/miyabi-full/skills/performance-analysis ~/.claude/skills/Miyabi

// tip: Run this command in your terminal to install the skill


name: Performance Analysis and Optimization description: CPU profiling, benchmarking, and memory analysis for Rust applications. Use when code is slow, memory usage is high, or optimization is needed. allowed-tools: Bash, Read, Grep, Glob

⚡ Performance Analysis and Optimization

Version: 2.0.0 Last Updated: 2025-11-22 Priority: ⭐⭐⭐ (P2 Level) Purpose: Rustアプリケーションのパフォーマンス分析と最適化


📋 概要

CPUプロファイリング、ベンチマーク、メモリ分析を通じた パフォーマンス問題の特定と最適化を提供します。


🎯 P0: 呼び出しトリガー

トリガー
遅い"this is slow"
メモリ使用"why is memory usage so high?"
最適化"optimize this function"
プロファイリング"profile this code"
ベンチマーク"benchmark performance"

🔧 P1: 分析ツール一覧

ツール優先順位

ツール用途対象コマンド
criterionベンチマーク関数cargo bench
flamegraphCPUプロファイルプロセスcargo flamegraph
perf詳細プロファイルLinuxperf record
valgrindメモリヒープvalgrind --tool=massif
heaptrackヒープ追跡割り当てheaptrack ./binary
cargo-bloatバイナリサイズサイズcargo bloat
tokio-console非同期タスクtokio-console

🚀 P2: 分析パターン

Pattern 1: ベンチマーク(criterion)

// benches/my_benchmark.rs
use criterion::{black_box, criterion_group, criterion_main, Criterion};

fn bench_function(c: &mut Criterion) {
    c.bench_function("my_function", |b| {
        b.iter(|| my_function(black_box(input)))
    });
}

criterion_group!(benches, bench_function);
criterion_main!(benches);
cargo bench

Pattern 2: Flamegraph

# フレームグラフ生成
cargo flamegraph --bin miyabi -- --issue 270

# 出力: flamegraph.svg

Pattern 3: メモリプロファイル

# valgrind massif
valgrind --tool=massif ./target/release/miyabi
ms_print massif.out.*

# heaptrack(推奨)
heaptrack ./target/release/miyabi
heaptrack_gui heaptrack.miyabi.*

Pattern 4: バイナリサイズ分析

# サイズ分析
cargo bloat --release --crates

# シンボル別
cargo bloat --release -n 20

⚡ P3: 最適化戦略

最適化優先順位

優先度戦略効果難易度
1アルゴリズム改善
2データ構造変更
3メモリ割り当て削減
4並列化
5キャッシュ活用
6SIMD/低レベル

よくある最適化

// ❌ 毎回allocate
for item in items {
    let s = item.to_string();
    // ...
}

// ✅ 事前allocate
let mut buf = String::with_capacity(1024);
for item in items {
    buf.clear();
    write!(&mut buf, "{}", item).unwrap();
    // ...
}
// ❌ Clone多用
fn process(data: Vec<T>) -> Vec<T> {
    data.clone()
}

// ✅ 参照で渡す
fn process(data: &[T]) -> Vec<T> {
    // ...
}

📊 パフォーマンス目標

メトリクス目標測定方法
ビルド時間<5分CI計測
テスト時間<2分cargo test
バイナリサイズ<50MBcargo bloat
メモリ使用量<500MBruntime計測

🛡️ 注意事項

リリースビルドで測定

# ❌ デバッグビルド(遅い)
cargo run

# ✅ リリースビルド
cargo run --release

PGO(Profile-Guided Optimization)

# Step 1: インストルメント
RUSTFLAGS="-Cprofile-generate=/tmp/pgo" cargo build --release

# Step 2: プロファイル収集
./target/release/miyabi [typical workload]

# Step 3: 最適化ビルド
llvm-profdata merge -o /tmp/pgo/merged.profdata /tmp/pgo
RUSTFLAGS="-Cprofile-use=/tmp/pgo/merged.profdata" cargo build --release

✅ 成功基準

チェック項目基準
ボトルネック特定上位3箇所
ベンチマーク改善前後比較
メモリリークなし
回帰テストパフォーマンス維持

🔗 関連Skills

  • Rust Development: ビルド最適化
  • Debugging: 問題箇所特定
  • Security Audit: セキュリティとのトレードオフ

Repository

ShunsukeHayashi
ShunsukeHayashi
Author
ShunsukeHayashi/Miyabi/packages/mcp-bundle/claude-plugins/miyabi-full/skills/performance-analysis
11
Stars
6
Forks
Updated6d ago
Added1w ago