| name | analyze-rust-optimizations |
| description | This skill performs thorough analysis of Rust libraries to find optimization opportunities. It should be used when reviewing Rust code for performance improvements, memory efficiency, or when profiling indicates bottlenecks. Focuses on runtime performance and memory usage through dynamic profiling tools and static code analysis. |
Analyze Rust libraries for optimization opportunities with focus on runtime performance and memory usage. Uses dynamic profiling tools when available, falls back to static analysis, and produces prioritized findings with a detailed report.
<quick_start>
Run profiling tools first, then analyze code:
ls benches/ 2>/dev/null || ls **/bench*.rs 2>/dev/null
cargo bench
cargo flamegraph --bench <benchmark_name>
cargo bloat --release
cargo bloat --release --crates
If tools missing, see <tools_setup> for installation.
</quick_start>
**Phase 1: Tool Detection & Dynamic Analysis**
-
Check available tools:
command -v flamegraph && echo "flamegraph: installed"
cargo bloat --version 2>/dev/null && echo "cargo-bloat: installed"
command -v samply && echo "samply: installed"
command -v heaptrack && echo "heaptrack: installed"
-
Run benchmarks (if benches/ or #[bench] exists):
cargo bench -- --save-baseline before
-
CPU profiling (pick available tool):
cargo flamegraph - generates SVG flamegraph
samply record cargo run --release - sampling profiler
perf record cargo run --release && perf report - Linux perf
-
Memory profiling:
heaptrack cargo run --release - heap allocations
valgrind --tool=massif - memory over time
DHAT via #[global_allocator] instrumentation
Phase 2: Static Code Analysis
Scan the codebase for these patterns (priority order):
- Hot path allocations:
.clone(), .to_string(), .to_vec() in loops
- Unnecessary allocations:
String where &str suffices, Vec where slice works
- Missing
#[inline]: Small functions called across crate boundaries
- Inefficient iterators:
.collect() followed by iteration, multiple .iter() passes
- Box/Arc overuse: Heap allocation where stack would work
- HashMap/BTreeMap inefficiency: Missing
with_capacity(), poor hash functions
- String formatting in hot paths:
format!() allocates, prefer write!()
- Bounds checking: Array indexing vs
.get_unchecked() in verified-safe paths
- Memory layout: Large structs, poor field ordering, excessive padding
Phase 3: Generate Report
Produce two outputs:
- Prioritized findings list - ranked by estimated impact
- Detailed report - comprehensive markdown with sections
<optimization_patterns>
Runtime Performance (Priority 1)
| Pattern | Problem | Solution |
|---|
.clone() in loops | Repeated heap allocation | Borrow, use Cow<T>, or hoist clone |
collect().iter() | Intermediate allocation | Chain iterators directly |
Missing #[inline] | Function call overhead | Add #[inline] or #[inline(always)] |
format!() in hot path | Allocates String | Use write!() to existing buffer |
HashMap default hasher | SipHash is slow | Use FxHashMap or ahash |
| Recursive without TCO | Stack growth, cache misses | Convert to iteration |
Virtual dispatch (dyn Trait) | Indirect call overhead | Use generics or enum dispatch |
Memory Usage (Priority 2)
| Pattern | Problem | Solution |
|---|
String for static text | Heap allocation | Use &'static str or Cow<str> |
Vec<T> without capacity | Repeated reallocation | Vec::with_capacity(n) |
| Large enum variants | Wastes memory on small variants | Box large variants |
Box<dyn Trait> | Heap + vtable overhead | Consider enum dispatch |
| Struct field ordering | Padding waste | Order fields large-to-small |
Arc where Rc suffices | Extra atomic overhead | Use Rc if single-threaded |
| Owned in struct fields | Forces cloning | Consider borrowing with lifetime |
| </optimization_patterns> | | |
<tools_setup>
Install profiling tools if missing:
cargo install flamegraph
cargo install cargo-bloat
cargo install samply
sudo apt install heaptrack heaptrack-gui
For accurate profiling:
[profile.release]
debug = true
</tools_setup>
<output_format>
1. Prioritized Findings List
## Optimization Findings (Prioritized)
### Critical (High Impact)
1. **[PERF]** `src/parser.rs:142` - `.clone()` inside hot loop, ~500k calls/sec
2. **[MEM]** `src/cache.rs:89` - HashMap without capacity, grows 12 times
### Important (Medium Impact)
3. **[PERF]** `src/lib.rs:34` - Missing `#[inline]` on public API hot path
4. **[MEM]** `src/types.rs:15-28` - Struct padding wastes 24 bytes per instance
### Minor (Low Impact)
5. **[PERF]** `src/utils.rs:67` - `format!()` could use `write!()`
2. Detailed Report Structure
# Rust Optimization Analysis Report
## Executive Summary
- Total findings: X
- Critical: Y | Important: Z | Minor: W
- Estimated performance gain: ...
## Profiling Results
### CPU Profile
[Flamegraph or hotspot analysis]
### Memory Profile
[Allocation patterns, peak usage]
## Findings by Category
### Runtime Performance
[Detailed findings with code snippets and fixes]
### Memory Usage
[Detailed findings with before/after layouts]
## Recommended Changes
[Prioritized action items with effort estimates]
## Appendix
- Tool versions used
- Benchmark results
- Profiling methodology
</output_format>
<static_analysis_commands>
Use these to find common patterns:
rg '\.clone\(\)' --type rust -n
rg 'for .* in|while|loop' -A 10 --type rust | rg '\.to_string\(\)|\.to_vec\(\)|\.clone\(\)'
rg 'HashMap::new\(\)|Vec::new\(\)' --type rust -n
rg 'format!\(' --type rust -n
rg '^pub fn \w+' --type rust -n
rg '^pub enum|^enum' -A 20 --type rust
rg 'dyn \w+' --type rust -n
</static_analysis_commands>
<success_criteria>
Analysis is complete when: