-
Notifications
You must be signed in to change notification settings - Fork 758
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8327cd8
commit f298132
Showing
41 changed files
with
693 additions
and
213 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 19 additions & 16 deletions
35
...allerina-runtime/src/main/java/io/ballerina/runtime/api/types/semtype/TypeCheckCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,44 @@ | ||
package io.ballerina.runtime.api.types.semtype; | ||
|
||
import io.ballerina.runtime.api.types.Type; | ||
import io.ballerina.runtime.internal.types.semtype.UniqueLookupKey; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.WeakHashMap; | ||
|
||
/** | ||
* Generalized implementation of type check result cache. It is okay to access | ||
* this from multiple threads but makes no | ||
* guarantee about the consistency of the cache under parallel access. Given | ||
* result don't change due to race conditions | ||
* Generalized implementation of type check result cache. It is okay to access this from multiple threads but makes no | ||
* guarantee about the consistency of the cache under parallel access. Given result don't change due to race conditions | ||
* this should eventually become consistent. | ||
* | ||
* @param <T> Type of the type descriptor which owns this cache | ||
* @since 2201.11.0 | ||
*/ | ||
public class TypeCheckCache<T extends Type> { | ||
public class TypeCheckCache { | ||
|
||
// Not synchronizing this should be fine since race conditions don't lead to inconsistent results. (i.e. results | ||
// of doing multiple type checks are agnostic to the order of execution). Data races shouldn't lead to tearing in | ||
// 64-bit JVMs. | ||
private final Map<T, Boolean> cachedResults = new WeakHashMap<>(); | ||
private final T owner; | ||
private final Map<TypeCheckCacheKey, Boolean> cachedResults = new WeakHashMap<>(); | ||
private final TypeCheckCacheKey ownerKey; | ||
|
||
public TypeCheckCache(T owner) { | ||
this.owner = owner; | ||
public TypeCheckCache(CacheableTypeDescriptor owner) { | ||
this.ownerKey = owner.getLookupKey(); | ||
} | ||
|
||
public Optional<Boolean> cachedTypeCheckResult(T other) { | ||
if (other.equals(owner)) { | ||
public Optional<Boolean> cachedTypeCheckResult(CacheableTypeDescriptor other) { | ||
TypeCheckCacheKey otherKey = other.getLookupKey(); | ||
if (otherKey.equals(ownerKey)) { | ||
return Optional.of(true); | ||
} | ||
return Optional.ofNullable(cachedResults.get(other)); | ||
return Optional.ofNullable(cachedResults.get(otherKey)); | ||
} | ||
|
||
public void cacheTypeCheckResult(T other, boolean result) { | ||
cachedResults.put(other, result); | ||
public void cacheTypeCheckResult(CacheableTypeDescriptor other, boolean result) { | ||
TypeCheckCacheKey lookupKey = other.getLookupKey(); | ||
// FIXME: this is just to prevent cache becoming too large. Revisit this after structured keys | ||
if (lookupKey instanceof UniqueLookupKey) { | ||
return; | ||
} | ||
cachedResults.put(lookupKey, result); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...a-runtime/src/main/java/io/ballerina/runtime/api/types/semtype/TypeCheckCacheFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package io.ballerina.runtime.api.types.semtype; | ||
|
||
import io.ballerina.runtime.internal.types.semtype.UniqueLookupKey; | ||
|
||
import java.util.Map; | ||
import java.util.WeakHashMap; | ||
import java.util.concurrent.locks.ReadWriteLock; | ||
import java.util.concurrent.locks.ReentrantReadWriteLock; | ||
|
||
public final class TypeCheckCacheFactory { | ||
|
||
private static final ReadWriteLock lock = new ReentrantReadWriteLock(); | ||
|
||
private static final Map<TypeCheckCacheKey, TypeCheckCache> cache = new WeakHashMap<>(); | ||
|
||
private TypeCheckCacheFactory() { | ||
} | ||
|
||
public static TypeCheckCache getTypeCheckCache(CacheableTypeDescriptor owner) { | ||
var key = owner.getLookupKey(); | ||
if (key instanceof UniqueLookupKey) { | ||
return new TypeCheckCache(owner); | ||
} | ||
lock.readLock().lock(); | ||
try { | ||
if (cache.containsKey(key)) { | ||
return cache.get(key); | ||
} | ||
} finally { | ||
lock.readLock().unlock(); | ||
} | ||
lock.writeLock().lock(); | ||
try { | ||
if (cache.containsKey(key)) { | ||
return cache.get(key); | ||
} | ||
var typeCheckCache = new TypeCheckCache(owner); | ||
cache.put(key, typeCheckCache); | ||
return typeCheckCache; | ||
} finally { | ||
lock.writeLock().unlock(); | ||
} | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
...erina-runtime/src/main/java/io/ballerina/runtime/api/types/semtype/TypeCheckCacheKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package io.ballerina.runtime.api.types.semtype; | ||
|
||
/** | ||
* Marker type to represent the lookup key used in cache lookup operations involving {@CacheableTypeDescriptor}. | ||
* | ||
* @since 2201.12.0 | ||
*/ | ||
public interface TypeCheckCacheKey { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.