Performance tuning in Go starts with measurement, not guessing. Write a benchmark with the testing package, collect CPU and memory profiles with runtime/pprof or net/http/pprof, fix the biggest hot spot, and measure again. The standard library ships everything needed for that loop, and the Go toolchain adds escape analysis output and profile-guided optimization.
Most real gains come from fewer allocations, better algorithms and less lock contention, not from micro-tweaks. The sections below show the tools in the order you would use them.
A benchmark gives you a repeatable number. Since Go 1.24 you can use b.Loop(), which keeps the compiler from optimizing the work away and runs setup code only once.
Run it with go test -bench=. -benchmem. On older Go versions use for i := 0; i < b.N; i++ instead of b.Loop(). To compare two versions of your code statistically, save both outputs and use benchstat from golang.org/x/perf.
Benchmarks can write profiles directly:
For a running service, import net/http/pprof on an internal-only port. Never expose it publicly.
Then capture 30 seconds of CPU data with go tool pprof http://127.0.0.1:6060/debug/pprof/profile?seconds=30. Useful profiles at /debug/pprof/ include heap, allocs, goroutine, block and mutex (the last two must be enabled with runtime.SetBlockProfileRate and runtime.SetMutexProfileFraction).
For scheduling and latency problems use the execution tracer: go test -trace trace.out followed by go tool trace trace.out.
Allocation is often the real cost, because every heap object adds work for the garbage collector.
make([]T, 0, n), make(map[K]V, n).strings.Builder or bytes.Buffer instead of repeated string concatenation.sync.Pool for short-lived buffers on hot paths.go build -gcflags=-m.Always Reset a pooled object before use, and do not keep references to it after Put.
Two runtime settings matter. GOGC (default 100) controls how much the heap may grow between collections; raising it trades memory for less GC work. GOMEMLIMIT (Go 1.19+) sets a soft memory limit, which is useful in containers so the collector works harder before the process is killed for exceeding its memory cap.
Goroutines are cheap, but unbounded goroutines and shared locks are not.
| Problem | Typical fix |
|---|---|
| Too many goroutines | Worker pool or errgroup.SetLimit |
| Hot mutex | Shard the data, use sync.RWMutex for read-heavy loads, or atomic counters |
| Channel as a lock | Replace with sync.Mutex when only protecting state |
| Contended counter | atomic.Int64 instead of a mutex |
Run tests with go test -race to catch data races, but do not benchmark with the race detector on, since it slows programs several times over. In Go 1.25 and later, the default GOMAXPROCS respects container CPU limits on Linux; on older versions set it explicitly or use a library such as automaxprocs.
Profile-guided optimization (generally available since Go 1.21) feeds a real CPU profile back to the compiler, enabling better inlining and devirtualization. Put a representative profile named default.pgo in the main package directory and run go build; the toolchain picks it up automatically. Expect single-digit to low double-digit percentage gains on suitable workloads, and measure to confirm.
reflect, fmt.Sprintf or regex compilation inside hot loops; compile regexes once at package level.defer cost in extremely tight loops (it is cheap today, but still measurable).sync.Pool as a cache; the runtime may drop its contents at any collection.Capture a CPU profile with pprof, open it with go tool pprof -http, and look at the flame graph or the top command. Fix the largest cumulative cost first.
sync.Pool always make code faster?No. It helps when objects are expensive to allocate and used briefly in many goroutines. For rarely used objects it adds complexity without benefit, so benchmark first.
Only after profiling shows GC is a large share of CPU time. Prefer reducing allocations first; then adjust GOGC or GOMEMLIMIT with measurements.
Yes. Each release improves the runtime and compiler, and Go 1.24 introduced a faster built-in map implementation, so upgrading is often a free speedup.
Tune Go code with a loop of benchmark, profile, change, and re-measure. Use testing, pprof and the tracer from the standard library, cut allocations, bound concurrency, and consider GOMEMLIMIT and PGO for production services. Avoid optimizing anything you have not measured.