aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornea <romangraef@gmail.com>2022-12-19 23:10:16 +0100
committernea <romangraef@gmail.com>2022-12-24 02:52:20 +0100
commit7f09cac0aa1451ef0e0c330f908cc279df323e17 (patch)
treec7c63c23d093560e3b808496d54b389cea4f0dbf
parent912654c092ad123febf9f22775d8569d85517646 (diff)
downloadNotEnoughUpdates-7f09cac0aa1451ef0e0c330f908cc279df323e17.tar.gz
NotEnoughUpdates-7f09cac0aa1451ef0e0c330f908cc279df323e17.tar.bz2
NotEnoughUpdates-7f09cac0aa1451ef0e0c330f908cc279df323e17.zip
LRUCache: Cache null results
-rw-r--r--src/main/java/io/github/moulberry/notenoughupdates/util/LRUCache.java11
-rw-r--r--src/main/kotlin/io/github/moulberry/notenoughupdates/miscfeatures/inventory/MuseumItemHighlighter.kt1
2 files changed, 7 insertions, 5 deletions
diff --git a/src/main/java/io/github/moulberry/notenoughupdates/util/LRUCache.java b/src/main/java/io/github/moulberry/notenoughupdates/util/LRUCache.java
index f107d522..6adbc30d 100644
--- a/src/main/java/io/github/moulberry/notenoughupdates/util/LRUCache.java
+++ b/src/main/java/io/github/moulberry/notenoughupdates/util/LRUCache.java
@@ -32,13 +32,15 @@ public interface LRUCache<K, V> extends Function<K, V> {
}
static <K, V> LRUCache<K, V> memoize(Function<K, V> mapper, IntSupplier maxCacheSize) {
- Map<K, V> cache = new LinkedHashMap<K, V>(10, 0.75F, true) {
+ Map<K, Object> cache = new LinkedHashMap<K, Object>(10, 0.75F, true) {
@Override
- protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
+ protected boolean removeEldestEntry(Map.Entry<K, Object> eldest) {
return this.size() > maxCacheSize.getAsInt();
}
};
- Map<K, V> synchronizedCache = Collections.synchronizedMap(cache);
+ Object SENTINEL_CACHE_RESULT_NULL = new Object();
+ Function<K, Object> sentinelAwareMapper = mapper.andThen(it -> it == null ? SENTINEL_CACHE_RESULT_NULL : it);
+ Map<K, Object> synchronizedCache = Collections.synchronizedMap(cache);
return new LRUCache<K, V>() {
@Override
public void clearCache() {
@@ -52,7 +54,8 @@ public interface LRUCache<K, V> extends Function<K, V> {
@Override
public V apply(K k) {
- return synchronizedCache.computeIfAbsent(k, mapper);
+ Object value = synchronizedCache.computeIfAbsent(k, sentinelAwareMapper);
+ return value == SENTINEL_CACHE_RESULT_NULL ? null : (V) value;
}
};
}
diff --git a/src/main/kotlin/io/github/moulberry/notenoughupdates/miscfeatures/inventory/MuseumItemHighlighter.kt b/src/main/kotlin/io/github/moulberry/notenoughupdates/miscfeatures/inventory/MuseumItemHighlighter.kt
index 75bd1ce8..68810db1 100644
--- a/src/main/kotlin/io/github/moulberry/notenoughupdates/miscfeatures/inventory/MuseumItemHighlighter.kt
+++ b/src/main/kotlin/io/github/moulberry/notenoughupdates/miscfeatures/inventory/MuseumItemHighlighter.kt
@@ -26,7 +26,6 @@ import io.github.moulberry.notenoughupdates.core.util.StringUtils
import io.github.moulberry.notenoughupdates.events.GuiContainerBackgroundDrawnEvent
import io.github.moulberry.notenoughupdates.events.ReplaceItemEvent
import io.github.moulberry.notenoughupdates.events.RepositoryReloadEvent
-import io.github.moulberry.notenoughupdates.mixins.AccessorGuiContainer
import io.github.moulberry.notenoughupdates.util.ItemUtils
import io.github.moulberry.notenoughupdates.util.LRUCache
import net.minecraft.client.gui.Gui