Category: Performance and measurement
One constructor, five call sites, two verdicts
`-gcflags=-m` printed "escapes to heap" for the constructor and "does not escape" for half the places that call it. The package boundary had nothing to do with it.

TL;DR
- `-gcflags=-m` prints one verdict for a function and a separate verdict for each place it is inlined. The same constructor got `escapes to heap` at its definition and `does not escape` at two of its four call sites.
- The call site in another package allocated nothing and cost 1.880 ns against the same-package 1.841 ns, indistinguishable at p=0.060. Export data carries the body across; the boundary is not a barrier.
- What decides it is inlinability. A `//go:noinline` copy allocated 32 B at a call site that drops the value immediately, because the caller could not see that it dies.
- The gap between the two is 3.7× and one allocation — 1.841 ns and 0 B against 6.877 ns and 32 B, from one constructor in one file.
- `-gcflags='-m -l'` collapses the five call-site verdicts into two per-function ones. Reading both is how you tell which kind you are looking at.
go build -gcflags=-m printed &T{...} escapes to heap for a constructor, and
&T{...} does not escape for two of the four places that call it.
Same constructor. Same build. The two lines are two apart in the same output, and the one people quote is the first.
What is being measured
All numbers: go1.27.0 darwin/arm64, Apple M4 Pro, 12 cores, macOS 26.6.2. The
harness is
callsite/.
One constructor, small enough that the compiler will consider keeping its result in a frame:
type T struct {
V int
buf [3]int
}
func New(v int) *T { return &T{V: v} }
Five call sites. Three in the same package — one that drops the value, one that
stores it in a package variable, and one that calls a //go:noinline copy of
the same constructor. Two more in another package, repeating the first two
verbatim.
The two drop sites are the same source compiled into two packages, which makes
them a control pair as well as a result:
their true difference is zero, so whatever separates them is the error bar for
everything else in the table.
Escape analysis is not a rule of thumb
established that -m reports escape analysis rather than allocation, and left a
claim standing in its FAQ: -m is per-function, allocation is per-call-site.
This is that claim measured.
What the compiler prints
$ go build -gcflags=-m ./callsite ./callsite/remote
# gopheria.lab/callsite
callsite/ctor.go:19:6: can inline New
callsite/ctor.go:32:6: can inline Drop
callsite/ctor.go:38:6: can inline Keep
callsite/ctor.go:46:6: can inline Sealed
callsite/ctor.go:33:10: inlining call to New
callsite/ctor.go:39:10: inlining call to New
callsite/ctor.go:19:29: &T{...} escapes to heap
callsite/ctor.go:26:35: &T{...} escapes to heap
callsite/ctor.go:33:10: &T{...} does not escape
callsite/ctor.go:39:10: &T{...} escapes to heap
# gopheria.lab/callsite/remote
callsite/remote/remote.go:11:6: can inline Drop
callsite/remote/remote.go:17:6: can inline Keep
callsite/remote/remote.go:12:19: inlining call to callsite.New
callsite/remote/remote.go:18:19: inlining call to callsite.New
callsite/remote/remote.go:12:19: &callsite.T{...} does not escape
callsite/remote/remote.go:18:19: &callsite.T{...} escapes to heap
Line 19 is New itself: escapes to heap. Line 33 is New inlined into a
caller that reads one field and drops the pointer: does not escape. Line 39
is New inlined into a caller that stores the pointer in a package variable:
escapes, correctly.
The can inline and inlining call to lines are the decision being made, and
they are why the verdicts multiply: each inlining call to New produces a fresh
copy of the constructor’s body inside a caller, and each copy gets analysed in
that caller’s context. Five lines of escape verdict for one composite literal,
because the compiler analysed it five times.
Four verdicts about one constructor, and they are not the same verdict. The
column numbers are the tell — 19:29 is the composite literal inside New,
33:10 and 39:10 are that same literal after it was folded into two different
callers.
Now the same build with inlining off:
$ go build -gcflags='-m -l' ./callsite ./callsite/remote
callsite/ctor.go:19:29: &T{...} escapes to heap
callsite/ctor.go:26:35: &T{...} escapes to heap
Two lines. Every call-site verdict is gone, because nothing was inlined into anything and the compiler had only the per-function question to answer. This is the output that matches the mental model most people have, and it describes a program nobody ships.
What it costs
$ go test -run=^$ -bench='Warmup|CallSite' -benchmem -count=10 ./callsite |
grep -v Warmup | benchstat -col /site -
│ same-package-drop │ same-package-keep │ same-package-sealed │ cross-package-drop │ cross-package-keep │
│ sec/op │ sec/op │ sec/op │ sec/op │ sec/op │
CallSite-12 1.841n ± 2% 6.746n ± 1% 6.877n ± 1% 1.880n ± 1% 6.636n ± 1%
+266.50% +273.62% ~ (p=0.060) +260.55%
│ B/op │ B/op │ B/op │ B/op │ B/op │
CallSite-12 0.00 ± 0% 32.00 ± 0% 32.00 ± 0% 0.00 ± 0% 32.00 ± 0%
3.7 times and one allocation, between two call sites of the same constructor
in the same file. The 32 bytes are what T costs on the heap; at that size the
allocation itself is
one of the cheap ones, and it is
still most of the 6.7 ns.
The boundary that is not a boundary
The two drop rows are the same code in two packages. Their delta is ~ at
p=0.060 — 1.841 ns against 1.880 ns, zero allocations either way.
That is the result I expected least. A package boundary feels like it should be
an analysis boundary: the compiler is compiling remote without callsite’s
source in front of it. But export data carries inlinable function bodies across,
so callsite.New is available to be folded into remote.Drop exactly as it is
into callsite.Drop, and the verdict follows the body rather than the file.
The keep rows say the same thing from the other side. same-package-keep and
cross-package-keep both store the pointer in a package variable, both allocate
32 bytes, and they land at 6.746 ns and 6.636 ns — the cross-package one
marginally faster, which is noise rather than a finding. When the value
genuinely escapes, the boundary does not make it escape harder.
Read the four rows together and the package column is empty. Two shapes of call site, each measured twice on either side of a boundary, and the boundary explains none of the variation.
The two drop rows also serve as this table’s control. They are the same source
compiled into two places, so their true difference is zero, and the 2% between
them is the error bar every other column has to clear. All three keep-shaped
columns clear it by a factor of a hundred.
What actually decides it
same-package-sealed is the row that names the mechanism. It calls a copy of
the constructor marked //go:noinline, from a call site that drops the value
immediately — the same shape as same-package-drop, which allocates nothing.
It allocates 32 bytes.
//go:noinline
func NewSealed(v int) *T { return &T{V: v} }
func Sealed(v int) int {
t := NewSealed(v)
return t.V // dies here, and allocates anyway
}
The compiler compiled NewSealed without knowing what any caller would do with
the result, so it had to assume the pointer outlives the frame. The caller, in
turn, could not see inside it. Neither half of the program had the information,
and the conservative answer is the only one available.
So the axis is not where the constructor lives. It is whether the caller could see into it — which makes the escape verdict downstream of the inlining decision, and the inlining decision is a budget the compiler keeps.
Reading both flags
The practical shape of this:
-m is what your program does. Every inlining call to line is followed by
a verdict about the inlined copy, and those verdicts are per call site. This is
the output to read when you want to know whether a particular caller allocates.
-m -l is what the function is. One verdict per function, no call sites,
and it is the conservative one — because with inlining off, every function has
to be compiled as if any caller might keep anything it returns.
Quoting the second while believing you have measured the first is how a
constructor acquires a reputation. The line that reads escapes to heap next to
a definition is true and says nothing about the four places that call it.
What this does not measure
Whether any of this matters in a real program. One allocation of 32 bytes is 5 ns; a handler doing it once per request will never notice, and one doing it a million times in a loop will. The profile that tells you which you have is a different tool and it comes first.
The constructor here is also the easiest possible case: a small struct, no
interfaces, no closures, no slices growing out of it. The verdicts get harder to
predict as soon as a value crosses an interface, and -m gets correspondingly
more useful and more confusing at the same time.
And this measures one Go version. Inlining budgets have moved between releases before, and a constructor that is inlinable today is one refactor — or one compiler change — away from being the sealed row instead.
Frequently asked
Does -gcflags=-m tell me whether my function allocates?
It tells you what the compiler decided about each expression it analysed, and it prints a line for the function's own body as well as one for every place that body was inlined. Those lines can disagree: in the runs below the constructor's own line says "escapes to heap" while two of its call sites say "does not escape". A verdict on the function is not a verdict on the callers.
Does calling a constructor from another package force a heap allocation?
No. Export data carries the function body across the boundary, so the compiler can inline it and reach the same conclusion it reaches in the same file. The cross-package call site below allocated nothing and its timing was statistically indistinguishable from the same-package one at p=0.060. What blocks the analysis is a callee the compiler cannot see through, which is a question about inlining rather than about packages.
Why does //go:noinline change whether something escapes?
Because the caller can no longer see that the returned pointer dies. Escape analysis is a whole-program-ish question answered per compilation unit, and when a constructor is inlined the compiler reasons about the value in the caller's frame. When it is not, the compiler compiled that constructor without knowing what any caller does with the result, so the conservative answer is the only available one.
What is the difference between -m and -m -l?
-l disables inlining, so the output is the per-function analysis with no call sites folded into anything. Running both is the only way to separate the two kinds of line: -m shows what happens to the program you ship, and -m -l shows the verdict the function carries on its own. In the file below they produce five lines and two.

