Skip to content

Add load, which shares one load among the callers of a key - #12

Merged
floatdrop merged 1 commit into
mainfrom
load
Nov 25, 2026
Merged

floatdrop merged 1 commit into
mainfrom
load

Conversation

@floatdrop

Copy link
Copy Markdown
Owner

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. SetIfAbsent covered a Set during a load, but not a Delete.

Changes

  • New package load. load.Wrap(cache, loader) returns a *load.Cache, which implements evict.Cache; Get and Peek never load. Fetch(ctx, key) (V, error) returns the stored value, or loads it.
  • Fetch calls 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.
  • Errors are not stored. A loader panic is raised again in every waiting Fetch as a *load.PanicError (value and stack), and ends the program if nobody waits. runtime.Goexit in a loader gives the waiters an error.
  • Set, SetIfAbsent, Delete, DeleteFunc and Clear through the wrapper keep a load in progress from storing its value. Callers already waiting still get it; after a Delete, DeleteFunc or Clear, 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.
  • NaN keys are loaded by every Fetch, as a plain call, and never stored.
  • internal/inlinecheck gets a load probe. 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):

  • A Fetch that hits took 20% longer than a Get on 1 CPU, and 23% on 8.
  • A Set took 10 ns longer on 1 CPU, and 60% longer on 8 (118 vs 74 ns).
  • A load costs 0.56 µs, 6 allocations and 352 B with int keys and values, plus the loader's own time.

Decisions to confirm

  • The loading method is called Fetch, and there is one loader per cache rather than one per call.
  • An abandoned load is canceled and its value dropped, rather than allowed to finish and fill the cache.
  • Entries that a load's store evicts are not handed back the way Set hands them back (stats still counts them).
  • There is no batch load.

Verification

  • make test (race detector, root and bench modules) and make lint, after rebasing onto 2f4bd93.
  • cachetest.Run and NoAllocs run on the wrapper over lru and s3fifo.
  • TestConcurrent mixes Fetch, abandoned Fetch calls, Set, Delete, Clear and DeleteFunc against a versioned source, then checks that the cache holds no out-of-date value.
  • Deterministic tests use hooks (load start and finish, stripe-lock checks) instead of sleeps. 27 mutants of load.go are killed. One survives: closing done after the unlock in finish, which a comment explains.

🤖 Generated with Claude Code

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>
@floatdrop
floatdrop merged commit 05b2067 into main Nov 25, 2026
7 checks passed
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