performance-tuning
Use when something is reported slow, sluggish, memory-hungry or too large, when a performance budget, Core Web Vitals threshold or latency target is breached, when asked to optimise or make code faster, or when tempted to add a cache, index, async rewrite or micro-optimisation.
NoobyGains · 已有静态检查记录
安装与使用
运行命令前先阅读来源与依赖说明。复制命令不会自动安装或执行。
npx skills add NoobyGains/godmode --skill performance-tuning技能说明
name: performance-tuning description: "Use when something is reported slow, sluggish, memory-hungry or too large, when a performance budget, Core Web Vitals threshold or latency target is breached, when asked to optimise or make code faster, or when tempted to add a cache, index, async rewrite or micro-optimisation."
Performance Tuning
Intuition about where time goes is wrong more often than it is right. You measure the problem, profile to find where the time or memory actually goes, change one thing, and measure again with the same method. No measurement, no optimisation: every change is justified by a number before and proven by a number after.
The Prime Directive
NO OPTIMISATION WITHOUT A MEASUREMENT PROVING THE PROBLEM
No exceptions. No workarounds. No shortcuts.
When to Use
Required:
- A user, test or monitor reports slowness, timeouts, high memory, large bundles or high cost.
- A performance budget or target is breached (latency percentile, Core Web Vitals, bundle size, query time, cold start).
- Someone asks to "optimise", "speed up" or "make it scale".
- You are about to add a cache, an index, concurrency, a rewrite in another language or a micro-optimisation.
Skip:
- The code does not work correctly yet. Correctness comes first; invoke
godmode:fault-diagnosisorgodmode:test-first. - There is no target and no complaint. "It might be slow some day" is not a problem; record it and move on.
- Choosing a sensible algorithm or data structure while first writing code. That is ordinary design, not tuning, as long as it does not cost clarity.
The Protocol
digraph perf {
rankdir=TB;
target [label="1. Target\n(number + percentile)", shape=box];
baseline [label="2. Baseline\n(repeatable measurement)", shape=box];
met [label="Target met?", shape=diamond];
profile [label="3. Profile\n(where does it go?)", shape=box];
hypo [label="4. One hypothesis", shape=box];
change [label="5. Change one thing", shape=box];
remeasure [label="6. Re-measure\n(same method)", shape=box];
better [label="Significant\nimprovement?", shape=diamond];
revert [label="Revert", shape=box];
done [label="7. Record + guard", shape=box];
target -> baseline -> met;
met -> done [label="yes"];
met -> profile [label="no"];
profile -> hypo -> change -> remeasure -> better;
better -> met [label="yes, keep"];
better -> revert [label="no"];
revert -> hypo;
}
-
Set the target. A number, a percentile and a context: "p95 under 300 ms for
GET /ordersat 50 requests per second", "LCP 2.5 s or less at the 75th percentile on mobile", "bundle under 200 KB gzipped". Without a target there is no end condition; ask the user for one or propose one. -
Take a baseline that you can repeat. Same input data, same environment (production-like, not a debug build), warm-up runs discarded, several runs, and the spread reported, not one number. Save the exact command and its output. Tools are in the table below.
-
Profile before you guess. Use a profiler, query plan, trace or bundle analyser to find where the time, memory or bytes go. The evidence is a profile artefact (flame graph,
EXPLAIN ANALYZEoutput, trace) that names the hot spot. Usual suspects, in rough order: database queries (N+1, missing index, full scans), network round trips made in sequence, serialisation of oversized payloads, algorithmic complexity, synchronous I/O, and memory churn. -
State one hypothesis that links the hot spot to a change: "The order list makes one query per item (N+1); a single joined query should cut p95 from 920 ms to under 300 ms."
-
Change one thing. One change per measurement, behind passing tests, so the effect is attributable. Keep the change on its own commit.
-
Re-measure with the identical method and compare against the baseline, including the spread. If the improvement is within the noise or misses the expectation, revert it, even if it "should" help. If it helps, go back to step 2 with the new baseline until the target is met.
-
Record and guard. Write the before and after numbers, the method and the commit in the PR description. Add a guard so the regression cannot return silently: a benchmark in CI, a budget check (
godmode:quality-gates), or a test that asserts the query count.
Measurement Tools
| What | Measure | Profile |
|---|---|---|
| A command or script end to end | hyperfine --warmup 3 'cmd' (compare two: hyperfine 'old' 'new') | the language profiler below |
| Python | pytest-benchmark, timeit | py-spy record -o profile.svg -- python app.py, py-spy top --pid <pid> |
| Node.js | hyperfine, a benchmark library | node --cpu-prof app.js (CPU), node --heap-prof app.js (memory); open the files in browser DevTools |
| Go | go test -bench . -benchmem | go test -bench . -cpuprofile cpu.out, then go tool pprof cpu.out |
| Rust | a benchmark harness such as Criterion | perf or a flame-graph tool on Linux |
| PostgreSQL query | EXPLAIN (ANALYZE, BUFFERS) <query> | the same plan: look for sequential scans, row-estimate errors, loops |
| HTTP service under load | a load generator at a fixed request rate, reporting percentiles | APM traces or the language profiler on the server |
| Web page | lighthouse <url> (lab), field data from real users for Core Web Vitals | browser DevTools Performance panel |
| Bundle size | the bundler's analyser or a size-limit check | the same analyser, per module |
EXPLAIN ANALYZE executes the statement. For INSERT, UPDATE or DELETE, run it inside a transaction and roll back.