In 2019, I prepared a lecture on simple ways to optimize Go programs, based on an article by Stephen Whitworth. Looking back at my notes, the tools and the language implementation have changed, but the central question remains: where does a program spend time and memory, and is that worth changing?
Lecture material in Russian · Stephen Whitworth’s “Simple techniques to optimise Go programs” in the archive.
Measure first
Start with a reproducible measurement: choose representative data, write a benchmark, and save the baseline. go test -bench=. -benchmem reports time per operation and allocation statistics. A CPU or memory profile helps locate the code actually consuming resources. After a change, run the same test and compare the results.
A microbenchmark answers a narrow question. Even a substantial speedup in a small function may have little effect on a request dominated by network or database latency. Check the result at the application level too.
Slice capacity and building strings
When you have an estimate of the number of elements, you can specify a slice’s capacity. This gives append room without repeatedly growing the backing array:
ids := make([]string, 0, len(users))
for _, user := range users {
ids = append(ids, user.ID)
}
The slice starts with length zero and capacity equal to the number of users. This is a useful hint for a particular task, not a reason to reserve large amounts of memory without measurement.
For a string assembled from many pieces, try strings.Builder. It writes the pieces into a buffer; when the approximate final size is known, Grow can reserve space ahead of time. For a few strings, ordinary + is often clearer. Measure the difference in your own code.
Conversions and buffers
For converting an integer to a string, strconv.Itoa expresses the task more precisely than the general-purpose fmt.Sprintf. In a hot path it may reduce overhead, though the benefit depends on the context and Go version.
Some standard-library functions append to a supplied []byte, including strconv.AppendInt and time.AppendFormat. If an application is already building a response in a buffer, this interface can let it reuse that buffer and control allocations. Keep track of who owns the buffer and when it is safe to change it again.
Serialization and code generation
The lecture also covered JSON serialization. The standard encoding/json package uses reflection for ordinary structs, while a generator such as easyjson can prepare specialized code ahead of time. This is worth investigating if a profile shows that serialization takes a significant share of runtime.
Generated code comes with a maintenance cost: it needs to be updated when a struct changes. Checking generation in CI helps catch stale code. Test compatibility and performance against your own data and current Go version.
Pools and garbage collection
sync.Pool allows objects to be reused temporarily across independent operations and can help when allocations are frequent. It does not guarantee that an object will remain in the pool: the runtime may remove it at any time. Reset an object’s state before returning it, especially data from a previous request, so the next caller cannot see someone else’s data. A pool adds complexity of its own, so introduce it only after measuring.
The 2019 article also discusses the cost of scanning large pointer-containing structures during garbage collection. Data representation does affect GC work, but replacing string keys with hashes requires handling collisions and can change program behavior. Make that decision with a profile and test it under a representative workload.
Performance belongs to the whole program and its environment. I would keep an optimization when measurement shows a useful effect and the added complexity is proportional to the benefit.