Optimization Rescue
Salvage optimization work done without proper benchmarks. Generate evidence, validate claims, and discover bugs hiding in "optimizations."
Valuable Outcomes
| Outcome | Value | Action |
|---|---|---|
| Change validated | Real improvement proven | KEEP, include in rescued branch |
| Change invalidated | No improvement | DISCARD, record lesson |
| Bug discovered | Correctness issue found | Invoke /validate-correctness |
Bugs found during rescue are SUCCESS, not failure.
Phase 0: Pre-flight
Run /preflight first.
Phase 1: Audit
bash
git diff --name-only origin/main...HEAD | grep '\.go$'
For each change, categorize:
- •Preallocation
- •Pooling
- •Buffer reuse
- •Data structure change
- •Potential bug (suspicious patterns)
Phase 2: Triage
| Hot Path? | Decision |
|---|---|
| Yes (top 10%) | INVESTIGATE |
| Warm (10-25%) | SKEPTICAL |
| Cold | LIKELY DISCARD |
| Looks buggy | INVESTIGATE for correctness |
Bug Warning Signs
- •
make([]T, n)followed byappend - •Missing nil checks
- •Changed return semantics
- •Slice aliasing
Phase 3: Generate Evidence
Create Benchmarks
go
func BenchmarkFunction(b *testing.B) {
data := createTestData()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = Function(data)
}
}
Run Comparison
bash
benchstat /tmp/baseline.txt /tmp/optimized.txt
Phase 4: Validate
| benchstat Result | Decision |
|---|---|
| >=5% improvement, p<0.05 | KEEP |
| No significant change | DISCARD |
| Tests fail | Possible BUG → /validate-correctness |
| Behavior differs | Possible BUG → /validate-correctness |
Phase 5: Handle Bug Discovery
If rescue uncovers a bug instead of an optimization:
code
/validate-correctness
After validation:
- •Record bug in validations.yaml (via validate-correctness)
- •Record rescue as BUG_FOUND in rescues.yaml
- •The bug fix becomes part of rescued branch (with tests!)
Phase 6: Reconstruct
bash
git checkout main git checkout -b mem-opt/<name>-rescued
Apply only:
- •KEEP changes (validated optimizations)
- •BUG_FOUND changes (with tests from /validate-correctness)
Discard everything else.
Phase 7: Record
yaml
# Append to rescues.yaml
- branch: mem-opt/original
date: 2026-01-06
audited: 12
kept: 3
discarded: 7
bugs_found: 2
rescued_as: mem-opt/original-rescued
lesson: "<pattern>"
Usage
code
/rescue-optimization