Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache repeated type calculations #42111

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
heshanpadmasiri marked this conversation as resolved.
Show resolved Hide resolved
/**
* 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) {
}

Check warning on line 44 in bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/Type.java

View check run for this annotation

Codecov / codecov/patch

bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/Type.java#L44

Added line #L44 was not covered by tests
/**
* 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;

Check warning on line 53 in bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/Type.java

View check run for this annotation

Codecov / codecov/patch

bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/Type.java#L53

Added line #L53 was not covered by tests
}

/**
* 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) {
}

Check warning on line 64 in bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/Type.java

View check run for this annotation

Codecov / codecov/patch

bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/Type.java#L64

Added line #L64 was not covered by tests

/**
* 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;

Check warning on line 74 in bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/Type.java

View check run for this annotation

Codecov / codecov/patch

bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/Type.java#L74

Added line #L74 was not covered by tests
}

/**
* 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;
}
}
Loading