Skip to content

Commit

Permalink
Add putIfAbsent optimistic fastpath (#506)
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-manes committed Feb 23, 2021
1 parent 9cc266a commit f5a5e32
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.gradle
Expand Up @@ -42,7 +42,7 @@ allprojects {
version.with {
major = 2 // incompatible API changes
minor = 9 // backwards-compatible additions
patch = 0 // backwards-compatible bug fixes
patch = 1 // backwards-compatible bug fixes
releaseBuild = rootProject.hasProperty('release')
}
}
Expand Down
Expand Up @@ -2029,12 +2029,34 @@ public Map<K, V> getAllPresent(Iterable<?> keys) {
if (prior == node) {
afterWrite(new AddTask(node, newWeight));
return null;
} else if (onlyIfAbsent) {
// An optimistic fast path to avoid unnecessary locking
V currentValue = prior.getValue();
if ((currentValue != null) && !hasExpired(prior, now)) {
if (!isComputingAsync(prior)) {
tryExpireAfterRead(prior, key, currentValue, expiry(), now);
setAccessTime(prior, now);
}
afterRead(prior, now, /* recordHit */ false);
return currentValue;
}
}
} else {
prior = data.putIfAbsent(node.getKeyReference(), node);
if (prior == null) {
afterWrite(new AddTask(node, newWeight));
return null;
} else if (onlyIfAbsent) {
// An optimistic fast path to avoid unnecessary locking
V currentValue = prior.getValue();
if ((currentValue != null) && !hasExpired(prior, now)) {
if (!isComputingAsync(prior)) {
tryExpireAfterRead(prior, key, currentValue, expiry(), now);
setAccessTime(prior, now);
}
afterRead(prior, now, /* recordHit */ false);
return currentValue;
}
}
}
} else if (onlyIfAbsent) {
Expand Down

0 comments on commit f5a5e32

Please sign in to comment.