Skip to content

Commit

Permalink
Unnecessary generic / lamda in CharDeduplication#CacheReference
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
laeubi authored and akurtakov committed Feb 8, 2024
1 parent b3d3957 commit bf4a3c9
Showing 1 changed file with 8 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import java.lang.ref.SoftReference;
import java.util.Arrays;
import java.util.function.Supplier;

public class CharDeduplication {

Expand All @@ -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<T> {
private SoftReference<T> reference;
private final Supplier<? extends T> supplier;

CacheReference(Supplier<? extends T> 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<CacheReference<CharDeduplication>> mutableCache = ThreadLocal.withInitial(()->new CacheReference<>(CharDeduplication::new));
private final static ThreadLocal<SoftReference<CharDeduplication>> 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!): ----
Expand Down

0 comments on commit bf4a3c9

Please sign in to comment.