AgentSkillsCN

hunt-optimization

系统性的优化猎手。既能发现内存优化方案,也能揪出潜在的 Bug——这两者都极具价值。一旦发现 Bug,立即运行 /validate-correctness。

SKILL.md
--- frontmatter
name: hunt-optimization
description: Systematic optimization hunter. Finds memory optimizations AND bugs - both are valuable. Run /validate-correctness when bugs are discovered.

Optimization Hunt

Systematically explores the codebase, implements optimizations, validates with benchmarks. Finding bugs is equally valuable as finding optimizations.

Valuable Outcomes

OutcomeValueAction
Optimization worksMemory improvedSubmit to /review-optimization
Optimization failsLearned cold pathRecord, next target
Bug discoveredFound correctness issueInvoke /validate-correctness

All three outcomes build institutional knowledge.


Phase 0: Pre-flight

Run /preflight first. Then check what's already been done:

bash
cat ~/.claude/skills/hunt-optimization/hunts.yaml
cat ~/.claude/skills/review-optimization/reviews.yaml

If a target/technique combination exists in either database → SKIP IT.


Phase 1: Select Target

Target Sources

Use profiling data when available:

bash
go tool pprof -top -alloc_objects mem.pprof | head -20

Otherwise, check the queue in hunts.yaml or pick from hot subsystems:

  • pkg/metrics - per-metric-sample path
  • pkg/aggregator - aggregation hot loop
  • comp/dogstatsd - ingestion path
  • comp/core/tagger - tag resolution

Phase 2: Analyze Target

Identify Opportunity Type

PatternTechniqueBug Risk
append() in loopPreallocationmake([]T, n) vs make([]T, 0, n)
map[K]V{}Size hintNone
String + in loopstrings.BuilderNone
new(T) repeatedsync.PoolReset state issues
Large struct by valuePointerNil pointer risk

Watch for bugs while analyzing - they're valuable findings.


Phase 3: Implement

bash
git checkout main && git pull
git checkout -b mem-opt/<package>-<technique>

Make ONE change. Commit:

bash
git commit -m "mem-opt: <description>

Hypothesis: <expected improvement>
Technique: <prealloc|pool|builder|etc>
"

Phase 4: Benchmark

bash
# Baseline first, then optimized
git stash
git checkout main
go test -bench=<Name> -benchmem -count=10 ./pkg/... > /tmp/baseline.txt
git checkout -
git stash pop
go test -bench=<Name> -benchmem -count=10 ./pkg/... > /tmp/optimized.txt
benchstat /tmp/baseline.txt /tmp/optimized.txt

Decision Point

ResultAction
B/op improved >=5%, p<0.05/review-optimization
No change or regression→ Record FAILURE, next target
Tests fail unexpectedly→ Might be a BUG
Behavior changed→ Might be a BUG

Phase 5: Handle Bug Discovery

If during hunting you discover a bug (not an optimization):

Invoke Correctness Validation

code
/validate-correctness

This skill will:

  1. Create reproducing test
  2. Verify fix works
  3. Add fuzz test if appropriate
  4. Record in validations.yaml

After Validation

Return here and record as BUG_FOUND (a success!):

yaml
  - target: pkg/foo/bar.go:Function
    date: 2026-01-06
    result: BUG_FOUND
    bug_id: <from validations.yaml>
    lesson: "Found make([]T, n) + append bug"

Then continue hunting - don't stop.


Phase 6: Record & Continue

MANDATORY: Update hunts.yaml

yaml
  - target: pkg/path/file.go:Function
    date: 2026-01-06
    branch: mem-opt/name
    technique: preallocation
    result: SUCCESS|FAILURE|BUG_FOUND
    delta_bop: "-18%" | "~" | "N/A"
    lesson: "<pattern>"

Immediately Continue

code
Target completed → Back to Phase 1 → Pick new target → Never stop

Usage

code
/hunt-optimization

A 10% combined success rate (optimizations + bugs) is excellent. Most targets are cold paths - that's expected.