Analyze and optimize code performance. Use when the user says /perf, asks about performance, wants to optimize slow code, profile a bottleneck, reduce memory usage, or improve response times. Triggers: perf, performance, slow, optimize, bottleneck, profile, memory, latency, throughput, speed up.
Identify and resolve performance bottlenecks.
Understand the problem:
Profile before optimizing:
cProfile, py-spy, memory_profiler, time.perf_counter--prof, clinic.js, console.time, perf_hookspprof, go test -benchcargo benchflamegraphtime command, hyperfine for benchmarksAnalyze the code for common issues:
| Category | What to look for |
|---|---|
| Algorithm | O(n^2) or worse where O(n log n) exists, unnecessary sorting |
| Database | N+1 queries, missing indexes, SELECT *, no pagination |
| I/O | Sequential where parallel is possible, no batching, no streaming |
| Memory | Large object copies, no generators/iterators, memory leaks, unbounded caches |
| Caching | Repeated expensive computations, no memoization, cache stampede |
| Rendering | Unnecessary re-renders, large DOM, no virtualization |
| Network | No compression, no connection pooling, chatty APIs |
| Build | No tree-shaking, large bundles, no code splitting |
Propose fixes ranked by impact:
Verify improvements:
EXPLAIN ANALYZE).