Repository navigation
Keep Get's hit path free of helper calls - #1
Open
JohanLindvall wants to merge 1 commit into
Open
JohanLindvall wants to merge 1 commit into
JohanLindvall wants to merge 1 commit into
Conversation
Both Gets went through factory.reuse, which did the reset check and the miss-path build in one function. Together they cost 149 against the inliner's budget of 80, so every Get paid a call frame, hit or miss. Split it into factory.rewind (the reset check, cost 69) and factory.build (the miss path, cost 76). Both inline, so a hit now calls nothing beyond sync.Pool.Get (or the mutex) and resetFn, and the contract still lives in factory. Get tests rewind in its own if, nested under the nil check: joined with &&, the compiler materializes the result as a bool (SETE/TEST) on the hit path. Measured interleaved: Pool Get+Put without a reset -7.1%, FreeList with a reset -3.1%, and Lumberjack's Benchmark_Sampler_rate -2.3% (40.26 -> 39.34 ns), within noise of its pre-extraction pool. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
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.
Both
Gets went throughfactory.reuse, which did the reset check and the miss-path build in one function. That costs 149 against the inliner's budget of 80, so everyGetpaid an extra call frame, hit or miss. The typed-pool wrapper in Lumberjack that objpool was extracted from did not: itsGetbranched onresetFnitself and only called out to build on a miss.Change
factory.reuseis split intofactory.rewind(the reset check, cost 69) andfactory.build(the miss path, cost 76). The element contract still lives once infactory.Pool.GetandFreeList.Gettestrewindin its ownif, nested under the nil check. Joined with&&, the compiler materializesrewind's result as a bool (SETE+TESTL) on the hit path. Isolated by building both half-variants: the&&inGetcauses it, not the||insiderewind.Inlining evidence
go test -c -gcflags=-m=2in a consumer module (generic methods report inlining only where they are instantiated):No
factoryhelper symbol remains in the consumer binary, andPool.Get's hit path issync.Pool.Get→ type check →resetFn→ return. The oneSETEleft inFreeList.Getissync.Mutex.Lock's inlined CAS, which is also present before.Benchmarks
amd64 (Ultra 9 185H), Go 1.27.1. Variants run interleaved and back to back with the order reversed every other round;
benchstat.Isolated
Get+Puthit path, from a scratch module that is not part of this PR; n=16:Pool, no resetPool, resetFreeList, resetFreeList, no resetReal-world, Lumberjack's per-line sampler, whose
Poolhas a reset; n=20:Benchmark_Sampler_ratepkg/poolBenchmark_DynamicMetrics_Add_ProdShapeis flat (1.004 µs both, p=0.723). Allocations are unchanged everywhere, and zero on every hit path.Checks
gofmt,go vet,go test -race -shuffle=onat 100.0% coverage,golangci-lintv2.13.1: 0 issues.🤖 Generated with Claude Code