用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/caarlos0/dotfiles --skill go-performance命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
正在显示 SKILL.md
| name | go-performance |
| description | Profile and optimize Go CPU, allocations, GC, concurrency, and I/O with benchmarks and pprof. |
Profile before optimizing. Pin the Go version, GOMAXPROCS, input, and
benchmark flags. Change one measured bottleneck and compare with benchstat.
Benchmark production code with testing.B. Report allocations and throughput
where relevant:
func BenchmarkEncode(b *testing.B) {
b.ReportAllocs()
b.SetBytes(int64(len(input)))
for b.Loop() {
Encode(input)
}
}
Use b.RunParallel only when contention or scaling is the behavior under test.
Collect multiple samples:
go test -run='^$' -bench=BenchmarkEncode -benchmem -count=10 ./pkg > old.txt
go test -run='^$' -bench=BenchmarkEncode -benchmem -count=10 ./pkg > new.txt
benchstat old.txt new.txt
Use realistic sizes and distributions. Include benchstat output with a
performance change; a single ns/op result is not evidence.
From tests:
go test -bench=BenchmarkEncode \
-cpuprofile=cpu.prof -memprofile=heap.prof \
-blockprofile=block.prof -mutexprofile=mutex.prof
go tool pprof -http=:8080 cpu.prof
go tool pprof -top -cum -alloc_objects heap.prof
For services, expose net/http/pprof only on an authenticated internal
listener. Use:
allocs for allocation sites and heap for live objects.go tool trace for scheduler latency, goroutine transitions, syscalls, GC,
and network blocking that sampling profiles do not explain.go build -gcflags='-m=2' ./pkg/...
Escape and inlining diagnostics are leads. Confirm the allocation or call site in a benchmark or profile before changing ownership.
make([]T, 0, n) and make(map[K]V, n).s = s[:0] only when retained backing arrays do not
pin excessive memory.strings.Builder for strings and bytes.Buffer for bytes when repeated
appends are measured. Do not copy a non-zero Builder; call Reset before
reuse.strconv.Append* or Format* over fmt.Sprintf in formatting hot
paths.-gcflags=-m=2 and -benchmem.sync.Pool is for temporary reusable objects that may be discarded at any
time. It provides no retention guarantee. Reset objects before Put, validate
state after Get, and benchmark pool contention plus retained capacity.
Use heap profiles to distinguish allocation rate from live heap. A large
alloc_space profile with a small inuse_space profile indicates churn, not a
leak. A goroutine retaining a reference can pin a large object graph.
Tune GOGC or GOMEMLIMIT only after allocation and GC profiles identify the
trade-off. GOMEMLIMIT is a soft runtime memory limit, not an RSS cap.
Invoke runtime-process-debugging for child processes, pipes, shutdown, or
scheduler/lifecycle stalls.
Use bufio.Reader/Writer for repeated small operations and batch small writes.
Reuse byte buffers where ownership is clear. Preserve partial-write handling,
flush errors, EOF, deadlines, cancellation, and ordering.
encoding/json, reflection, and struct-tag processing can be hot in
serialization-heavy programs. Profile before adding generated codecs or a
dependency; include compatibility, escaping, number handling, and error behavior
in comparisons.
Use a representative CPU profile as default.pgo and compare:
go build -pgo=auto ./...
PGO can alter inlining and devirtualization, so re-run benchmarks and binary size checks on the deployment target.
Before unsafe, assembly, or architecture-specific code, confirm bounds checks,
dispatch, cache misses, or instructions are material. The low-level version
must outperform safe Go and document its invariant and portability.
Pin Go and GOMAXPROCS. Use focused benchmarks and allocation assertions for
stable invariants. Keep wall time on shared runners advisory unless variance is
controlled.
go-conventions takes precedence for correctness and API design.code-review checks a completed diff. When invoked from code-review, do not
invoke it again.code-simplifier runs after the gain is proven.runtime-process-debugging owns process and lifecycle stalls.Correctness overrides performance.