Allocate batchRequest only on cache miss#123
Merged
pavelnikolov merged 1 commit intoMay 27, 2026
Merged
Conversation
Loader.Load allocated the batchRequest and its done channel at the top of the function, before the cache lookup. req is only ever used on a cache miss (it is sent to the batcher), so on a cache hit it was allocated and immediately discarded. Move the allocation below the cache-hit early return so cached loads don't pay for it. Benchmark (Load on a primed key, -benchmem): before: 3 allocs/op 160 B/op 45.7 ns/op after: 1 alloc/op 16 B/op 20.5 ns/op Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Member
|
Released v7.2.0 |
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.
Summary
Loader.Loadallocated thebatchRequestand itsdonechannel at the top of the function, before the cache lookup.reqis only used on a cache miss (it's sent to the batcher and read by the returned thunk), so on a cache hit it was allocated and immediately discarded — every cached load paid for a struct + channel allocation it never used.This moves the allocation below the cache-hit early-return so cached loads don't pay for it. No behavioural change: nothing between the cache check and the new allocation site references
req.Fixes #122.
Benchmark
Loadon a primed/cached key (-benchmem):Testing
go test .passes. (The pre-existing build failures inexample/lru_cacheandexample/ttl_cacheare unrelated to this change — they fail the same way on unmodifiedmaster.)