Skip to content

Commit

Permalink
Use basic types to avoid unwanted type checks
Browse files Browse the repository at this point in the history
  • Loading branch information
heshanpadmasiri committed Feb 5, 2025
1 parent a3f67fc commit 371299f
Show file tree
Hide file tree
Showing 33 changed files with 179 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,10 @@ public static boolean isSubType(Context cx, SemType t1, SemType t2) {
return res;
}

public static SemType widenToBasicType(SemType t) {
return SemType.from(t.all() | t.some());
}

public static boolean isSubtypeSimple(SemType t1, SemType t2) {
assert t1 != null && t2 != null;
int bits = t1.all() | t1.some();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ public static SemType tryInto(Context cx, Type type) {
return (SemType) type;
}

public static SemType basicType(Type type) {
if (type instanceof MutableSemType mutableSemType) {
return mutableSemType.basicType();
}
return (SemType) type;
}

@Override
public String toString() {
return SemTypeHelper.stringRepr(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
import static io.ballerina.runtime.api.types.PredefinedTypes.TYPE_INT_UNSIGNED_32;
import static io.ballerina.runtime.api.types.PredefinedTypes.TYPE_INT_UNSIGNED_8;
import static io.ballerina.runtime.api.types.PredefinedTypes.TYPE_NULL;
import static io.ballerina.runtime.api.types.semtype.Core.widenToBasicType;
import static io.ballerina.runtime.api.utils.TypeUtils.getImpliedType;
import static io.ballerina.runtime.internal.utils.CloneUtils.getErrorMessage;

Expand Down Expand Up @@ -628,10 +629,23 @@ private static boolean isSubType(Context cx, Type source, Type target) {
target instanceof CacheableTypeDescriptor targetCacheableType) {
return isSubTypeWithCache(cx, sourceCacheableType, targetCacheableType);
}
// Try avoiding type check using basic type
return isSubTypeInner(cx, source, target);
}

private static boolean isSubTypeInner(Context cx, Type source, Type target) {
SemType sourceBasicType = widenToBasicType(SemType.basicType(source));
SemType targetBasicType = widenToBasicType(SemType.basicType(target));
if (Core.isSubtypeSimple(sourceBasicType, targetBasicType)) {
SemType sourceSemType = SemType.tryInto(cx, source);
SemType targetSemType = SemType.tryInto(cx, target);
return Core.isSubType(cx, sourceSemType, targetSemType);
}
assert !test(cx, source, target);
return false;
}

private static boolean test(Context cx, Type source, Type target) {
SemType sourceSemType = SemType.tryInto(cx, source);
SemType targetSemType = SemType.tryInto(cx, target);
return Core.isSubType(cx, sourceSemType, targetSemType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ public void setIntersectionType(IntersectionType intersectionType) {
this.intersectionType = intersectionType;
}

@Override
public SemType basicType() {
return Builder.getAnyType();
}
}

private static SemType pickSemType(boolean readonly) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.ballerina.runtime.api.types.IntersectionType;
import io.ballerina.runtime.api.types.Type;
import io.ballerina.runtime.api.types.TypeTags;
import io.ballerina.runtime.api.types.semtype.Builder;
import io.ballerina.runtime.api.types.semtype.Context;
import io.ballerina.runtime.api.types.semtype.Env;
import io.ballerina.runtime.api.types.semtype.SemType;
Expand Down Expand Up @@ -248,6 +249,11 @@ public SemType createSemType(Context cx) {
return getSemTypePart(env, ld, size, tryInto(cx, getElementType()), mut);
}

@Override
public SemType basicType() {
return Builder.getListType();
}

private SemType getSemTypePart(Env env, ListDefinition defn, int size, SemType elementType,
CellAtomicType.CellMutability mut) {
if (size == -1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,10 @@ public boolean isReadOnly() {
public StructuredLookupKey getStructuredLookupKey() {
return null;
}

@Override
public SemType basicType() {
return Builder.getBooleanType();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,10 @@ public BType clone() {
public StructuredLookupKey getStructuredLookupKey() {
return null;
}

@Override
public SemType basicType() {
return Builder.getIntType();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,10 @@ public BType clone() {
public StructuredLookupKey getStructuredLookupKey() {
return null;
}

@Override
public SemType basicType() {
return Builder.getDecimalType();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ public SemType createSemType(Context cx) {
return distinctIdSupplier.get().stream().map(ErrorUtils::errorDistinct).reduce(err, Core::intersect);
}

@Override
public SemType basicType() {
return Builder.getErrorType();
}

private void initializeDistinctIdSupplierIfNeeded(Context cx) {
if (distinctIdSupplier == null) {
synchronized (this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,12 @@ public SemType createSemType(Context cx) {
.reduce(Builder.getNeverType(), Core::union);
}

@Override
public SemType basicType() {
return this.valueSpace.stream().map(TypeChecker::getType).map(SemType::basicType).reduce(Builder.getNeverType(),
Core::union);
}

@Override
public StructuredLookupKey getStructuredLookupKey() {
return lookupKey;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,10 @@ public boolean isReadOnly() {
public StructuredLookupKey getStructuredLookupKey() {
return null;
}

@Override
public SemType basicType() {
return Builder.getFloatType();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,11 @@ public SemType createSemType(Context cx) {
return fd.define(env, paramType, returnType, getQualifiers());
}

@Override
public SemType basicType() {
return Builder.getFunctionType();
}

private SemType getTopType(Context cx) {
if (SymbolFlags.isFlagOn(flags, SymbolFlags.ISOLATED)) {
return createIsolatedTop(cx.env);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ public SemType createSemType(Context cx) {
return FutureUtils.futureContaining(cx.env, tryInto(cx, constraint));
}

@Override
public SemType basicType() {
return Builder.getFutureType();
}

@Override
protected boolean isDependentlyTypedInner(Set<MayBeDependentType> visited) {
return constraint instanceof MayBeDependentType constraintType && constraintType.isDependentlyTyped(visited);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.ballerina.runtime.api.types.TypeTags;
import io.ballerina.runtime.api.types.semtype.Builder;
import io.ballerina.runtime.api.types.semtype.ConcurrentLazySupplier;
import io.ballerina.runtime.api.types.semtype.SemType;
import io.ballerina.runtime.internal.types.semtype.StructuredLookupKey;
import io.ballerina.runtime.internal.values.RefValue;

Expand Down Expand Up @@ -87,5 +88,9 @@ public StructuredLookupKey getStructuredLookupKey() {
return null;
}

@Override
public SemType basicType() {
return Builder.getHandleType();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ public BType clone() {
public StructuredLookupKey getStructuredLookupKey() {
return null;
}

@Override
public SemType basicType() {
return Builder.getIntType();
}
}

private static final class IntegerTypeCache {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,11 @@ public SemType createSemType(Context cx) {
return createSemTypeInner(cx, type -> SemType.tryInto(cx, type));
}

@Override
public SemType basicType() {
return constituentTypes.stream().map(SemType::basicType).reduce(Core::intersect).orElseThrow();
}

@Override
protected boolean isDependentlyTypedInner(Set<MayBeDependentType> visited) {
return constituentTypes.stream().filter(each -> each instanceof MayBeDependentType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import io.ballerina.runtime.api.Module;
import io.ballerina.runtime.api.types.IteratorType;
import io.ballerina.runtime.api.types.TypeTags;
import io.ballerina.runtime.api.types.semtype.Builder;
import io.ballerina.runtime.api.types.semtype.SemType;
import io.ballerina.runtime.api.types.semtype.TypeCheckCacheKey;
import io.ballerina.runtime.internal.types.semtype.StructuredLookupKey;
import io.ballerina.runtime.internal.types.semtype.UniqueLookupKey;
Expand Down Expand Up @@ -58,4 +60,9 @@ public int getTag() {
public StructuredLookupKey getStructuredLookupKey() {
return lookupKey;
}

@Override
public SemType basicType() {
return Builder.getObjectType();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ public SemType createSemType(Context cx) {
return createSemTypeInner(env, md, tryInto(cx, getConstrainedType()), mut);
}

@Override
public SemType basicType() {
return Builder.getMappingType();
}

@Override
public void resetSemType() {
defn.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,10 @@ public boolean isReadOnly() {
public StructuredLookupKey getStructuredLookupKey() {
return null;
}

@Override
public SemType basicType() {
return Builder.getNilType();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,11 @@ public final SemType createSemType(Context cx) {
return distinctIdSupplier.get().stream().map(ObjectDefinition::distinct).reduce(innerType, Core::intersect);
}

@Override
public SemType basicType() {
return Builder.getObjectType();
}

private static boolean skipField(Set<String> seen, String name) {
if (name.startsWith("$")) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ public SemType createSemType(Context cx) {
return SemType.tryInto(cx, this.paramValueType);
}

@Override
public SemType basicType() {
return SemType.basicType(this.paramValueType);
}

@Override
protected boolean isDependentlyTypedInner(Set<MayBeDependentType> visited) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.ballerina.runtime.api.types.TypeTags;
import io.ballerina.runtime.api.types.semtype.Builder;
import io.ballerina.runtime.api.types.semtype.ConcurrentLazySupplier;
import io.ballerina.runtime.api.types.semtype.SemType;
import io.ballerina.runtime.internal.types.semtype.StructuredLookupKey;
import io.ballerina.runtime.internal.values.RefValue;

Expand Down Expand Up @@ -71,5 +72,10 @@ public boolean isReadOnly() {
public StructuredLookupKey getStructuredLookupKey() {
return null;
}

@Override
public SemType basicType() {
return Builder.getReadonlyType();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,11 @@ public SemType createSemType(Context cx) {
return createSemTypeInner(md, env, mut(), (type) -> SemType.tryInto(cx, type));
}

@Override
public SemType basicType() {
return Builder.getMappingType();
}

private CellMutability mut() {
return isReadOnly() ? CELL_MUT_NONE : CellMutability.CELL_MUT_LIMITED;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ public SemType createSemType(Context cx) {
return sd.define(env, tryInto(cx, constraint), tryInto(cx, completionType));
}

@Override
public SemType basicType() {
return Builder.getStreamType();
}

@Override
protected boolean isDependentlyTypedInner(Set<MayBeDependentType> visited) {
return (constraint instanceof MayBeDependentType constrainedType &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,10 @@ public BType clone() {
public StructuredLookupKey getStructuredLookupKey() {
return null;
}

@Override
public SemType basicType() {
return Builder.getStringType();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ public SemType createSemType(Context cx) {
return createSemTypeWithConstraint(cx, tryInto(cx, constraint));
}

@Override
public SemType basicType() {
return Builder.getTableType();
}

private SemType createSemTypeWithConstraint(Context cx, SemType constraintType) {
SemType semType;
if (fieldNames.length > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.ballerina.runtime.api.types.TupleType;
import io.ballerina.runtime.api.types.Type;
import io.ballerina.runtime.api.types.TypeTags;
import io.ballerina.runtime.api.types.semtype.Builder;
import io.ballerina.runtime.api.types.semtype.Context;
import io.ballerina.runtime.api.types.semtype.Core;
import io.ballerina.runtime.api.types.semtype.Env;
Expand Down Expand Up @@ -350,6 +351,11 @@ public SemType createSemType(Context cx) {
return createSemTypeInner(cx, ld, SemType::tryInto, mut());
}

@Override
public SemType basicType() {
return Builder.getListType();
}

private CellAtomicType.CellMutability mut() {
return isReadOnly() ? CELL_MUT_NONE : CellAtomicType.CellMutability.CELL_MUT_LIMITED;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ public SemType createSemType(Context cx) {
return tryInto(cx, referredType);
}

@Override
public SemType basicType() {
return basicType(getReferredType());
}

@Override
protected boolean isDependentlyTypedInner(Set<MayBeDependentType> visited) {
return getReferredType() instanceof MayBeDependentType refType && refType.isDependentlyTyped(visited);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ public SemType createSemType(Context cx) {
return TypedescUtils.typedescContaining(cx.env, constraint);
}

@Override
public SemType basicType() {
return Builder.getTypeDescType();
}

@Override
protected boolean isDependentlyTypedInner(Set<MayBeDependentType> visited) {
return constraint instanceof MayBeDependentType constraintType &&
Expand Down
Loading

0 comments on commit 371299f

Please sign in to comment.