From bf4a3c951e2e476e1eeaec53d1000d45595a2ac5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Mon, 5 Feb 2024 09:14:06 +0100 Subject: [PATCH] Unnecessary generic / lamda in CharDeduplication#CacheReference CharDeduplication#CacheReference uses generics with a lamda supplier to handle a SoftReference to the cache, but this generalization/abstraction is actually not needed or used anywhere. This removes the CacheReference and implements its functionality directly in the CharDeduplication#getThreadLocalInstance making it easier to follow the flow and not creating lamda instance indirections. --- .../compiler/util/CharDeduplication.java | 31 +++++-------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/util/CharDeduplication.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/util/CharDeduplication.java index 92958a9d3e0..0b6bf6e6a1c 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/util/CharDeduplication.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/util/CharDeduplication.java @@ -17,7 +17,6 @@ import java.lang.ref.SoftReference; import java.util.Arrays; -import java.util.function.Supplier; public class CharDeduplication { @@ -35,31 +34,17 @@ public class CharDeduplication { /** number of entries to linear search affects performance but decreases collisions - does not affect memory */ public static final int SEARCH_SIZE = 8; // a power of 2, has to be smaller then TABLE_SIZE - /** avoid OOME by additional CharDeduplication memory **/ - static final class CacheReference { - private SoftReference reference; - private final Supplier supplier; - - CacheReference(Supplier supplier) { - this.supplier = supplier; - this.reference = new SoftReference<>(supplier.get()); - } - - T get() { - T referent = this.reference.get(); - if (referent == null) { - referent = this.supplier.get(); - this.reference = new SoftReference<>(referent); - } - return referent; - } - } - - private final static ThreadLocal> mutableCache = ThreadLocal.withInitial(()->new CacheReference<>(CharDeduplication::new)); + private final static ThreadLocal> mutableCache = ThreadLocal + .withInitial(() -> new SoftReference<>(new CharDeduplication())); /** @return an instance that is *not* thread safe. To be used in a single thread only. **/ public static CharDeduplication getThreadLocalInstance() { - return mutableCache.get().get(); + CharDeduplication local = mutableCache.get().get(); + if (local == null) { + local = new CharDeduplication(); + mutableCache.set(new SoftReference<>(local)); + } + return local; } // ----- mutable non-static part (not thread safe!): ----