DOCUMENTATION
v0.1.0GITHUB
stack / MIT
DOC7 sections

rust-performance

Guides Rust performance optimization. Use when profiling, benchmarking, reducing allocations, improving cache locality, choosing between rayon/async/threads, or applying SIMD/parallelism.

stack

rust-performance

actionbook/rust-skills

Metadata

Config Path
License
License Source

Description

Guides Rust performance optimization. Use when profiling, benchmarking, reducing allocations, improving cache locality, choosing between rayon/async/threads, or applying SIMD/parallelism.

Source

Docs Path
Original Source

Stack Context

9 skills in stack
agent-friendly-cli

rust-agent-friendly-cli

actionbook/rust-skills

Designs Rust command-line interfaces that are reliable for agents, scripts, and humans. Use when building or reviewing Rust binaries, clap command trees, diagnostics commands, codegen tools, or automation-facing workflows.

anti-patterns

rust-anti-patterns

actionbook/rust-skills

Identifies and fixes common Rust anti-patterns. Use during code review, when seeing excessive clones/unwraps/index loops, or when code fights the borrow checker instead of working with it.

coding-guidelines

rust-coding-guidelines

actionbook/rust-skills

50 core Rust coding conventions covering naming, data types, strings, error handling, memory, concurrency, async, and modern crate recommendations. Use when reviewing code style or setting up project conventions.

concurrency

rust-concurrency

actionbook/rust-skills

Guides concurrency and async decisions in Rust. Use when encountering Send/Sync errors (E0277), choosing between threads vs async, designing shared state, or debugging deadlocks.

error-handling

rust-error-handling

actionbook/rust-skills

Guides Rust error handling strategy. Use when choosing between Result/Option/panic, using anyhow vs thiserror, propagating errors with ?, or designing custom error types.

actionbook/rust-skills

Guides ownership, borrowing, and lifetime decisions in Rust. Use when encountering E0382, E0597, E0506, E0507, move errors, or when designing data ownership.

performance

rust-performance

Current skill

Guides Rust performance optimization. Use when profiling, benchmarking, reducing allocations, improving cache locality, choosing between rayon/async/threads, or applying SIMD/parallelism.

type-driven-design

rust-type-driven-design

actionbook/rust-skills

Guides type-driven design in Rust. Use when encoding invariants in types, applying newtype pattern, implementing type state machines, using PhantomData, or making invalid states unrepresentable.

zero-cost-abstractions

rust-zero-cost-abstractions

actionbook/rust-skills

Guides generic vs trait object decisions in Rust. Use when choosing between static and dynamic dispatch, designing traits, handling E0277/E0038, or deciding between enum and dyn Trait.

Markdown

configs/stacks/rust/performance/SKILL.mdMarkdown
---
name: rust-performance
description: Guides Rust performance optimization. Use when profiling, benchmarking, reducing allocations, improving cache locality, choosing between rayon/async/threads, or applying SIMD/parallelism.
license: MIT
origin_url: https://github.com/actionbook/rust-skills
---

# Performance Optimization

> **Layer 2: Design Choices**

## Core Question

**What's the bottleneck, and is optimization worth it?**

Before optimizing:
- Have you measured? (Don't guess)
- What's the acceptable performance target?
- Will optimization add significant complexity?

---

## Thinking Prompt

1. **Have you measured?**
   - Profile first → flamegraph, perf
   - Benchmark → criterion, `cargo bench`
   - Identify actual hotspots

2. **What's the priority?**
   - Algorithm (10x–1000x improvement)
   - Data structure (2x–10x)
   - Allocation reduction (2x–5x)
   - Cache optimization (1.5x–3x)

3. **What's the trade-off?**
   - Complexity vs speed
   - Memory vs CPU
   - Latency vs throughput

---

## Optimization Priority

```
1. Algorithm choice     (10x - 1000x)
2. Data structure       (2x - 10x)
3. Allocation reduction (2x - 5x)
4. Cache optimization   (1.5x - 3x)
5. SIMD/Parallelism     (2x - 8x)
```

---

## Common Techniques

| Technique | When | How |
|-----------|------|-----|
| Pre-allocation | Known size | `Vec::with_capacity(n)` |
| Avoid cloning | Hot paths | Use references or `Cow<T>` |
| Batch operations | Many small ops | Collect then process |
| SmallVec | Usually small | `smallvec::SmallVec<[T; N]>` |
| Inline buffers | Fixed-size data | Arrays over Vec |

## Tooling

| Tool | Purpose |
|------|---------|
| `cargo bench` | Micro-benchmarks |
| `criterion` | Statistical benchmarks |
| `perf` / `flamegraph` | CPU profiling |
| `heaptrack` | Allocation tracking |
| `valgrind` / `cachegrind` | Cache analysis |

---

## Common Mistakes

| Mistake | Why Wrong | Better |
|---------|-----------|--------|
| Optimize without profiling | Wrong target | Profile first |
| Benchmark in debug mode | Meaningless results | Always `--release` |
| Use `LinkedList` | Cache unfriendly | `Vec` or `VecDeque` |
| Hidden `.clone()` in loop | Unnecessary allocs | Use references |
| Premature optimization | Wasted effort | Make it correct first |

---

## Anti-Patterns

| Anti-Pattern | Why Bad | Better |
|--------------|---------|--------|
| Clone to avoid lifetimes | Performance cost | Proper ownership |
| Box everything | Indirection cost | Stack allocation when possible |
| HashMap for small sets | Overhead | Vec with linear search |
| String concat in loop | O(n²) | `String::with_capacity` or write! |

---

## Related Skills

- `ownership` — avoid clones, use references in hot paths
- `concurrency` — rayon for data parallelism, tokio for I/O
- `anti-patterns` — performance anti-patterns