Skip to content

Commit

Permalink
Optimize getAllPresent methods
Browse files Browse the repository at this point in the history
This restores an optimization that was removed in #176 due to a bug in
Java 8.
  • Loading branch information
ben-manes committed Feb 8, 2021
1 parent 1f51eb3 commit fbcddc0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
Expand Down Expand Up @@ -1916,33 +1915,33 @@ public boolean containsValue(Object value) {

@Override
public Map<K, V> getAllPresent(Iterable<?> keys) {
Set<Object> uniqueKeys = new LinkedHashSet<>();
Map<Object, Object> result = new LinkedHashMap<>();
for (Object key : keys) {
uniqueKeys.add(key);
result.put(key, null);
}

int misses = 0;
int uniqueKeys = result.size();
long now = expirationTicker().read();
Map<Object, Object> result = new LinkedHashMap<>(uniqueKeys.size());
for (Object key : uniqueKeys) {
for (var iter = result.entrySet().iterator(); iter.hasNext();) {
Map.Entry<Object, Object> entry = iter.next();
V value;
Node<K, V> node = data.get(nodeFactory.newLookupKey(key));
Node<K, V> node = data.get(nodeFactory.newLookupKey(entry.getKey()));
if ((node == null) || ((value = node.getValue()) == null) || hasExpired(node, now)) {
misses++;
iter.remove();
} else {
result.put(key, value);
entry.setValue(value);

if (!isComputingAsync(node)) {
@SuppressWarnings("unchecked")
K castedKey = (K) key;
K castedKey = (K) entry.getKey();
tryExpireAfterRead(node, castedKey, value, expiry(), now);
setAccessTime(node, now);
}
afterRead(node, now, /* recordHit */ false);
}
}
statsCounter().recordMisses(misses);
statsCounter().recordHits(result.size());
statsCounter().recordMisses(uniqueKeys - result.size());

@SuppressWarnings("unchecked")
Map<K, V> castedResult = (Map<K, V>) result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
Expand Down Expand Up @@ -474,24 +473,25 @@ abstract class AbstractCacheView<K, V> implements Cache<K, V>, Serializable {

@Override
public Map<K, V> getAllPresent(Iterable<?> keys) {
Set<Object> uniqueKeys = new LinkedHashSet<>();
Map<Object, Object> result = new LinkedHashMap<>();
for (Object key : keys) {
uniqueKeys.add(key);
result.put(key, null);
}

int misses = 0;
Map<Object, Object> result = new LinkedHashMap<>();
for (Object key : uniqueKeys) {
CompletableFuture<V> future = asyncCache().cache().get(key);
int uniqueKeys = result.size();
for (var iter = result.entrySet().iterator(); iter.hasNext();) {
Map.Entry<Object, Object> entry = iter.next();

CompletableFuture<V> future = asyncCache().cache().get(entry.getKey());
Object value = Async.getIfReady(future);
if (value == null) {
misses++;
iter.remove();
} else {
result.put(key, value);
entry.setValue(value);
}
}
asyncCache().cache().statsCounter().recordMisses(misses);
asyncCache().cache().statsCounter().recordHits(result.size());
asyncCache().cache().statsCounter().recordMisses(uniqueKeys - result.size());

@SuppressWarnings("unchecked")
Map<K, V> castedResult = (Map<K, V>) result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.function.Function;
Expand Down Expand Up @@ -64,24 +62,26 @@ default Map<K, V> getAll(Iterable<? extends K> keys) {

/** Sequentially loads each missing entry. */
default Map<K, V> loadSequentially(Iterable<? extends K> keys) {
Set<K> uniqueKeys = new LinkedHashSet<>();
Map<K, V> result = new LinkedHashMap<>();
for (K key : keys) {
uniqueKeys.add(key);
result.put(key, null);
}

int count = 0;
Map<K, V> result = new LinkedHashMap<>(uniqueKeys.size());
try {
for (K key : uniqueKeys) {
for (var iter = result.entrySet().iterator(); iter.hasNext();) {
Map.Entry<K, V> entry = iter.next();
count++;

V value = get(key);
if (value != null) {
result.put(key, value);
V value = get(entry.getKey());
if (value == null) {
iter.remove();
} else {
entry.setValue(value);
}
}
} catch (Throwable t) {
cache().statsCounter().recordMisses(uniqueKeys.size() - count);
cache().statsCounter().recordMisses(result.size() - count);
throw t;
}
return Collections.unmodifiableMap(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
Expand Down Expand Up @@ -110,23 +109,23 @@ public long estimatedSize() {

@Override
public Map<K, V> getAllPresent(Iterable<?> keys) {
Set<Object> uniqueKeys = new LinkedHashSet<>();
Map<Object, Object> result = new LinkedHashMap<>();
for (Object key : keys) {
uniqueKeys.add(key);
result.put(key, null);
}

int misses = 0;
Map<Object, Object> result = new LinkedHashMap<>(uniqueKeys.size());
for (Object key : uniqueKeys) {
Object value = data.get(key);
int uniqueKeys = result.size();
for (var iter = result.entrySet().iterator(); iter.hasNext();) {
Map.Entry<Object, Object> entry = iter.next();
Object value = data.get(entry.getKey());
if (value == null) {
misses++;
iter.remove();
} else {
result.put(key, value);
entry.setValue(value);
}
}
statsCounter.recordMisses(misses);
statsCounter.recordHits(result.size());
statsCounter.recordMisses(uniqueKeys - result.size());

@SuppressWarnings("unchecked")
Map<K, V> castedResult = (Map<K, V>) result;
Expand Down

0 comments on commit fbcddc0

Please sign in to comment.