Skip to content

Add SetIfAbsent to every cache and wrapper - #10

Merged
floatdrop merged 1 commit into
mainfrom
setifabsent
Nov 23, 2026
Merged

floatdrop merged 1 commit into
mainfrom
setifabsent

Conversation

@floatdrop

Copy link
Copy Markdown
Owner

SetIfAbsent(key, value) (actual V, present bool, oldKey K, oldValue V, evicted bool) on every cache, stats.Cache and expire.Cache, and in evict.Cache.

  • Stores value only if key has no entry; otherwise records the access as Get does and returns the value key has.
  • Atomic under the shard lock: of goroutines racing on one key, one stores and the rest get its value back. For deduplication and state per key, such as rate limiters.
  • The last three results are Set's, so an evicted entry can be released.
  • Breaking: evict.Cache gains the method, so types outside the module that implement it need one too.
  • stats counts only evictions for it, no hit or miss.
  • expire takes an expired entry for none. Callers that find the same expired entry take turns under one of 64 striped locks and look again with Peek. A value stored just after that look is replaced and handed back; the doc says so.

Tests: shared suite (testSetIfAbsent, SetIfAbsentOnce, model comparison, concurrency, no allocations), expire's expiry, race and one-winner tests, stats counts. Two review agents and mutation testing; every mutant found is killed. inlinecheck covers SetIfAbsent and the add that Set now goes through.

Apple M3 Max, one CPU: a present key took 12–15 ns, about a Get hit; one that evicts about as long as Set. Set is within noise, but for lru's Set that evicts, 3.5% slower in the median (39.3 → 40.7 ns).

🤖 Generated with Claude Code

SetIfAbsent(key, value) stores value unless key has an entry, and if it
has, returns that entry's value with present true. The look and the
store happen under the shard lock, so of the goroutines that race on one
key, one stores its value and the rest get it back. The last three
results are Set's: the entry that left the cache for the new one, if
any. evict.Cache gains the method, which breaks types outside the module
that implement it.

A key that is present counts as an access, as Get does. stats counts
only evictions for SetIfAbsent, no hit or miss, since a Get usually
comes first. Each shard's set splits into set and add, the insert path,
and inlinecheck covers both.

expire takes an expired entry for none: SetIfAbsent stores the new one
in its place and hands the expired one back, as Set does. Callers that
find the same expired entry take turns under one of 64 locks of the
Cache and look again with Peek. A value stored just after that look, by
a Set or by a SetIfAbsent that found the expired entry gone, is replaced
and handed back; the documentation says so. stats counts no eviction
for an expired entry of the key that is replaced, as for Set.

On an Apple M3 Max with one CPU, SetIfAbsent of a present key took 12
to 15 ns, about a Get hit, and one that evicts about as long as Set.
Set stayed within noise, but for lru's Set that evicts: 3.5% slower in
the median of six interleaved runs, 39.3 to 40.7 ns. Through expire, a
present key took about 18 ns more, in one run: it reads the time for
the entry it may store, as Set does.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@floatdrop
floatdrop merged commit 3b5f66b into main Nov 23, 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