SuperAPI AI Directory

performance analysis and optimization

Coding & development

View repository

Updated 10-16 08:08

About this skill

⚡ Performance Analysis and Optimization

Version: 2.0.0 Last Updated: 2025-11-22 Priority: ⭐⭐⭐ (P2 Level) Purpose: Performance Analysis and Optimization for Rust Applications


📋 Overview

We provide identification and optimization of performance issues through CPU profiling, benchmarking, and memory analysis.


🎯 P0: Invocation Triggers

Trigger Example
Slow "this is slow"
Memory usage "why is memory usage so high?"
Optimization "optimize this function"
Profiling "profile this code"
Benchmark "benchmark performance"

🔧 P1: Analysis Tools List

Tool Priority

Tool Purpose Target Command
criterion Benchmark Function cargo bench
flamegraph CPU profile Process cargo flamegraph
perf Detailed profile Linux perf record
valgrind Memory Heap valgrind --tool=massif
heaptrack Heap tracking Allocation heaptrack ./binary
cargo-bloat Binary size Size cargo bloat
tokio-console Async Task tokio-console

🚀 P2: Analysis Patterns

Pattern 1: Benchmarking (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: Memory Profiling

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

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

Pattern 4: Binary Size Analysis

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

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

⚡ P3: Optimization Strategies

Optimization Priority

Priority Strategy Effect Difficulty
1 Algorithm improvement High Medium
2 Data structure change High Medium
3 Memory allocation reduction Medium Low
4 Parallelization Medium High
5 Cache utilization Medium Medium
6 SIMD/Low-level Low High

Common Optimizations

// ❌ 毎回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> {
    // ...
}

📊 Performance Goals

Metric Goal Measurement Method
Build time <5 minutes CI measurement
Test time <2 minutes cargo test
Binary size <50MB cargo bloat
Memory usage <500MB runtime measurement

🛡️ Precautions

Measure with release builds

# ❌ デバッグビルド(遅い)
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

✅ Success Criteria

Check Item Criteria
Bottleneck identification Top 3 locations
Benchmark Before/after comparison
Memory leak None
Regression test Performance maintained

🔗 Related Skills

  • Rust Development: Build optimization
  • Debugging: Problem location identification
  • Security Audit: Trade-off with security