Skip to content

Commit

Permalink
Merge pull request #42111 from heshanpadmasiri/optim/cache-type-calc
Browse files Browse the repository at this point in the history
Cache repeated type calculations
  • Loading branch information
heshanpadmasiri authored Feb 19, 2024
2 parents 91e7d41 + b044eba commit b5c6a95
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,49 @@
*/
public interface Type {

// TODO: remove default implementations when standard library types are updated
/**
* Set the referred type for this type once it has been calculated. This must be called the first time this
* calculation is done for {@code Type#getReferredTypeCache()} to work properly. This is non-blocking and
* will eventually become consistent. Expect {@code TypeUtils#getReferredType(Type)} to be referentially
* transparent.
*
* @param type Type referred by this type. For non-reference types, this is the same type.
*/
default void setCachedReferredType(Type type) {
}
/**
* Get the type referred by this type if it has been already calculated. If it has not been already calculated, it
* will return null. For non-reference types, this will return the same type. This is non-blocking and will
* eventually become consistent. Expect {@code TypeUtils#getReferredType(Type)} to be referentially transparent.
*
* @return Referred type of the type
*/
default Type getCachedReferredType() {
return null;
}

/**
* Set the implied type for this type once it has been calculated. This must be called the first time this
* calculation is done for {@code Type#getImpliedTypeCache()} to work properly. This is non-blocking and
* will eventually become consistent. Expect {@code TypeUtils#getImpliedType(Type)} to be referentially transparent.
*
* @param type Type implied by this type. For non-intersection types, this is the same type.
*/
default void setCachedImpliedType(Type type) {
}

/**
* Get the type implied by this type if it has been already calculated. If it has not been already calculated, it
* will return null. For non-intersection types, this will return the same type. This is non-blocking and will
* eventually become consistent. Expect {@code TypeUtils#getImpliedType(Type)} to be referentially transparent.
*
* @return Implied type of the type
*/
default Type getCachedImpliedType() {
return null;
}

/**
* Get the default value of the type. This is the value of an uninitialized variable of this type.
* For value types, this is same as the value get from {@code BType#getInitValue()}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,17 @@ public static boolean isSameType(Type sourceType, Type targetType) {
* @return the referred type if provided with a type reference type, else returns the original type
*/
public static Type getReferredType(Type type) {
Type referredType = type.getCachedReferredType();
if (referredType != null) {
return referredType;
}
if (type.getTag() == TypeTags.TYPE_REFERENCED_TYPE_TAG) {
return getReferredType(((ReferenceType) type).getReferredType());
referredType = getReferredType(((ReferenceType) type).getReferredType());
} else {
referredType = type;
}
return type;
type.setCachedReferredType(referredType);
return referredType;
}

/**
Expand All @@ -167,12 +174,18 @@ public static Type getReferredType(Type type) {
* else returns the original type
*/
public static Type getImpliedType(Type type) {
Type impliedType = type.getCachedImpliedType();
if (impliedType != null) {
return impliedType;
}
type = getReferredType(type);

if (type.getTag() == TypeTags.INTERSECTION_TAG) {
return getImpliedType(((IntersectionType) type).getEffectiveType());
impliedType = getImpliedType(((IntersectionType) type).getEffectiveType());
} else {
impliedType = type;
}

return type;
type.setCachedImpliedType(impliedType);
return impliedType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public abstract class BType implements Type {
protected Module pkg;
protected Class<? extends Object> valueClass;
private int hashCode;
private Type cachedReferredType = null;
private Type cachedImpliedType = null;

protected BType(String typeName, Module pkg, Class<? extends Object> valueClass) {
this.typeName = typeName;
Expand Down Expand Up @@ -195,4 +197,19 @@ public long getFlags() {
return 0;
}

public void setCachedReferredType(Type type) {
this.cachedReferredType = type;
}

public Type getCachedReferredType() {
return this.cachedReferredType;
}

public void setCachedImpliedType(Type type) {
this.cachedImpliedType = type;
}

public Type getCachedImpliedType() {
return this.cachedImpliedType;
}
}

0 comments on commit b5c6a95

Please sign in to comment.