Skip to content
Tutorials Center
My learning

Topics

Lesson 125 of 147 ยท Advanced Go

Performance Tuning

Make Go programs faster the right way - measure first, then cut allocations with strings.Builder, preallocation, sync.Pool and fewer interface boxes. Includes inlining, bounds checks and PGO.

Go is fast by default. A straightforward Go program usually runs well within a small factor of C, compiles in seconds and uses little memory. That is why the first rule of performance work in Go is a rule about patience: do not optimize until you have measured. Most code is not on the hot path, and clever changes to code that does not matter make it harder to read for no gain.

When measurement does show a problem, it is very often an allocation problem. Every heap allocation costs time to perform and then more time for the garbage collector to clean up. This lesson gives you a workflow (measure, find, change, measure again), the handful of changes that fix most real-world slowness, and runnable programs that show the effect without depending on your machine's speed. The programs print allocation counts and direction ("A is faster than B"), not timings, because timings differ on every computer and every run.

Step one: measure

Guessing is unreliable. Use the tools in this order:

  1. A benchmark for the function in question (benchmarks and fuzzing). It tells you time per operation (ns/op), bytes allocated per operation (B/op) and allocations per operation (allocs/op).
  2. A profile of the real program (profiling and debugging): pprof shows where CPU time and allocations actually go, often somewhere you did not expect.
  3. A comparison between "before" and "after", ideally with several runs each and benchstat to tell real change from noise.

A benchmark lives in a _test.go file and runs with go test -bench . -benchmem. This program does not have go test here, but the testing package lets you run a benchmark directly, so you can see the API. It compares summing a slice with an index against building the sum through a function value (the exact numbers vary by machine, so we only print which is relatively cheaper and the allocations):

Go Running a benchmark with testing.Benchmark

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

b.Loop() (Go 1.24) is the modern way to write the benchmark loop: the framework decides how many iterations are needed, keeps setup code outside the timed region and prevents the compiler from optimizing the call away. In older code you will see for i := 0; i < b.N; i++ instead. The result struct tells us result.N, the iteration count the framework settled on; here the sum allocates nothing, so AllocsPerOp is 0. The output is true, 0, true and sum: 49995000.

When you only care about allocations, testing.AllocsPerRun(runs, f) is better than a timer: the answer is a stable number, the same on every machine, which makes it usable in a unit test (if allocs > 0 { t.Fatal(...) }) to stop a regression.

Allocations: where the time hides

A value goes on the stack (cheap, freed automatically when the function returns) or the heap (costs an allocation and later garbage collection). The compiler decides using escape analysis (stack and heap). You can ask it to explain with go build -gcflags=-m, which prints lines such as moved to heap: x or ... does not escape.

The best performance wins usually come from doing less allocation. The common patterns follow.

Build strings with strings.Builder

Strings are immutable (strings and runes), so s += "x" allocates a brand-new string and copies the old content every time. In a loop that is quadratic work. A strings.Builder appends into one growing buffer:

Go Concatenation versus strings.Builder

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Joining 100 pieces with += allocates a new string for every piece after the first, 99 in total. A plain Builder doubles its buffer as it fills, so it needs only a handful, and Grow with the final size needs exactly one. The same advice applies to bytes.Buffer and to strings.Join, which computes the total length first (the strings package, bytes and buffers).

Preallocate slices and maps

Appending to a slice that is too small makes it grow by allocating a bigger array and copying (slice internals). If you know, or can estimate, the final size, say so with make:

Go Preallocating capacity

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

The prealloc slice costs one allocation in total, while the growing one reallocates roughly every time its capacity doubles (about a dozen times for 1,000 elements). Maps behave similarly: make(map[K]V, n) sizes the table up front and avoids repeated rehashing. Preallocating is a good habit when the size is cheap to know. It is not worth much for tiny slices.

Reuse buffers with sync.Pool

If a function needs a temporary buffer on every call (formatting, encoding, reading), allocating a fresh one each time creates garbage at the rate of your request traffic. sync.Pool keeps a cache of reusable objects (the sync package):

Go Recycling buffers with sync.Pool

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

The pooled version allocates nothing per call after warm-up (the output shows 0) because the same buffer keeps coming back. Remember the caveats: the garbage collector may empty a pool at any time, so a pool is an optimization and never storage; reset the object before reuse so that old data cannot leak into the next user; and do not put huge buffers back, or the pool will hold on to memory. fmt, encoding/json and net/http use pools internally.

Avoid needless interface boxing

Storing a non-pointer value in an interface (any, error, fmt.Stringer) can force an allocation, because the interface needs a pointer to the data (interface internals). Tiny values (single bytes, small integers 0โ€“255, zero values) are special-cased, but a general int is not. This is why passing numbers to fmt.Println allocates, and why hot paths use strconv or Append functions instead:

Go Boxing costs, and a generic way around it

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

The //go:noinline comment keeps the compiler from inlining box into the benchmark, where it could prove the value never escapes and skip the allocation; in real code, the boxing happens whenever the value really escapes to the heap. The pattern behind the numbers: code that reaches any costs an allocation, while generics and concrete types do not. That does not mean you should avoid fmt. Avoid it in a loop that runs millions of times per second. Elsewhere, clarity wins.

Other slow patterns worth recognizing

Pattern Why it is slow Better
regexp.MustCompile inside a loop or handler Compiles the pattern on every call Compile once into a package-level variable (regexp)
Converting []byte and string repeatedly Each conversion copies Stay in one type; use bytes functions on bytes
Passing huge structs by value Copies on every call Pass a pointer; benchmark first (pointers and functions)
Searching a slice for membership in a loop O(n) per lookup Build a map or sort and use binary search (maps)
defer in a very hot inner loop Small cost per call (much smaller since Go 1.14) Fine usually; refactor only if the profile points at it
Unbuffered output, one syscall per Println Each write is a system call Wrap in bufio.Writer (os, io and bufio)
Goroutine per tiny task Scheduling and memory overhead A fixed worker pool (concurrency patterns)
Lock contention on one mutex Goroutines queue up Shard the data, use RWMutex or atomics (atomics)

Here is the first row with numbers you can verify. Compiling a regular expression is far more work than using one:

Go Compile once, match many times

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

The first line prints true true false. The precompiled version allocates nothing per match, while the inline compile allocates dozens of objects every time. go vet and linters flag this one automatically.

What the compiler does for you

You can write simple code and let the compiler help. Three optimizations to know about, all inspectable with compiler flags on your own machine:

  • Inlining. Small functions are copied into their callers, which removes call overhead and enables more optimization. go build -gcflags=-m prints can inline add and inlining call to add. Functions with loops, defer, recover or big bodies are limited; keep hot helpers small.
  • Escape analysis. -gcflags=-m also prints x escapes to heap and moved to heap, so you can see which allocations are avoidable.
  • Bounds-check elimination. Every s[i] is checked against len(s). When the compiler can prove the index is in range, it removes the check. Patterns that help: for i := range s, or a single early check such as _ = s[3] before touching s[0] to s[3]. Find remaining checks with -gcflags=-d=ssa/check_bce/debug=1. Do not contort code for this unless a profile points at it.
Go
// One check up front lets the compiler drop the four checks below it.
func parse(b []byte) uint32 {
	_ = b[3]
	return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
}

Profile-guided optimization (PGO)

Since Go 1.21, the compiler can use a real CPU profile to optimize the code that actually runs: it inlines hot calls more aggressively and, for interface calls that are mostly one concrete type, adds a fast path (devirtualization). Typical gains are a few percent up to about 10%, for free. The workflow, read-only because it needs a real program:

Terminal
# 1. Collect a CPU profile from production (net/http/pprof) or a representative run
curl -o cpu.pprof "http://localhost:6060/debug/pprof/profile?seconds=30"

# 2. Save it as default.pgo in the main package directory
mv cpu.pprof default.pgo

# 3. Build normally; -pgo=auto is the default and finds default.pgo
go build -o app .

Commit default.pgo to version control, refresh it now and then, and compare the before and after with benchmarks. The build gets slower with a profile; the program gets a bit faster.

A performance checklist

  1. Define the goal ("p99 latency under 50 ms", "process one million lines per second"). Without a target you cannot tell when to stop.
  2. Benchmark or profile to find the hot spot. Optimize that, not what looks suspicious.
  3. Reduce allocations first (-benchmem, AllocsPerRun, heap profile).
  4. Choose better algorithms and data structures before micro-tuning: an O(n log n) sort beats a perfectly tuned O(nยฒ) one.
  5. Reduce work: cache results, batch I/O, avoid repeated parsing.
  6. Add concurrency only when work is genuinely parallel and the profile shows idle cores.
  7. Measure again. Keep the change only if the numbers moved, and keep the code readable.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why does s += piece inside a loop get slow for many pieces?

    Strings are immutable, so every += builds a fresh string. The total copying grows quadratically. strings.Builder appends into one buffer.
  2. 2. What does make([]int, 0, 1000) buy you before appending 1000 elements?

    The length is 0, but the capacity is 1000, so the backing array is allocated once and append fills it in place.
  3. 3. Which statement about sync.Pool is true?

    A pool is an optimization for temporary objects. You cannot rely on what it still holds, and stale data in a reused object is a classic bug.
  4. 4. What is the best first step when a Go service is slower than you want?

    Intuition about hot spots is often wrong. Measure, change one thing, measure again.
  5. 5. Why is testing.AllocsPerRun handy in unit tests?

    Allocation counts are deterministic for most code, so a test can assert that a function allocates nothing without being flaky on slow CI machines.

Summary

  • Measure first: benchmarks (b.Loop, -benchmem), profiles (pprof) and before/after comparisons. Optimize only what the numbers point at.
  • Allocations are the usual cost. testing.AllocsPerRun gives deterministic counts you can assert in tests.
  • Use strings.Builder (with Grow) instead of +=, preallocate slices and maps with make, reuse temporary buffers with sync.Pool, and avoid hot-path boxing into any.
  • Compile regular expressions once, avoid repeated conversions, use maps for lookups and buffer your I/O.
  • The compiler inlines small functions, removes some bounds checks and keeps values on the stack when they do not escape; -gcflags=-m shows its decisions.
  • PGO (default.pgo, Go 1.21+) feeds a real profile back into the compiler for a few percent for free.

Next: Design Patterns and Idioms in Go shows how classic patterns look when you write them the Go way.