Skip to content

fix: a captured variable put an allocation on every list request - #77

Merged
blakebauman merged 1 commit into
mainfrom
fix/list-closure-allocation
Aug 15, 2026
Merged

blakebauman merged 1 commit into
mainfrom
fix/list-closure-allocation

Conversation

@blakebauman

Copy link
Copy Markdown
Member

Found by re-measuring docs/benchmarks.md rather than by review — the table said 83 allocations per merged list at 20×50 and main measured 103.

The regression

Bisected to 93a44b6 (definition pinning, v1.12.0):

commit allocs/op (20 upstreams × 50 tools)
abfb737 (parent) 83
93a44b6 (pinning) 103
this branch 83

Exactly one extra allocation per upstream, on the warm-cache path.

The cause is not the hashing

The pin check sits inside the fill closure, so it runs once per cache generation exactly as docs/design-definition-pinning.md describes. What cost is the capture around it: referencing u.pins gave the closure a free variable, and Go allocates a capturing function literal on every call while a non-capturing one is allocated statically, once. The closure is constructed before cachedList decides whether it needs to fill, so every warm tools/list paid for it — per upstream, on fold's hottest fan-out.

The profiler pointed at the closure line directly; every hypothesis I formed before profiling was wrong, which is the argument for -memprofile over reading diffs.

The fix

cachedList's fetch takes the upstream as a parameter instead of closing over it. The parameter is redundant at every call site — that is the point, and it carries a comment saying so, since the obvious "cleanup" is to delete it and reintroduce the capture.

Verification

Median of 5, after the fix:

federation ns/op B/op allocs/op
1 × 10 1,251 640 16
5 × 20 5,332 3,336 31
20 × 50 31,003 21,871 83
20 × 50 + policy 54,190 21,866 83

Race suite clean, golangci-lint 0 issues, added p50 197µs.

Docs

  • docs/benchmarks.md re-measured, and it records the regression rather than quietly restating the old numbers — including that v1.12.0 through v1.14.0 carry the extra allocation, since this fix lands after the v1.14.0 tag. It also separates what this fix does not explain: the ns/op growth since 2026-08-07 is real CPU cost from tenant resolution, the visibility subset, and consumption metering landing in that window.
  • docs/design-definition-pinning.md gains the implementation note this belonged in — the check was in the right place and the cost was in the closure around it.

🤖 Generated with Claude Code

Definition pinning (v1.12.0) hung its check off the closure each list method
hands cachedList. The check itself is inside the fill, so it runs once per
cache generation exactly as designed — but referencing u.pins gave the
closure a free variable, and Go allocates a capturing function literal on
every call while a non-capturing one is allocated statically, once. The
closure is built before cachedList decides whether it is needed, so every
warm-cache tools/list paid one allocation per upstream: the warm path the
pinning design record promised the feature would stay off.

Measured on the federated list bench: 20 upstreams x 50 tools went from 83
allocations per merged list to 103 at 93a44b6, and is back to 83 here. The
count had drifted from docs/benchmarks.md for three releases without anyone
noticing, which is what re-measuring rather than trusting the table catches.

fetch now takes the upstream as a parameter. The parameter looks redundant —
every caller has u in scope — and that is exactly why it carries a comment:
the redundancy is what keeps the literal free of captures.

docs/benchmarks.md is re-measured (median of 5) and records the regression
rather than quietly restating the old numbers; the pinning design record
gains the implementation note this cost belonged in.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@blakebauman
blakebauman merged commit 87aa746 into main Aug 15, 2026
8 checks passed
@blakebauman
blakebauman deleted the fix/list-closure-allocation branch August 15, 2026 15:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant