Repository navigation
Add SetIfAbsent to every cache and wrapper - #10
Merged
Merged
Conversation
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]>
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.
SetIfAbsent(key, value) (actual V, present bool, oldKey K, oldValue V, evicted bool)on every cache,stats.Cacheandexpire.Cache, and inevict.Cache.Getdoes and returns the value key has.Set's, so an evicted entry can be released.evict.Cachegains the method, so types outside the module that implement it need one too.statscounts only evictions for it, no hit or miss.expiretakes an expired entry for none. Callers that find the same expired entry take turns under one of 64 striped locks and look again withPeek. 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.inlinecheckcoversSetIfAbsentand theaddthatSetnow goes through.Apple M3 Max, one CPU: a present key took 12–15 ns, about a
Gethit; one that evicts about as long asSet.Setis within noise, but forlru'sSetthat evicts, 3.5% slower in the median (39.3 → 40.7 ns).🤖 Generated with Claude Code