Category: Performance and measurement

The inlining budget is 80, and one call spends 59

The compiler prints what every construct costs. A make and a map index cost 2 each; a call costs 59; defer and go are not priced at all — they disqualify.

Compiler operations branch through Go keywords into a blank central block, leading to an inline or cannot-inline decision node.

TL;DR

  1. The compiler prints the number. `go build -gcflags=-m` emits `can inline F with cost N` for everything it accepted, and the budget it is measured against is 80 on go1.27.0.
  2. The prices are not the guessable ones. `make([]int, n)` costs 2 and a map index costs 2 — the same as `n + 1`. A single call to a function the compiler cannot see through costs 59.
  3. `defer` and `go` have no price. They produce `cannot inline: unhandled op DEFER` and `unhandled op GO`, a rejection that happens before the accounting does.
  4. A ladder of forty functions prices one statement at exactly 3. Grow25 costs 77, Grow26 costs 80 and is accepted, Grow27 costs 83 and is refused.
  5. Crossing that boundary cost nothing on a leaf computation — 1.774 ns against 1.760 ns, inside a control pair. Crossing it with a constructor cost 337% and one allocation, because the caller could no longer see that the value dies.

make([]int, n) costs 2 against the compiler’s inlining budget. So does a map index. A single call to a function the compiler cannot see through costs 59, out of a budget of 80.

The compiler has been printing these numbers all along, and almost nobody reads them.

Asking the compiler

All numbers: go1.27.0 darwin/arm64, Apple M4 Pro, 12 cores, macOS 26.6.2. The harness is inlinebudget/.

Two flags, two halves of the answer:

go build -gcflags=-m ./inlinebudget       | grep 'can inline'
go build -gcflags='-m -m' ./inlinebudget  | grep 'cannot inline'

The first prints a cost for everything the compiler accepted. The second says why it refused the rest:

inlinebudget/cost.go:47:6: can inline WithMake with cost 4
inlinebudget/grow.go:...: cannot inline Grow27: function too complex: cost 83 exceeds budget 80

80 is the budget on this toolchain. It has moved between releases before and will again, which is why every table below carries the version.

The benchmarks further down each carry a control pair — a second row for a function measured against itself — because one of the two results is a null and a null is only worth publishing next to the error bar that produced it.

The price list

Sixteen functions, each one a baseline body plus exactly one construct, so subtracting the baseline’s cost gives that construct’s price with nothing else in the way:

construct              cost   over baseline
Base (reference)          2              —
n + 1                     4             +2
make([]int, n)            4             +2
m[n]  (map index)         4             +2
append(xs, n)             8             +6
if n > 0 { … }            8             +6
panic("…")                9             +7
for _, x := range xs      11            +9
switch n { … }            12           +10
select { … }              14           +12
switch v := v.(type)      15           +13
for i := 0; i < 4; i++    17           +15
f := func() int { … }     27           +25
opaque(n)  (a call)       61           +59

The bottom row is the finding. A single call to a function the compiler cannot inline costs 59 of the 80. One such call and a couple of statements is the whole budget; two of them cannot fit however small the rest of the function is. That is the real content of “keep functions small” — not the line count, the call count.

The top rows are the ones that surprised me. make allocates a slice and costs the same as an addition. A map index costs the same as an addition. Neither price reflects the work at runtime, because the budget is not about runtime work — it is about how much the inliner would have to paste into the caller.

A closure at 25 is the priciest thing that is not a call, and a for loop at 15 costs nearly twice a range at 9, which I would have guessed the other way round.

The two that have no price

defer and go are missing from that table, and their absence is the point:

inlinebudget/cost.go:75:6:  cannot inline WithDefer: unhandled op DEFER
inlinebudget/cost.go:101:6: cannot inline WithGo: unhandled op GO

No cost is computed. A function containing either is refused before the accounting starts, so shrinking it does not help and never will. This is qualitatively different from being over budget, and the compiler says so in different words — unhandled op against function too complex.

If you have ever wondered why a two-line function with a defer in it did not get inlined, that is the whole answer.

Where the edge is

A ladder of forty functions, each one n += k longer than the last, walks the budget to its edge:

can inline Grow25 with cost 77
can inline Grow26 with cost 80
cannot inline Grow27: function too complex: cost 83 exceeds budget 80

Three per statement, exactly, and the boundary is a hard >: cost 80 is accepted, cost 83 is not. There is nothing gradual about it.

Crossing the edge costs nothing

So what does a program lose when a function tips over? For this ladder, nothing at all:

$ go test -run=^$ -bench='Warmup|Boundary|Direct' -benchmem -count=10 ./inlinebudget |
    grep -v Warmup | benchstat -col /fn -

            │ grow26-inlined │ grow26-control │ grow27-not-inlined │
Boundary-12      1.774n ± 0%      1.767n ± 0%         1.760n ± 1%
Direct-12        1.671n ± 1%      1.663n ± 0%         1.679n ± 1%

grow26-control is a second row for the same function, so its true delta is zero — the control pair that decides whether anything else on this line is a result. It came in 0.39% below its own twin. grow27-not-inlined came in 0.79% below, which is inside that.

The not-inlined function is marginally faster, and both readings are noise.

The two rows are two call shapes. Boundary reaches the functions through a func(int) int value, which cannot be inlined whatever the budget says, so it measures the two bodies with the decision removed. Direct calls them by name, which is where the decision applies. They agree, and that agreement is itself the result: if inlining were worth something here, only one of the two rows would have moved.

This benchmark cannot say anything else, and the reason is my harness. The ladder’s body is n += 1; n += 2; …, which the compiler folds to a single constant addition. Grow26 and Grow27 are the same two instructions with and without a call around them, and a perfectly predicted call is close to free on this machine. The ladder prices the budget correctly and measures speed not at all.

Crossing the edge with a constructor

The case where the decision has a consequence needs a callee whose body the caller needs to see into. Two constructors, identical in what they return:

inlinebudget/cost.go:135:6: can inline makeSmall with cost 5
inlinebudget/cost.go:141:6: cannot inline makeSmallFat: function too complex: cost 172 exceeds budget 80

Both are called from a site that reads one field and drops the pointer:

            │ ctor=inlinable │ ctor=inlinable-control │      ctor=over-budget       │
            │     sec/op     │  sec/op    vs base     │  sec/op     vs base         │
Escape-12      1.791n ± 0%    1.789n ± 0%  ~ (p=0.489)  7.823n ± 1%  +336.77% (p=0.000)

            │      B/op      │          B/op          │            B/op             │
Escape-12         0.00 ± 0%      0.00 ± 0%  ~            32.00 ± 0%   ?

337% and one allocation, from the same call-site shape. Not because the call is expensive — the ladder just showed a call is nearly free — but because a callee the compiler cannot see into gets the conservative escape verdict. makeSmall is folded into its caller, the compiler sees the pointer die, and the value stays in the frame. makeSmallFat is compiled without knowing what any caller does, so the pointer has to be assumed to escape.

That is what the budget actually buys. Not the call, the analysis.

What to do with the number

The price list is not a tuning guide, and treating it as one produces the folklore it replaces. Two things it is good for.

Diagnosing a specific function. When something you expected to be inlined is not, -gcflags='-m -m' names the reason in one line, and the reason is one of two kinds: a cost that exceeds 80, which shrinking can fix, or an unhandled op, which shrinking cannot. Knowing which before you refactor is most of the value here.

Reading the call count rather than the line count. A function with fifteen statements and no calls is comfortably inside the budget. A function with three statements and two calls the compiler cannot see through is not. “Small” turns out to be the wrong axis and it is the one everybody repeats.

What this does not measure

Profile-guided optimisation, which raises the budget for call sites a profile says are hot. Every number here is from a build with no profile, and PGO would move the boundary for exactly the functions most likely to matter.

The price list is also one construct at a time in a trivial body. Real functions interact — a range over a map inside an if is not 9 + 6 + 2 — and the compiler’s accounting has cases this ladder does not exercise.

And the whole table is a single toolchain. cost 80 is go1.27.0; the budget was different before and the per-construct prices move with the compiler’s internals. The command is more durable than the numbers, which is why it is at the top rather than the bottom.

Frequently asked

What is Go's inlining budget?

A cost figure the compiler keeps per function, currently 80 on go1.27.0. Every construct in a function body adds to a running cost, and if the total exceeds the budget the function is not inlined. The compiler will tell you both numbers for free: -gcflags=-m prints "can inline F with cost N" for each function it accepted, and -gcflags='-m -m' prints "cannot inline F: function too complex: cost N exceeds budget 80" for the ones it did not.

What does a function call cost against the inlining budget?

59, measured as the difference between a function containing one call to a non-inlinable callee and the same function without it. That is three quarters of the whole budget in a single statement, which is why a wrapper around two calls is usually not inlinable and a wrapper around one usually is.

Why is my function with a defer never inlined?

Because defer is not expensive, it is disqualifying. The compiler reports "cannot inline WithDefer: unhandled op DEFER" — the function is refused before any cost is computed, so no amount of shrinking it will help. The same applies to go statements, with "unhandled op GO".

Does getting a function under the budget make it faster?

Not by itself. Two functions one statement apart across the boundary — one at cost 80 and inlined, one at cost 83 and not — benchmarked identically, with the not-inlined one marginally faster inside the control pair. What made a measurable difference was a constructor: when the compiler cannot see into a callee it cannot see that the returned pointer dies, so the value goes to the heap. That was 337% and one allocation.

Nolan Keir

Systems-minded Go engineer

Nolan Keir writes about Go, backend engineering, and the systems behind production software. His work focuses on concurrency, runtime behavior, performance, tooling, and the trade-offs hidden behind clean abstractions. He prefers reproducible experiments and measurable behavior over rules of thumb. He writes at Gopheria.

More about the author

Arrow keys to move, Enter to open.