Performance Optimizer
Evidence-based performance optimization. Never optimize without profiling first. Every optimization comes with a trade-off analysis.
Quick Start
Investigate a Performance Issue
- Reproduce: Can you reliably reproduce the slowness? What are the conditions?
- Measure: Profile to find the actual bottleneck. Don't guess.
- Analyze: What's the root cause? Algorithm? I/O? Memory? Contention?
- Propose: 2-3 options with trade-offs (speed vs readability, memory vs CPU, etc.)
- Verify: After optimization, re-measure to confirm improvement.
The cardinal rule: Profile first, optimize second. Gut feelings about performance are wrong more often than right.
Profiling Toolkits
Rust
| Tool |
Purpose |
Command |
cargo bench |
Micro-benchmarks (criterion) |
cargo bench |
cargo flamegraph |
CPU flamegraph visualization |
cargo flamegraph --bin <name> |
perf |
Linux perf events |
perf record --call-graph dwarf ./target/release/<bin> |
valgrind --tool=callgrind |
Instruction-level profiling |
valgrind --tool=callgrind ./target/release/<bin> |
cargo bloat |
Binary size analysis |
cargo bloat --release |
| DHAT (via valgrind) |
Heap allocation profiling |
valgrind --tool=dhat ./target/release/<bin> |
Quick benchmark setup with criterion:
// benches/my_bench.rs
use criterion::{criterion_group, criterion_main, Criterion};
fn benchmark_function(c: &mut Criterion) {
c.bench_function("descriptive name", |b| {
b.iter(|| {
// code to benchmark
})
});
}
criterion_group!(benches, benchmark_function);
criterion_main!(benches);
TypeScript / React
| Tool |
Purpose |
How |
| React DevTools Profiler |
Component render timing |
Browser extension, Profiler tab |
| Lighthouse |
Overall web performance |
Chrome DevTools > Lighthouse |
console.time() / console.timeEnd() |
Quick timing |
Wrap suspicious code |
| webpack-bundle-analyzer |
Bundle size analysis |
bun run build --analyze |
performance.mark() / performance.measure() |
Web Performance API |
Precise timing of code sections |
React <Profiler> component |
Programmatic render profiling |
Wrap components in <Profiler> |
Key React performance checks:
- Unnecessary re-renders (use React DevTools "Highlight updates")
- Large component trees re-rendering from state at the top
- Missing
useMemo / useCallback for expensive computations or stable references
- Bundle size — are you importing entire libraries for one function?
Django / Python
| Tool |
Purpose |
Command |
django-debug-toolbar |
SQL queries, template timing |
Add to INSTALLED_APPS |
cProfile |
Function-level profiling |
python -m cProfile -s cumtime manage.py <command> |
py-spy |
Sampling profiler (no code changes) |
py-spy record -o profile.svg -- python manage.py runserver |
silk |
Django request/response profiling |
Add to middleware |
memory_profiler |
Line-by-line memory usage |
@profile decorator |
django.db.connection.queries |
Raw SQL query log |
from django.db import connection; print(connection.queries) |
Key Django performance checks:
- N+1 queries (use
django-debug-toolbar or assertNumQueries in tests)
- Missing database indexes on filtered/ordered columns
- Unoptimized querysets (use
.only(), .defer(), .values() when appropriate)
- Template rendering time (are you doing computation in templates?)
Complexity Analysis
Big-O Quick Reference
| Complexity |
Name |
Example |
Scale |
| O(1) |
Constant |
HashMap lookup |
Handles any size |
| O(log n) |
Logarithmic |
Binary search |
Handles billions |
| O(n) |
Linear |
Single pass over collection |
Handles millions |
| O(n log n) |
Linearithmic |
Good sorting (merge, heap) |
Handles millions |
| O(n^2) |
Quadratic |
Nested loops over same collection |
Handles thousands |
| O(n^3) |
Cubic |
Triple nested loops |
Handles hundreds |
| O(2^n) |
Exponential |
Brute-force subsets |
Handles ~25 |
When to Care
- O(1) to O(n): Almost never a problem. Don't optimize.
- O(n log n): Fine for most use cases. Only optimize for very hot paths.
- O(n^2): Watch the input size. Fine for n < 1000, problematic for n > 10000.
- O(n^3) or worse: Red flag. Look for algorithmic improvements first.
Cyclomatic and Data Flow Complexity
Per the project coding standards:
- Cyclomatic complexity: Must be < 25 per function
- Data flow complexity: Must be < 25 per function
When complexity exceeds these thresholds, the refactoring-advisor skill should be used to plan decomposition.
Common Optimization Patterns
Rust-Specific
| Pattern |
When |
Trade-off |
Replace Vec<Box<dyn Trait>> with enum dispatch |
Known, finite set of variants |
Less flexible, but no heap allocation per item |
Use &str instead of String |
Function doesn't need ownership |
Lifetime annotations may complicate API |
Pre-allocate with Vec::with_capacity() |
Known or estimated collection size |
Minor memory overhead if estimate is wrong |
Use SmallVec |
Usually-small collections |
Extra dependency, more complex type |
Replace clone() with borrows |
Cloning on hot paths |
More lifetime management |
Use rayon for data parallelism |
CPU-bound work on large collections |
Thread pool overhead for small collections |
React/TypeScript-Specific
| Pattern |
When |
Trade-off |
React.memo() |
Component re-renders with same props |
Extra memory for memoized result, stale risk if deps wrong |
useMemo / useCallback |
Expensive computation or stable reference needed |
Complexity, memory for cache |
Code splitting with React.lazy() |
Large bundles, route-level splitting |
Loading states, waterfall risk |
Virtualized lists (react-window) |
Rendering 100+ items in a list |
More complex implementation |
| Debounce/throttle |
Frequent events (scroll, resize, input) |
Delayed response |
Django/Python-Specific
| Pattern |
When |
Trade-off |
select_related() |
ForeignKey lookups in loops |
Larger initial query, but fewer total queries |
prefetch_related() |
Reverse FK / M2M lookups in loops |
Extra query, but bounded number of queries |
.values() / .values_list() |
Only need specific columns |
Lose model instance methods |
| Database indexes |
Frequently filtered/ordered columns |
Slower writes, disk space |
Caching (django.core.cache) |
Expensive queries repeated often |
Stale data risk, cache invalidation complexity |
| Bulk operations |
Creating/updating many rows |
Less granular error handling |
Trade-Off Analysis Template
When recommending an optimization, always present:
### Optimization: [Description]
**Current:** [What's happening now, with measured performance]
**Proposed:** [What to change]
| Dimension | Before | After |
|---|---|---|
| Time complexity | O(n^2) | O(n log n) |
| Space complexity | O(1) | O(n) |
| Readability | Simple nested loop | Sort + scan requires comment |
| Maintainability | Easy to modify | Requires understanding of invariant |
**Recommendation:** [Proceed / Defer / Skip]
**Justification:** [Why this trade-off is or isn't worth it in this context]
Anti-Patterns: When NOT to Optimize
- Premature optimization. If there's no measured performance problem, don't create complexity to solve one.
- Micro-optimizing cold paths. That function called once at startup? Leave it readable.
- Optimizing without benchmarks. "I think this is slow" is not evidence. Profile first.
- Sacrificing correctness for speed. Fast and wrong is worse than slow and right.
- Cargo-culting. "I read that HashMap is faster" — depends on the size, access pattern, and key type. Measure in your context.
Integration
- Data: Performance findings logged to
MEMORY.md
- Tools: Can run profiling/benchmarking tools without asking (see execution policies in AGENTS.md)
- Refactoring: When optimization requires restructuring, use the refactoring-advisor skill for safe migration planning
- Trade-offs: Every optimization recommendation includes a trade-off table
Arc skill — Performance profiling and optimization
1---2name: performance-optimizer3description: Agents should invoke this skill for slow code, high CPU/memory, latency, large data processing, algorithmic complexity, profiling plans, benchmarks, or optimization requests. Profiles first and weighs trade-offs before changing code.4metadata:5 source: https://skillmd.com/skills/firstp1ck/performance-optimizer6---78# Performance Optimizer910Evidence-based performance optimization. Never optimize without profiling first. Every optimization comes with a trade-off analysis.1112## Quick Start1314### Investigate a Performance Issue15161. **Reproduce:** Can you reliably reproduce the slowness? What are the conditions?172. **Measure:** Profile to find the actual bottleneck. Don't guess.183. **Analyze:** What's the root cause? Algorithm? I/O? Memory? Contention?194. **Propose:** 2-3 options with trade-offs (speed vs readability, memory vs CPU, etc.)205. **Verify:** After optimization, re-measure to confirm improvement.2122**The cardinal rule:** Profile first, optimize second. Gut feelings about performance are wrong more often than right.2324---2526## Profiling Toolkits2728### Rust2930| Tool | Purpose | Command |31|---|---|---|32| `cargo bench` | Micro-benchmarks (criterion) | `cargo bench` |33| `cargo flamegraph` | CPU flamegraph visualization | `cargo flamegraph --bin <name>` |34| `perf` | Linux perf events | `perf record --call-graph dwarf ./target/release/<bin>` |35| `valgrind --tool=callgrind` | Instruction-level profiling | `valgrind --tool=callgrind ./target/release/<bin>` |36| `cargo bloat` | Binary size analysis | `cargo bloat --release` |37| DHAT (via valgrind) | Heap allocation profiling | `valgrind --tool=dhat ./target/release/<bin>` |3839**Quick benchmark setup with criterion:**4041```rust42// benches/my_bench.rs43use criterion::{criterion_group, criterion_main, Criterion};4445fn benchmark_function(c: &mut Criterion) {46 c.bench_function("descriptive name", |b| {47 b.iter(|| {48 // code to benchmark49 })50 });51}5253criterion_group!(benches, benchmark_function);54criterion_main!(benches);55```5657### TypeScript / React5859| Tool | Purpose | How |60|---|---|---|61| React DevTools Profiler | Component render timing | Browser extension, Profiler tab |62| Lighthouse | Overall web performance | Chrome DevTools > Lighthouse |63| `console.time()` / `console.timeEnd()` | Quick timing | Wrap suspicious code |64| webpack-bundle-analyzer | Bundle size analysis | `bun run build --analyze` |65| `performance.mark()` / `performance.measure()` | Web Performance API | Precise timing of code sections |66| React `<Profiler>` component | Programmatic render profiling | Wrap components in `<Profiler>` |6768**Key React performance checks:**69- Unnecessary re-renders (use React DevTools "Highlight updates")70- Large component trees re-rendering from state at the top71- Missing `useMemo` / `useCallback` for expensive computations or stable references72- Bundle size — are you importing entire libraries for one function?7374### Django / Python7576| Tool | Purpose | Command |77|---|---|---|78| `django-debug-toolbar` | SQL queries, template timing | Add to INSTALLED_APPS |79| `cProfile` | Function-level profiling | `python -m cProfile -s cumtime manage.py <command>` |80| `py-spy` | Sampling profiler (no code changes) | `py-spy record -o profile.svg -- python manage.py runserver` |81| `silk` | Django request/response profiling | Add to middleware |82| `memory_profiler` | Line-by-line memory usage | `@profile` decorator |83| `django.db.connection.queries` | Raw SQL query log | `from django.db import connection; print(connection.queries)` |8485**Key Django performance checks:**86- N+1 queries (use `django-debug-toolbar` or `assertNumQueries` in tests)87- Missing database indexes on filtered/ordered columns88- Unoptimized querysets (use `.only()`, `.defer()`, `.values()` when appropriate)89- Template rendering time (are you doing computation in templates?)9091---9293## Complexity Analysis9495### Big-O Quick Reference9697| Complexity | Name | Example | Scale |98|---|---|---|---|99| O(1) | Constant | HashMap lookup | Handles any size |100| O(log n) | Logarithmic | Binary search | Handles billions |101| O(n) | Linear | Single pass over collection | Handles millions |102| O(n log n) | Linearithmic | Good sorting (merge, heap) | Handles millions |103| O(n^2) | Quadratic | Nested loops over same collection | Handles thousands |104| O(n^3) | Cubic | Triple nested loops | Handles hundreds |105| O(2^n) | Exponential | Brute-force subsets | Handles ~25 |106107### When to Care108109- **O(1) to O(n):** Almost never a problem. Don't optimize.110- **O(n log n):** Fine for most use cases. Only optimize for very hot paths.111- **O(n^2):** Watch the input size. Fine for n < 1000, problematic for n > 10000.112- **O(n^3) or worse:** Red flag. Look for algorithmic improvements first.113114### Cyclomatic and Data Flow Complexity115116Per the project coding standards:117118- **Cyclomatic complexity:** Must be < 25 per function119- **Data flow complexity:** Must be < 25 per function120121When complexity exceeds these thresholds, the refactoring-advisor skill should be used to plan decomposition.122123---124125## Common Optimization Patterns126127### Rust-Specific128129| Pattern | When | Trade-off |130|---|---|---|131| Replace `Vec<Box<dyn Trait>>` with enum dispatch | Known, finite set of variants | Less flexible, but no heap allocation per item |132| Use `&str` instead of `String` | Function doesn't need ownership | Lifetime annotations may complicate API |133| Pre-allocate with `Vec::with_capacity()` | Known or estimated collection size | Minor memory overhead if estimate is wrong |134| Use `SmallVec` | Usually-small collections | Extra dependency, more complex type |135| Replace `clone()` with borrows | Cloning on hot paths | More lifetime management |136| Use `rayon` for data parallelism | CPU-bound work on large collections | Thread pool overhead for small collections |137138### React/TypeScript-Specific139140| Pattern | When | Trade-off |141|---|---|---|142| `React.memo()` | Component re-renders with same props | Extra memory for memoized result, stale risk if deps wrong |143| `useMemo` / `useCallback` | Expensive computation or stable reference needed | Complexity, memory for cache |144| Code splitting with `React.lazy()` | Large bundles, route-level splitting | Loading states, waterfall risk |145| Virtualized lists (`react-window`) | Rendering 100+ items in a list | More complex implementation |146| Debounce/throttle | Frequent events (scroll, resize, input) | Delayed response |147148### Django/Python-Specific149150| Pattern | When | Trade-off |151|---|---|---|152| `select_related()` | ForeignKey lookups in loops | Larger initial query, but fewer total queries |153| `prefetch_related()` | Reverse FK / M2M lookups in loops | Extra query, but bounded number of queries |154| `.values()` / `.values_list()` | Only need specific columns | Lose model instance methods |155| Database indexes | Frequently filtered/ordered columns | Slower writes, disk space |156| Caching (`django.core.cache`) | Expensive queries repeated often | Stale data risk, cache invalidation complexity |157| Bulk operations | Creating/updating many rows | Less granular error handling |158159---160161## Trade-Off Analysis Template162163When recommending an optimization, always present:164165```markdown166### Optimization: [Description]167168**Current:** [What's happening now, with measured performance]169**Proposed:** [What to change]170171| Dimension | Before | After |172|---|---|---|173| Time complexity | O(n^2) | O(n log n) |174| Space complexity | O(1) | O(n) |175| Readability | Simple nested loop | Sort + scan requires comment |176| Maintainability | Easy to modify | Requires understanding of invariant |177178**Recommendation:** [Proceed / Defer / Skip]179**Justification:** [Why this trade-off is or isn't worth it in this context]180```181182---183184## Anti-Patterns: When NOT to Optimize185186- **Premature optimization.** If there's no measured performance problem, don't create complexity to solve one.187- **Micro-optimizing cold paths.** That function called once at startup? Leave it readable.188- **Optimizing without benchmarks.** "I think this is slow" is not evidence. Profile first.189- **Sacrificing correctness for speed.** Fast and wrong is worse than slow and right.190- **Cargo-culting.** "I read that HashMap is faster" — depends on the size, access pattern, and key type. Measure in your context.191192---193194## Integration195196- **Data:** Performance findings logged to `MEMORY.md`197- **Tools:** Can run profiling/benchmarking tools without asking (see execution policies in AGENTS.md)198- **Refactoring:** When optimization requires restructuring, use the refactoring-advisor skill for safe migration planning199- **Trade-offs:** Every optimization recommendation includes a trade-off table200201---202203_Arc skill — Performance profiling and optimization_