Repository navigation
Conversation
load.Wrap puts a loader in front of any evict.Cache, and Fetch loads what the cache does not hold. Fetch calls for one key at the same time wait for a single load, which runs in a goroutine of its own with the values of the first caller's context but not its cancellation. A caller whose context ends stops waiting; the load is canceled, and its value dropped, once nobody waits. Errors are not stored. A loader's panic is raised again in the waiting callers as a *PanicError. A Set, SetIfAbsent, Delete, DeleteFunc or Clear through the wrapper keeps a load in progress from storing what it may have read before the change. Writes lock a stripe for that, which made a Set of tinylfu 60% slower on eight CPUs of an M3 Max; a Fetch that hits took 20-23% longer than a Get. The README loses "No loader" from what the module does not do. Co-Authored-By: Claude Opus 5.5 <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.
Why
"What it does not do" listed No loader. Without one, every caller that misses a hot key goes to the source at once, and a load that finishes after a write can store a value the write made out of date.
SetIfAbsentcovered aSetduring a load, but not aDelete.Changes
load.load.Wrap(cache, loader)returns a*load.Cache, which implementsevict.Cache;GetandPeeknever load.Fetch(ctx, key) (V, error)returns the stored value, or loads it.Fetchcalls for one key at the same time share a single load. It runs in its own goroutine, with the first caller's context values but not its cancellation. A caller whose context ends stops waiting. Once nobody waits, the load's context is canceled and its value is dropped.Fetchas a*load.PanicError(value and stack), and ends the program if nobody waits.runtime.Goexitin a loader gives the waiters an error.Set,SetIfAbsent,Delete,DeleteFuncandClearthrough the wrapper keep a load in progress from storing its value. Callers already waiting still get it; after aDelete,DeleteFuncorClear, new callers start a load of their own. Writes lock one of max(64, 8 × GOMAXPROCS) stripes around the call to the wrapped cache, and a load stores under the same lock.Fetch, as a plain call, and never stored.internal/inlinecheckgets aloadprobe. The README gets a Loading section, and "No loader" leaves "What it does not do".Cost
On an Apple M3 Max with
tinylfu(medians of 8 interleaved runs):Fetchthat hits took 20% longer than aGeton 1 CPU, and 23% on 8.Settook 10 ns longer on 1 CPU, and 60% longer on 8 (118 vs 74 ns).Decisions to confirm
Fetch, and there is one loader per cache rather than one per call.Sethands them back (statsstill counts them).Verification
make test(race detector, root and bench modules) andmake lint, after rebasing onto 2f4bd93.cachetest.RunandNoAllocsrun on the wrapper overlruands3fifo.TestConcurrentmixesFetch, abandonedFetchcalls,Set,Delete,ClearandDeleteFuncagainst a versioned source, then checks that the cache holds no out-of-date value.load.goare killed. One survives: closingdoneafter the unlock infinish, which a comment explains.🤖 Generated with Claude Code