Code-Memo

Memory, GC, and performance

Go uses a non-generational concurrent garbage collector. Most tuning is “write less garbage” and “measure with pprof”.

Stack vs heap

The compiler escapes variables to the heap when their lifetime cannot be proven stack-safe (e.g. returned pointers to local variables, captured by closures). Not every new or &T escapes — trust go build -gcflags=-m escape analysis logs when optimizing hot paths.

Slices and maps
strings.Builder

Efficient string assembly.

var b strings.Builder
for _, s := range parts {
	b.WriteString(s)
}
return b.String()
sync.Pool

Reuse temporary allocations under strict lifetimes — objects may be cleared at any GC; never assume pooled values persist.

CPU profiling
go test -cpuprofile=cpu.prof
go tool pprof cpu.prof
Memory profiling
go test -memprofile=mem.prof
Benchmarks
func BenchmarkFoo(b *testing.B) {
	for i := 0; i < b.N; i++ {
		Foo()
	}
}
Compiler optimizations

Inlining, bounds check elimination, and devirtualization happen automatically; keep hot loops simple and allocation-free when possible.