Skip to content

Commit

Permalink
Refact: introduce explicit empty class for TypeCheckCache
Browse files Browse the repository at this point in the history
  • Loading branch information
heshanpadmasiri committed Jun 24, 2024
1 parent 8dc1f34 commit 9ebdb7d
Showing 1 changed file with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected SemType(int all, int some, SubType[] subTypeData) {
this.resultCache = new TypeCheckResultCache();
} else {
useCache = false;
this.resultCache = null;
this.resultCache = TypeCheckResultCache.EMPTY;
}
}

Expand Down Expand Up @@ -151,17 +151,18 @@ void cacheSubTypeRelation(SemType other, boolean result) {
}
}

private static final class TypeCheckResultCache {
private static sealed class TypeCheckResultCache {

private static final TypeCheckResultCache EMPTY = new EmptyTypeCheckResultCache();
private static final int CACHE_LIMIT = 100;
// See if we can use an identity hashmap on semtypes instead of tid
private Map<Long, Boolean> cache = new HashMap<>();

private void cacheResult(int tid, boolean result) {
protected void cacheResult(int tid, boolean result) {
cache.put((long) tid, result);
}

private CachedResult getCachedResult(int tid) {
protected CachedResult getCachedResult(int tid) {
Boolean cachedData = cache.get((long) tid);
if (cachedData == null) {
return CachedResult.NOT_FOUND;
Expand All @@ -170,6 +171,19 @@ private CachedResult getCachedResult(int tid) {
}
}

private static final class EmptyTypeCheckResultCache extends TypeCheckResultCache {

@Override
public void cacheResult(int tid, boolean result) {
throw new UnsupportedOperationException("Empty cache");
}

@Override
public CachedResult getCachedResult(int tid) {
throw new UnsupportedOperationException("Empty cache");
}
}

public final SubType subTypeByCode(int code) {
if ((some() & (1 << code)) == 0) {
return null;
Expand Down

0 comments on commit 9ebdb7d

Please sign in to comment.