Evaluate escape analysis and value copying
Evaluate escape analysis and value copying
Escape analysis is the compiler’s decision about whether storage can stay local to a call or must outlive it. Returning a pointer is not automatically a heap allocation at every call site: inlining and other optimizations matter. Value parameters have copy semantics, but the compiler may avoid physical copies. Inspect diagnostics and benchmark equivalent behavior rather than choosing pointer parameters by intuition.
Create the files shown below in a fresh directory, then run go test -count=1 -v ./... there. The separator lines are file labels, not source code. Go 1.23 is used for these local examples.
--- go.mod ---module example.test/lesson
go 1.23
--- lesson_test.go ---package lesson
import ( "testing")
var saved *int
func local(n int) *int { return &n }func TestLesson(t *testing.T) { p := local(7) q := local(8) if *p != 7 || *q != 8 { t.Fatal("lifetime") }}func BenchmarkEscape(b *testing.B) { for i := 0; i < b.N; i++ { saved = local(i) }}Run go test -run=^$ -bench=. -benchmem and go test -run=^$ -gcflags=-m=2 (diagnostics on stderr). The global assignment makes the pointer survive the call. A diagnostic such as “moved to heap” applies to this compilation, not a language guarantee. A non-escaping pointer can still have safe lifetime semantics.
Expected result (timings and formatting may vary; essential text shown):
--- PASS: TestLessonCompare a value and pointer parameter
--- go.mod ---module example.test/lesson
go 1.23
--- lesson_test.go ---package lesson
import ( "testing")
type Block [256]uint64
var total uint64
//go:noinlinefunc value(x Block) uint64 { return x[0] + x[255] }
//go:noinlinefunc pointer(x *Block) uint64 { return x[0] + x[255] }func TestLesson(t *testing.T) { x := Block{0: 2, 255: 3} if value(x) != 5 || pointer(&x) != 5 { t.Fatal("sum") }}func BenchmarkValue(b *testing.B) { x := Block{0: 2, 255: 3} b.ResetTimer() for i := 0; i < b.N; i++ { total = value(x) }}func BenchmarkPointer(b *testing.B) { x := Block{0: 2, 255: 3} b.ResetTimer() for i := 0; i < b.N; i++ { total = pointer(&x) }}The noinline directive is a Go compiler-specific experimental control, not a portable language feature or general recommendation. It reduces inlining differences in this lab. A pointer can introduce aliasing and shared mutation; a value provides independent array contents. A slice value copies a descriptor, not its backing array. Do not extrapolate this artificial array benchmark to small structs or production paths.
Expected result (timings and formatting may vary; essential text shown):
--- PASS: TestLessonTry it locally
Compare an increment function on an array value and pointer; assert that only the pointer version mutates the caller. Use a fresh module with the same go.mod shown above; run go test -count=1 -v ./...
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Performance engineering 03: Evaluate escape analysis and value copying
Check the lesson objective before moving on.
Does returning &local always force heap allocation at every call site?
What does passing a slice by value copy?