From code-foundations
Guides measure-first optimization: profile to identify hot spots, apply algorithm/data-structure improvements before micro-optimizations, and validate each change to prevent regression.
How this skill is triggered — by the user, by Claude, or both
Slash command
/code-foundations:performance-optimizationThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
**Do not optimize based on intuition -- profile first.**
Do not optimize based on intuition -- profile first.
No measurement = no optimization. This gate is non-negotiable.
This skill covers single-threaded, single-process code tuning for general-purpose computing.
Not covered (need specialized guidance):
Simpler code usually runs faster. Fewer special cases = less code to check; deep modules = more work per call with fewer layer crossings; complicated code does extraneous or redundant work.
Each step is a gate. Do NOT skip steps.
1. Is the program correct and complete?
NO -> Make it correct first. STOP optimization.
YES -> Continue
2. Have you measured to find the actual bottleneck?
NO -> Profile/measure first. Do NOT guess.
YES -> Continue
3. Can requirements be relaxed?
YES -> Relax requirements. Done.
NO -> Continue
4. Can design/architecture solve it? (Stage 2: Fundamental Fixes)
YES -> Fix design. Done.
NO -> Continue
5. Can algorithm/data structure solve it?
YES -> Change algorithm. Done.
NO -> Continue
6. Can compiler flags help? (40-59% improvement possible)
YES -> Enable optimizations. Measure.
NO -> Continue
7. Is it in the <4% that causes >50% of runtime?
NO -> Do NOT optimize this code. Find actual hot spot.
YES -> PROCEED with code tuning (see below)
What counts as valid measurement:
Identify WHICH dimension: throughput, latency, memory, or CPU. Different problems need different solutions.
Before code-level changes, check for architectural fixes:
If a fundamental fix exists, implement it with standard design techniques. If not, continue down the tree.
When no fundamental fix is available, redesign the critical path:
Consolidation techniques:
| Technique | Example |
|---|---|
| Encode multiple conditions in single value | Variable that is 0 when any special case applies |
| Single test for multiple cases | Replace 6 individual checks with 1 combined check |
| Combine layers into single method | Critical path handled in one method, not three |
| Merge variables | Combine multiple values into single structure |
Only reached after completing the 7-step decision tree.
1. Save working version (cannot revert without backup)
2. Make ONE change (multiple changes = unmeasurable)
3. Measure improvement (same workload, before/after)
4. Keep if faster, revert if not (no "close enough")
5. Repeat
Logic:
Loops:
Data:
Expressions:
Checklist and code examples: Read(${CLAUDE_SKILL_DIR}/checklists.md)
Re-measure before keeping any change. Keep only if: significant speedup (with data), OR simpler AND at least as fast. Otherwise back it out.
| Red Flag | Symptom |
|---|---|
| Premature Optimization | Optimizing without measurement |
| Death by Thousand Cuts | Many small inefficiencies, no single fix helps (5-10x slower) |
| Pass-Through Methods | Identical signature to caller, unnecessary layer crossing |
| Shallow Layers | Multiple layers providing same abstraction |
| Repeated Special Cases | Same conditions checked multiple times |
| Trading maintainability for <10% gain | Complex optimization for minor speedup |
| Threshold/Rule | Value | Source |
|---|---|---|
| Hot spot concentration | <4% causes >50% runtime | Knuth 1971 |
| Failed optimization rate | >50% negligible or negative | CC p.607 |
| Compiler optimization gains | 40-59% improvement possible | CC p.596 |
| I/O vs memory | ~1000x difference | CC p.591 |
Checklist: Read(${CLAUDE_SKILL_DIR}/checklists.md)
Output Format:
| Item | Status | Evidence | Location |
|---|---|---|---|
| Measured before tuning? | VIOLATION | No profiler/measurement found | N/A |
| Loop unswitching opportunity | WARNING | Invariant if (debug) inside loop | app.py:142 |
Severity: VIOLATION (clear anti-pattern), WARNING (needs measurement), PASS (no issues)
| After | Next |
|---|---|
| Optimization complete | Verify design not degraded |
| Structure degraded | cc-refactoring-guidance |
npx claudepluginhub ryanthedev/code-foundationsLocates a hot path in code, fans out multiple transformation agents, benchmarks candidates, and commits the winning speedup with proven measurement.
Measures and optimizes performance with data-driven profiling, identifying bottlenecks like N+1 queries, missing indexes, and synchronous I/O. Triggers on performance, speed, latency, profiling, or benchmark keywords.
Use when performance is a concern - sluggish pages, slow queries, bloated bundles, high-latency APIs, or whenever someone says "optimize" or "make it faster"