Performance optimization improves speed or efficiency while preserving the required behavior. Optimize a demonstrated bottleneck rather than working blindly: correct, clear code is usually preferable to faster code that is difficult to understand. Optimization can also target goals such as floating-point accuracy, not just speed. section 12.0 section 1.3.6
In Go, reducing allocations is one way to improve performance. Depending on the code, options include using strings.Builder for concatenation, avoiding unnecessary byte-to-string conversions, preallocating slices and maps when their lengths are known, and reducing struct size through alignment. Changing an API or using sync.Pool are other approaches. section 12.6
Similar-looking expressions can produce different results after compilation. A map cannot use a slice as a key, so a lookup using []byte data must express the key as a string. When string(bytes) appears directly in the lookup, the Go compiler can avoid the conversion that occurs when the string is assigned to a separate variable first. section 12.6.2
func (c *cache) get(bytes []byte) (v int, contains bool) {
v, contains = c.m[string(bytes)] #1
return
}
The direct conversion in the map lookup enables the compiler optimization. section 12.6.2
Inlining replaces a function call with the called function’s body. It removes call overhead and can enable further optimizations, such as keeping a value on the stack instead of letting it escape to the heap. Functions must fit an inlining complexity budget; inspect the compiler’s decisions with go build -gcflags "-m=2". section 12.7
$ go build -gcflags "-m=2"
./main.go:10:6: can inline sum with cost 4 as:
func(int, int) int { return a + b }
...
./main.go:6:10: inlining call to sum func(int, int) int { return a + b }
Compiler diagnostics report both the inlining cost and the call that was inlined. section 12.7
For a frequently used fast path, separating a more complex slow path can make the calling function eligible for inlining. In the mutex example, moving the locked case into lockSlow leaves a smaller Lock method. section 12.7
func (m *Mutex) Lock() {
if atomic.CompareAndSwapInt32(&m.state, 0, mutexLocked) {
if race.Enabled {
race.Acquire(unsafe.Pointer(m))
}
return
}
m.lockSlow() #1
}
func (m *Mutex) lockSlow() {
var waitStartTime int64
starving := false
awoke := false
iter := 0
old := m.state
for {
// ...
}
if race.Enabled {
race.Acquire(unsafe.Pointer(m))
}
}
The refactored Lock method keeps the fast path separate from the slow-path function. section 12.7
Compiler optimizations can also invalidate a benchmark. If a population-count result is discarded, the compiler can inline the call and remove its side-effect-free work, leaving an effectively empty loop. A surprisingly low measurement may therefore time no useful work. section 11.8.3
var global uint64 #1
func BenchmarkPopcnt2(b *testing.B) {
var v uint64 #2
for i := 0; i < b.N; i++ {
v = popcnt(uint64(i)) #3
}
global = v #4
}
Keep each result in a local variable, then assign the final result to a global variable after the loop. This preserves the work while limiting global-write overhead inside the loop. section 11.8.3
Compiler behavior can change between Go releases, so check optimization assumptions and benchmark the code that matters. section 12.6.2 section 11.8.3









