Repository navigation
fix: a captured variable put an allocation on every list request - #77
Merged
Merged
Conversation
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]>
6 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Found by re-measuring
docs/benchmarks.mdrather than by review — the table said 83 allocations per merged list at 20×50 andmainmeasured 103.The regression
Bisected to
93a44b6(definition pinning, v1.12.0):abfb737(parent)93a44b6(pinning)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.mddescribes. What cost is the capture around it: referencingu.pinsgave 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 beforecachedListdecides whether it needs to fill, so every warmtools/listpaid 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
-memprofileover reading diffs.The fix
cachedList'sfetchtakes 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:
Race suite clean,
golangci-lint0 issues, added p50 197µs.Docs
docs/benchmarks.mdre-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: thens/opgrowth 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.mdgains 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