Skip to content

Commit

Permalink
Make isSame use cacheable path and avoid CAS in Cache
Browse files Browse the repository at this point in the history
  • Loading branch information
heshanpadmasiri committed Feb 10, 2025
1 parent 944c0dc commit a987a20
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicIntegerArray;
import java.util.concurrent.atomic.AtomicReferenceArray;

/**
* Generalized implementation of type check result cache. It is okay to access
Expand All @@ -21,14 +19,12 @@
public class TypeCheckCache {

private static final int SIZE = 10;
private final AtomicReferenceArray<CachedResult> cachedResults;
private final AtomicIntegerArray hitCounts;
private final CachedResult[] cachedResults = new CachedResult[SIZE];
private final int[] hitCounts = new int[SIZE];
private final static AtomicInteger nextId = new AtomicInteger(0);
private final int id = nextId.getAndIncrement();

private TypeCheckCache() {
cachedResults = new AtomicReferenceArray<>(SIZE);
hitCounts = new AtomicIntegerArray(SIZE);
}

public Result cachedTypeCheckResult(CacheableTypeDescriptor other) {
Expand All @@ -37,14 +33,14 @@ public Result cachedTypeCheckResult(CacheableTypeDescriptor other) {
int minHitCount = Integer.MAX_VALUE;
int targetTypeId = other.typeId();
for (int i = 0; i < SIZE; i++) {
var each = cachedResults.get(i);
var each = cachedResults[i];
if (each != null) {
if (each.typeId == targetTypeId) {
hitCounts.incrementAndGet(i);
hitCounts[i]++;
return new Result(true, each.result, null);
}
}
int hitCount = hitCounts.get(i);
int hitCount = hitCounts[i];
if (minHitCount > hitCount) {
minHitCount = hitCount;
replacementCandidateId = i;
Expand All @@ -57,10 +53,8 @@ public Result cachedTypeCheckResult(CacheableTypeDescriptor other) {
public void cacheTypeCheckResult(CacheableTypeDescriptor other, boolean result, ReplacementData replacementData) {
int index = replacementData.index;
CachedResult newValue = new CachedResult(other.typeId(), result);
// Probably this has an effect only on ARM (and other RISC) not X86
if (cachedResults.weakCompareAndSetPlain(index, replacementData.candidate, newValue)) {
hitCounts.setPlain(index, 0);
}
cachedResults[index] = newValue;
hitCounts[index] = 0;
}

public record Result(boolean hit, boolean result, ReplacementData replacementData) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,7 @@ private static SemType appendNumericConversionTypes(SemType semType) {
* @return true if the two types are same; false otherwise
*/
public static boolean isSameType(Type sourceType, Type targetType) {
Context cx = context();
return Core.isSameType(cx, SemType.tryInto(cx, sourceType), SemType.tryInto(cx, targetType));
return sourceType == targetType || (checkIsType(sourceType, targetType) && checkIsType(targetType, sourceType));
}

public static Type getType(Object value) {
Expand Down

0 comments on commit a987a20

Please sign in to comment.