Skip to main content
skill CX Codex

go-performance-review — Claude Code skill for Go performance

Find Go performance bottlenecks and check proposed changes with benchmarks and profiles. Use it to review hot paths or reduce allocations.

Install into Claude Code · Skills CLI (Vercel) All methods and platforms ↓
npx skills add eduardo-sl/go-agent-skills --skill go-performance-review -a claude-code
Open on GitHub

What go-performance-review does

The skill reviews Go code for costs in hot paths, including excess allocations, map usage, and HTTP client reuse. Its optimization workflow starts with a target metric and a baseline benchmark, then uses profiling to locate the cause. Change one thing at a time and compare results with benchstat.

Its guidance covers pprof, execution traces, GC, GOMEMLIMIT, and PGO; detailed references are loaded when needed. The Go toolchain is required. benchstat is optional and helps compare benchmark runs. The skill advises against optimizing without a benchmark that demonstrates the problem. It does not handle concurrency correctness or general style reviews.

What it helps with

  • find CPU-heavy functions in a Go package
  • reduce allocations on a hot path
  • compare benchmark runs before and after a change
  • trace memory growth with heap profiles
  • review GC and GOMEMLIMIT settings

Example prompts for the agent

  • Review this Go package's hot paths and find excess allocations.
  • Compare the benchmark runs before and after the change with benchstat.
  • Inspect the CPU profile and identify what to investigate first.

How to install go-performance-review

1Skills CLI (Vercel)

npx skills add eduardo-sl/go-agent-skills --skill go-performance-review -a claude-code

2Manually: put the files in place

~/.claude/skills/go-performance-review/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/go-performance-review/

Step by step, with commands for each platform: How to install skills in Claude Code: five ways

Questions about go-performance-review

How do I install go-performance-review in Claude Code?

Run npx skills add eduardo-sl/go-agent-skills --skill go-performance-review -a claude-code. The skill file is at ~/.claude/skills/go-performance-review/SKILL.md.

What does go-performance-review do?

It looks for Go performance bottlenecks and allocation costs, then guides the agent to validate changes with profiles and benchmarks.

What does go-performance-review need to run?

It requires the Go toolchain. benchstat is optional and is used to compare benchmark results.

Source text

SKILL.md · MIT
---
name: go-performance-review
description: >
  Detect performance anti-patterns and apply optimization techniques in Go.
  Covers allocations, benchmarks and benchstat, pprof and traces, GC and
  GOMEMLIMIT tuning, PGO, and HTTP client reuse. Use when checking performance,
  finding slow code, reducing allocations, profiling, or reviewing hot
  paths. Trigger examples: "check performance", "find slow code", "reduce
  allocations", "benchmark this", "profile", "optimize Go code".
  Not for: concurrency correctness (go-concurrency-review), general style
  (go-coding-standards).
user-invocable: true
license: MIT
compatibility: Designed for Claude Code or similar AI coding agents working on Go projects. Requires the Go toolchain. benchstat is optional, for comparing benchmark runs.
allowed-tools: Read Edit Write Glob Grep Bash(go:*) Bash(gofmt:*) Bash(benchstat:*)
metadata:
  author: eduardo-sl
  version: "1.6.0"
---

# Go Performance Review

Profile first, optimize second. Never optimize without a benchmark proving the problem.

Detailed reference material, loaded on demand:

- `references/benchmarking.md` — benchmark shape, running, reading
  benchstat, comparing variants, commit format, CI regression checks.
- `references/memory-and-io.md` — allocation, hot-path and map examples,
  retention, struct layout, false sharing, HTTP clients, streaming, batching.
- `references/profiling.md` — which profile for which symptom, capture
  commands, reading pprof, compiler diagnostics, traces, flight recorder.
- `references/runtime-tuning.md` — GOGC/GOMEMLIMIT, reading `gctrace`,
  GOMAXPROCS in containers, PGO.
- `references/logging.md` — slog vs zap/zerolog decision table, slog with a
  zap backend, logging anti-patterns in hot paths.

Read a reference file only when the summary below is not enough.

## Operating Modes

- **Architecture review** (a package or service): look for structural
  costs — no connection reuse, unbounded goroutines, N+1 queries, the wrong
  data structure, per-request work that could be done once. Run separate
  passes for allocation and layout, I/O and concurrency, and algorithms and
  caching; give each to a parallel sub-agent if the environment supports it.
- **Hot-path review** (one function the caller names): read it alongside
  its profile, sequentially.
- **Optimize** (a bottleneck is known): follow the cycle below, one change
  at a time.

## The Optimization Cycle

1. **Name the metric and the target** — p99 latency, throughput,
   `allocs/op`, RSS. Without a target, optimization never ends.
2. **Rule out external time first.** Goroutines parked in
   `net.(*conn).Read` or `database/sql`, or a trace dominated by one
   upstream span, mean the fix is elsewhere (go-database, go-observability).
3. **Benchmark the isolated path** and save the baseline:
   `go test -run '^$' -bench Name -benchmem -count=10 | tee old.txt`.
4. **Profile to find the cause** — see Profiling below.
5. **Change one thing**, with a comment saying why it is faster.
6. **Re-measure on the same machine and toolchain**; keep the change only
   if `benchstat old.txt new.txt` shows a significant difference.
7. **Put the benchstat table in the commit body** (`perf(scope): ...`).

## 1. Allocation Reduction

- `strconv` instead of `fmt.Sprintf` for single values.
- `strings.Builder` (with `Grow` when the size is known) instead of `+=`
  in loops.
- Preallocate slices and maps when the size is known or bounded:
  `make([]T, 0, n)`, `make(map[K]V, n)`.
- Reuse buffers on a proven hot path with `sync.Pool` — reset on get,
  size-cap on put (the concurrency skill has the pattern).
- Watch for hidden allocations: values stored in interfaces, closures
  capturing variables, `[]byte(s)`/`string(b)` conversions, and anything
  `go build -gcflags=-m` reports as `escapes to heap`.

## 2. Hot Path Optimizations

- Concrete types in tight loops — interface method calls block inlining
  and boxing allocates.
- No `reflect` on hot paths; generate code instead (`stringer`, protobuf).
- Contiguous data beats pointer chasing: `[]Point` over `[]*Point`, and a
  struct of slices when a loop touches one field of many records.

## 3. Map Performance

- Capacity hints when the size is known.
- `sync.Map` only for its two documented workloads; otherwise a map
  guarded by a mutex.
- A small fixed key set (enum-indexed) is faster as an array or slice.

Examples for sections 1-3: `references/memory-and-io.md`.

## 4. Benchmarking

```go
func BenchmarkParse(b *testing.B) {
    input := makeInput(4 << 10) // setup runs once, outside the timed loop
    b.ReportAllocs()
    for b.Loop() {              // Go 1.24+: no ResetTimer, no sink variable
        Parse(input)
    }
}
```

- `-count=10` and `benchstat`, never a single run. `~` in its output means
  "no change", whatever the percentage says.
- Sub-benchmarks per input size; inputs sized like production.
- Below Go 1.24, use `for range b.N` with `b.ResetTimer()` after setup and
  a package-level sink.
- Competing variants are built in parallel but benchmarked one at a time —
  concurrent runs contaminate each other.

Commands, reading guide and CI regression checks: `references/benchmarking.md`.

## 5. Profiling

| Symptom | First profile |
|---|---|
| High CPU | CPU profile — high `flat` functions |
| GC busy, high allocation rate | heap with `-sample_index=alloc_objects` |
| Memory grows over time | two heap snapshots, `go tool pprof -base` |
| Latency high, CPU idle | goroutine dump, block/mutex profiles, a trace |
| Lock contention | mutex profile (`runtime.SetMutexProfileFraction`) |
| Scheduling delays, GC pauses | execution trace; flight recorder (Go 1.25+) in production |

```bash
go test -run '^$' -bench Name -cpuprofile cpu.out -memprofile mem.out ./pkg
go tool pprof -http=:0 cpu.out                     # flame graph
go build -gcflags='-m' ./pkg 2>&1 | grep 'escapes'  # what moves to the heap
```

The CPU profile only sees running goroutines: when latency is high and CPU
is low, the time is waiting, and only the goroutine, block and trace views
show it. Commands, reading guide, escape analysis and the flight recorder:
`references/profiling.md`.

Importing `net/http/pprof` registers its handlers on `http.DefaultServeMux`.
If the service also serves public traffic from `DefaultServeMux`, profiles,
command line and goroutine stacks become world-readable. Always give pprof
its own loopback or internal-network listener.

## 6. Runtime Tuning

- In containers, set `GOMEMLIMIT` to ~80-90% of the memory limit and keep
  `GOGC`; the GC then works harder near the ceiling instead of the process
  being OOM-killed. Never `GOGC=off` with only a limit.
- Go 1.25+ derives `GOMAXPROCS` from the cgroup CPU limit — remove
  `automaxprocs` and don't set it by hand without a measurement.
- Commit a production CPU profile as `default.pgo` for PGO (Go 1.21+).
- After a toolchain upgrade (Green Tea GC in 1.26, the 1.27 allocator),
  re-measure before keeping old tuning.

Detail, `gctrace` reading and `runtime/metrics`: `references/runtime-tuning.md`.

## 7. I/O and HTTP Clients

- One `http.Client` per process, with a `Timeout` and a transport cloned
  from `http.DefaultTransport` with `MaxIdleConnsPerHost` raised — the
  default of 2 churns connections under concurrency.
- Close every response body and read it to EOF so the connection returns
  to the pool.
- Stream: decode from `r.Body` with a size cap instead of `io.ReadAll`;
  `io.Copy` for proxying; `bufio` for many small writes.
- Batch round trips — rows, API calls, channel items.
- Subslices and substrings of large buffers keep the whole buffer alive;
  `slices.Clone`/`strings.Clone` what you keep.

Examples, struct layout and false sharing: `references/memory-and-io.md`.

## 8. High-Throughput Logging

`log/slog` is the default. Switch only when a benchmark shows logging in
the profile, and then keep the slog API with a faster backend (zap's
`zapslog` handler) so callers never change. In hot loops, log progress
every N items or a summary after the loop — never once per item.
Decision table and examples: `references/logging.md`.

## 9. Common Anti-Patterns

| Anti-Pattern | Fix |
|---|---|
| `fmt.Sprintf` for simple int→string | `strconv.Itoa` |
| String concatenation in loop | `strings.Builder` |
| Slice without preallocation | `make([]T, 0, n)` |
| Map without capacity hint | `make(map[K]V, n)` |
| `regexp.Compile` inside function | Compile once at package level |
| `json.Marshal` in hot path | Profile first: since Go 1.27 `encoding/json` runs on the faster v2 engine; code-gen (`easyjson`) only if JSON still dominates |
| Logging in tight loop | Batch or sample |
| `defer` in very tight inner loop | Manual cleanup (rare, benchmark first) |
| `http.Get` / default client in production | Shared client with `Timeout` and tuned transport |
| `io.ReadAll` on request or response bodies | Stream with a decoder and a size cap |
| `reflect.DeepEqual` outside tests | `slices.Equal`, `maps.Equal`, `bytes.Equal` |
| `panic`/`recover` as control flow | Return errors — a panic captures a stack trace |

## Important Caveat

Most Go code is not performance-critical. Readability and correctness ALWAYS
take priority over micro-optimizations. Only apply these patterns when:

1. A benchmark proves this code path is a bottleneck
2. The optimization is significant (>10% improvement)
3. The resulting code remains readable and maintainable

Premature optimization is still the root of all evil, even in Go.

© eduardo-sl · MIT · eduardo-sl/go-agent-skills

Similar skills

All skills for Code review and refactoring →

From the same set34

go-test-table-driven Guides coding agents when writing and refactoring Go table-driven tests. It enforces clean test structs, clear subtest names, and lean loop bodies. CX Codex +1 ★ 83repo stars go-test-quality Get guidance on Go test design, coverage checks, and concurrency tests. Use it when adding tests, reviewing existing ones, or investigating flaky behavior. CX Codex +1 ★ 83repo stars go-troubleshooting A skill that walks the agent through Go runtime diagnosis: panics, deadlocks, goroutine and memory leaks, race reports. Useful when a program crashes or hangs and the cause is unclear. CX Codex +1 ★ 83repo stars go-security-audit Audits Go code for OWASP Top 10 issues, injections, SSRF, and authentication flaws. Runs read-only checks across your repository or pull request diffs. CX Codex +1 ★ 83repo stars go-observability A skill that sets conventions for Go service telemetry: structured logs with log/slog, OpenTelemetry traces and metrics, SLO alerts. Useful once a service runs in production and you need to see what it does. CX Codex +1 ★ 83repo stars go-defensive-coding Stops panics, slice aliasing, and subtle runtime bugs in Go: typed-nil interfaces, numeric truncation, and loop defer hazards. CX Codex +1 ★ 83repo stars go-concurrency-review Review Go concurrency or build safer concurrent code with checks for goroutine lifecycles, channels, shared state, and context cancellation. CX Codex +1 ★ 83repo stars go-naming Applies Go naming conventions from Effective Go to packages, methods, acronyms, and errors. Helps prevent stuttering names like order.OrderService. CX Codex +1 ★ 83repo stars

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor