Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weak cache optimization #5344

Merged
merged 1 commit into from
Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,7 @@ final class WeakLockFreeCache<K, V> implements Cache<K, V> {

@Override
public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
V value = get(key);
if (value != null) {
return value;
}
// Best we can do, we don't expect high contention with this implementation. Note, this
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oof yeah stopped being true when stopped using caffeine

// prevents executing mappingFunction twice but it does not prevent executing mappingFunction
// if there is a concurrent put operation as would be the case for ConcurrentHashMap. However,
// we would never expect an order guarantee in this case anyways so it still has the same
// safety.
synchronized (delegate) {
value = get(key);
if (value != null) {
return value;
}
value = mappingFunction.apply(key);
V previous = delegate.putIfAbsent(key, value);
if (previous != null) {
return previous;
}
return value;
}
return delegate.computeIfAbsent(key, mappingFunction);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.NoSuchElementException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Function;
import javax.annotation.Nullable;

/**
Expand Down Expand Up @@ -161,6 +162,22 @@ public V putIfAbsent(K key, V value) {
return previous == null ? target.putIfAbsent(new WeakKey<>(key, this), value) : previous;
}

public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
if (key == null || mappingFunction == null) {
throw new NullPointerException();
}
V previous;
L lookupKey = getLookupKey(key);
try {
previous = target.get(lookupKey);
} finally {
resetLookupKey(lookupKey);
}
return previous == null
? target.computeIfAbsent(new WeakKey<>(key, this), ignored -> mappingFunction.apply(key))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought this was impossible due to needing to pass the weak key to mapping function but the solution of using a capturing lambda was so simple!

: previous;
}

/**
* @param key The key of the entry.
* @param value The value of the entry.
Expand Down