Claude-skill-registry analyze-rust-optimizations
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.
git clone https://github.com/majiayu000/claude-skill-registry
T=$(mktemp -d) && git clone --depth=1 https://github.com/majiayu000/claude-skill-registry "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/data/analyze-rust-optimizations" ~/.claude/skills/majiayu000-claude-skill-registry-analyze-rust-optimizations && rm -rf "$T"
skills/data/analyze-rust-optimizations/SKILL.md- uses sudo
<quick_start> Run profiling tools first, then analyze code:
# Check for benchmarks ls benches/ 2>/dev/null || ls **/bench*.rs 2>/dev/null # Run benchmarks if available cargo bench # Generate flamegraph (if installed) cargo flamegraph --bench <benchmark_name> # Check binary/dependency bloat cargo bloat --release cargo bloat --release --crates
If tools missing, see
<tools_setup> for installation.
</quick_start>
<process>
**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
orbenches/
exists):#[bench]cargo bench -- --save-baseline before -
CPU profiling (pick available tool):
- generates SVG flamegraphcargo flamegraph
- sampling profilersamply record cargo run --release
- Linux perfperf record cargo run --release && perf report
-
Memory profiling:
- heap allocationsheaptrack cargo run --release
- memory over timevalgrind --tool=massif
viaDHAT
instrumentation#[global_allocator]
Phase 2: Static Code Analysis
Scan the codebase for these patterns (priority order):
- Hot path allocations:
,.clone()
,.to_string()
in loops.to_vec() - Unnecessary allocations:
whereString
suffices,&str
where slice worksVec - Missing
: Small functions called across crate boundaries#[inline] - Inefficient iterators:
followed by iteration, multiple.collect()
passes.iter() - Box/Arc overuse: Heap allocation where stack would work
- HashMap/BTreeMap inefficiency: Missing
, poor hash functionswith_capacity() - String formatting in hot paths:
allocates, preferformat!()write!() - Bounds checking: Array indexing vs
in verified-safe paths.get_unchecked() - 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 </process>
<optimization_patterns> Runtime Performance (Priority 1)
| Pattern | Problem | Solution |
|---|---|---|
in loops | Repeated heap allocation | Borrow, use , or hoist clone |
| Intermediate allocation | Chain iterators directly |
Missing | Function call overhead | Add or |
in hot path | Allocates String | Use to existing buffer |
default hasher | SipHash is slow | Use or |
| Recursive without TCO | Stack growth, cache misses | Convert to iteration |
Virtual dispatch () | Indirect call overhead | Use generics or enum dispatch |
Memory Usage (Priority 2)
| Pattern | Problem | Solution |
|---|---|---|
for static text | Heap allocation | Use or |
without capacity | Repeated reallocation | |
| Large enum variants | Wastes memory on small variants | Box large variants |
| Heap + vtable overhead | Consider enum dispatch |
| Struct field ordering | Padding waste | Order fields large-to-small |
where suffices | Extra atomic overhead | Use if single-threaded |
| Owned in struct fields | Forces cloning | Consider borrowing with lifetime |
| </optimization_patterns> |
<tools_setup> Install profiling tools if missing:
# Flamegraph (CPU profiling visualization) cargo install flamegraph # Linux: sudo apt install linux-perf # macOS: works via dtrace # cargo-bloat (binary size analysis) cargo install cargo-bloat # samply (sampling profiler) cargo install samply # heaptrack (memory profiling) - Linux only sudo apt install heaptrack heaptrack-gui # criterion (micro-benchmarking) # Add to Cargo.toml: criterion = "0.5"
For accurate profiling:
# Cargo.toml - add release profile with debug info [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:
# Find .clone() in potential hot paths rg '\.clone\(\)' --type rust -n # Find allocating methods in loops rg 'for .* in|while|loop' -A 10 --type rust | rg '\.to_string\(\)|\.to_vec\(\)|\.clone\(\)' # Find HashMap/Vec without capacity hints rg 'HashMap::new\(\)|Vec::new\(\)' --type rust -n # Find format! macro usage rg 'format!\(' --type rust -n # Find missing inline attributes on pub functions rg '^pub fn \w+' --type rust -n # Find large enums (check for Box opportunities) rg '^pub enum|^enum' -A 20 --type rust # Find dyn Trait usage rg 'dyn \w+' --type rust -n
</static_analysis_commands>
<success_criteria> Analysis is complete when:
- Available profiling tools identified (or install instructions provided)
- Dynamic profiling run (benchmarks, flamegraph, memory profiler)
- Static analysis completed for all optimization patterns
- Findings prioritized by impact (Critical/Important/Minor)
- Each finding includes file:line reference
- Each finding includes specific fix recommendation
- Prioritized findings list generated
- Detailed markdown report generated
- Report includes methodology and tool versions </success_criteria>