Study escape and inlining diagnostics
Study escape and inlining diagnostics
Inlining substitutes a function body at a call site when profitable, exposing further optimizations. Escape analysis determines which values may outlive their current frame or must otherwise be stored on the heap. The analyses interact: an inlined call can make a previously escaping value local. Use go build -gcflags=-m=2 -o demo main.go to inspect compiler reasoning, not to promise exact allocation counts. Measure allocation behavior with benchmarks when performance matters.
package main
import ( "fmt")
//go:noinlinefunc makeNumber(v int) *int { return &v }func plusOne(n int) int { return n + 1 }func main() { p := makeNumber(8) fmt.Println(plusOne(*p))}The noinline directive intentionally preserves the call boundary. In Go 1.23 diagnostics report that v moves to the heap and plusOne can be inlined. Returning a pointer is safe in Go; lifetime is managed automatically. Do not remove safe pointers solely because a diagnostic says escape. Printing via fmt can introduce additional escapes through interface conversion.
Expected output:
9Compare an ordinary local pointer
package main
import ( "fmt")
//go:noinlinefunc local(n int) int { p := &n; return *p + 2 }func main() { fmt.Println(local(8))}Taking an address alone does not force heap allocation. Here the pointer need not outlive local. Compare -m=2 and -m=2 -l (pass both within one quoted gcflags value) on the same source. Disabling inlining can change allocation behavior and performance, so it is a diagnostic configuration, not a recommended deployment default. Absence of an escape message is not a complete allocation proof.
Expected output:
10Try it locally
Write a noinline function newPair returning a pointer to a two-int array, initialized to 3 and 7. Print the sum, then inspect escape diagnostics. Explain why the caller can still read the array after the function returns.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Compiler pipeline 05: Study escape and inlining diagnostics
Check the lesson objective before moving on.
Does taking an address always imply a heap allocation?
What is a useful interpretation of -m diagnostics?