From 07383de351e09a54f0c17842ea49230fb5deee7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Va=C5=BEan?= Date: Sat, 20 Jan 2024 21:26:40 +0000 Subject: [PATCH] Report inconsistencies between derivative cache linker and compute() early --- .../com/machinezoo/foxcache/CachedData.java | 62 ++++++++++--------- 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/src/main/java/com/machinezoo/foxcache/CachedData.java b/src/main/java/com/machinezoo/foxcache/CachedData.java index b27ab54..45b1374 100644 --- a/src/main/java/com/machinezoo/foxcache/CachedData.java +++ b/src/main/java/com/machinezoo/foxcache/CachedData.java @@ -9,33 +9,37 @@ @CodeIssue("Downgrade softref to weakref after one second. Anything that needs longer caching should be persisted.") class CachedData { - static final ConcurrentMap, Object> lazy = new ConcurrentHashMap<>(); - /* - * Soft-valued cache may cause extremely inefficient GC behavior: - * https://bugs.openjdk.java.net/browse/JDK-6912889 - * - * It is however very simple and it will use all RAM that is allocated to Java process, - * which is usually some fraction of physical RAM. - * This cache can be tuned indirectly with -Xmx and -XX:SoftRefLRUPolicyMSPerMB. - * - * Cached value is wrapped in Optional, because Guava cache does not tolerate null values. - */ - static final LoadingCache, Optional> compute = CacheBuilder.newBuilder() - .softValues() - .build(CacheLoader.from(k -> Optional.ofNullable(k.compute()))); - /* - * Like in ComputeCaches, just specialized for DerivativeCache. - */ - static final LoadingCache, ReactiveLazy>> derivative = CacheBuilder.newBuilder() - .softValues() - .build(CacheLoader.from(k -> materialize(k))); - private static ReactiveLazy> materialize(DerivativeCache cache) { - return new ReactiveLazy<>(() -> CacheDerivative.capture(() -> { - /* - * Touch the cache just in case there are extra dependencies there. - */ - cache.touch(); - return cache.compute(); - })); - } + static final ConcurrentMap, Object> lazy = new ConcurrentHashMap<>(); + /* + * Soft-valued cache may cause extremely inefficient GC behavior: + * https://bugs.openjdk.java.net/browse/JDK-6912889 + * + * It is however very simple and it will use all RAM that is allocated to Java process, + * which is usually some fraction of physical RAM. + * This cache can be tuned indirectly with -Xmx and -XX:SoftRefLRUPolicyMSPerMB. + * + * Cached value is wrapped in Optional, because Guava cache does not tolerate null values. + */ + static final LoadingCache, Optional> compute = CacheBuilder.newBuilder() + .softValues() + .build(CacheLoader.from(k -> Optional.ofNullable(k.compute()))); + /* + * Like in ComputeCaches, just specialized for DerivativeCache. + */ + static final LoadingCache, ReactiveLazy>> derivative = CacheBuilder.newBuilder() + .softValues() + .build(CacheLoader.from(k -> materialize(k))); + private static ReactiveLazy> materialize(DerivativeCache cache) { + return new ReactiveLazy<>(() -> CacheDerivative.capture(() -> { + /* + * Touch the cache just in case there are extra dependencies there. + */ + cache.touch(); + /* + * Report inconsistencies between linker and compute() early. + */ + CacheInput.get().freeze(); + return cache.compute(); + })); + } }