
performance-analysis-and-optimization
by ShunsukeHayashi
ð€ First open-source, economically-governed, beginner-friendly autonomous development framework built on Issue-Driven Development | è¶ åå¿è ã§ã䜿ããèªåŸåéçºãã¬ãŒã ã¯ãŒã¯
SKILL.md
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 |
flamegraph | CPUãããã¡ã€ã« | ããã»ã¹ | cargo flamegraph |
perf | 詳现ãããã¡ã€ã« | Linux | perf 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 | ãã£ãã·ã¥æŽ»çš | äž | äž |
| 6 | SIMD/äœã¬ãã« | äœ | é« |
ããããæé©å
// â æ¯å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 |
| ãã€ããªãµã€ãº | <50MB | cargo bloat |
| ã¡ã¢ãªäœ¿çšé | <500MB | runtimeèšæž¬ |
ð¡ïž 泚æäºé
ãªãªãŒã¹ãã«ãã§æž¬å®
# â ãããã°ãã«ãïŒé
ãïŒ
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: ã»ãã¥ãªãã£ãšã®ãã¬ãŒããªã
ã¹ã³ã¢
ç·åã¹ã³ã¢
ãªããžããªã®åè³ªææšã«åºã¥ãè©äŸ¡
SKILL.mdãã¡ã€ã«ãå«ãŸããŠãã
ã©ã€ã»ã³ã¹ãèšå®ãããŠãã
100æå以äžã®èª¬æããã
GitHub Stars 100以äž
3ã¶æä»¥å ã«æŽæ°ããã
10å以äžãã©ãŒã¯ãããŠãã
ãªãŒãã³Issueã50æªæº
ããã°ã©ãã³ã°èšèªãèšå®ãããŠãã
1ã€ä»¥äžã®ã¿ã°ãèšå®ãããŠãã
ã¬ãã¥ãŒ
ã¬ãã¥ãŒæ©èœã¯è¿æ¥å ¬éäºå®ã§ã

