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.

16 stars

Best use case

analyze-rust-optimizations is best used when you need a repeatable AI agent workflow instead of a one-off prompt.

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.

Teams using analyze-rust-optimizations should expect a more consistent output, faster repeated execution, less prompt rewriting.

When to use this skill

  • You want a reusable workflow that can be run more than once with consistent structure.

When not to use this skill

  • You only need a quick one-off answer and do not need a reusable workflow.
  • You cannot install or maintain the underlying files, dependencies, or repository context.

Installation

Claude Code / Cursor / Codex

$curl -o ~/.claude/skills/analyze-rust-optimizations/SKILL.md --create-dirs "https://raw.githubusercontent.com/diegosouzapw/awesome-omni-skill/main/skills/development/analyze-rust-optimizations/SKILL.md"

Manual Installation

  1. Download SKILL.md from GitHub
  2. Place it in .claude/skills/analyze-rust-optimizations/SKILL.md inside your project
  3. Restart your AI agent — it will auto-discover the skill

How analyze-rust-optimizations Compares

Feature / Agentanalyze-rust-optimizationsStandard Approach
Platform SupportNot specifiedLimited / Varies
Context Awareness High Baseline
Installation ComplexityUnknownN/A

Frequently Asked Questions

What does this skill do?

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.

Where can I find the source code?

You can find the source code on GitHub using the link provided at the top of the page.

SKILL.md Source

<objective>
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.
</objective>

<quick_start>
Run profiling tools first, then analyze code:

```bash
# 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**

1. **Check available tools**:
   ```bash
   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"
   ```

2. **Run benchmarks** (if `benches/` or `#[bench]` exists):
   ```bash
   cargo bench -- --save-baseline before
   ```

3. **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

4. **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):

1. **Hot path allocations**: `.clone()`, `.to_string()`, `.to_vec()` in loops
2. **Unnecessary allocations**: `String` where `&str` suffices, `Vec` where slice works
3. **Missing `#[inline]`**: Small functions called across crate boundaries
4. **Inefficient iterators**: `.collect()` followed by iteration, multiple `.iter()` passes
5. **Box/Arc overuse**: Heap allocation where stack would work
6. **HashMap/BTreeMap inefficiency**: Missing `with_capacity()`, poor hash functions
7. **String formatting in hot paths**: `format!()` allocates, prefer `write!()`
8. **Bounds checking**: Array indexing vs `.get_unchecked()` in verified-safe paths
9. **Memory layout**: Large structs, poor field ordering, excessive padding

**Phase 3: Generate Report**

Produce two outputs:
1. **Prioritized findings list** - ranked by estimated impact
2. **Detailed report** - comprehensive markdown with sections
</process>

<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:**

```bash
# 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:**
```toml
# Cargo.toml - add release profile with debug info
[profile.release]
debug = true
```
</tools_setup>

<output_format>
**1. Prioritized Findings List**

```markdown
## 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**

```markdown
# 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:

```bash
# 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>

Related Skills

analyze-project-architecture

16
from diegosouzapw/awesome-omni-skill

LLM-based architectural analysis that transforms raw project data into meaningful structure

analyze-pr-performance

16
from diegosouzapw/awesome-omni-skill

Analyze code review pipeline performance for a specific PR. Use when investigating slow PRs, identifying bottlenecks, or debugging performance issues in code reviews.

analyze

16
from diegosouzapw/awesome-omni-skill

Deep analysis and investigation

analyze-m1-module-for-migration

16
from diegosouzapw/awesome-omni-skill

Systematically analyze a Magento 1 module to determine its purpose, usage, and migration requirements for Magento 2. Use when you need to decide whether to migrate a M1 module, find alternatives, or skip it.

analyze-function

16
from diegosouzapw/awesome-omni-skill

Deep line-by-line analysis of a function or method. Explains what each line does, why it's written that way, performance implications, edge cases, and design patterns. Use when user says "analyze-function", "analyze {function}", "deep dive on {function}", or "explain {function} line by line".

analyze-friction

16
from diegosouzapw/awesome-omni-skill

Orchestrate 3-stage friction analysis workflow across conversations. Extracts raw friction, abstracts patterns, and presents for approval. Use when user wants to analyze conversation history for improvement opportunities.

analyze-codebase

16
from diegosouzapw/awesome-omni-skill

Analyze a codebase to generate a comprehensive architecture and structure report. Use when user wants to understand a codebase, explore project structure, or generate analysis.

analyze-code

16
from diegosouzapw/awesome-omni-skill

Deep code analysis using consultant agent. Identifies technical debt, risks, and improvement opportunities.

analyze-code-structure

16
from diegosouzapw/awesome-omni-skill

Examine code organization and identify structural patterns. Use when reviewing module design.

analyze-architecture

16
from diegosouzapw/awesome-omni-skill

Comprehensive brownfield architecture analysis for existing codebases. Discovers structure, identifies patterns, assesses quality, calculates production readiness, and provides actionable recommendations. Use when analyzing existing codebases to understand architecture, assess quality, or prepare for modernization.

agent-rust-engineer

16
from diegosouzapw/awesome-omni-skill

Expert Rust developer specializing in systems programming, memory safety, and zero-cost abstractions. Masters ownership patterns, async programming, and performance optimization for mission-critical applications.

acc-analyze-solid-violations

16
from diegosouzapw/awesome-omni-skill

Analyzes PHP codebase for SOLID principle violations. Detects God classes (SRP), type switches (OCP), broken contracts (LSP), fat interfaces (ISP), and concrete dependencies (DIP). Generates actionable reports with severity levels and remediation recommendations.