Skip to content

Commit

Permalink
Avoid necessary caching in TypeCreator
Browse files Browse the repository at this point in the history
  • Loading branch information
heshanpadmasiri committed Oct 6, 2024
1 parent dca84d0 commit 019797b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,13 @@
import io.ballerina.runtime.internal.types.BStreamType;
import io.ballerina.runtime.internal.types.BTableType;
import io.ballerina.runtime.internal.types.BTupleType;
import io.ballerina.runtime.internal.types.BType;
import io.ballerina.runtime.internal.types.BUnionType;
import io.ballerina.runtime.internal.types.BXmlType;

import java.util.Arrays;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.function.Function;
import java.util.function.Supplier;

/**
* Class @{@link TypeCreator} provides APIs to create ballerina type instances.
Expand All @@ -63,16 +58,6 @@
*/
public final class TypeCreator {

private static final Map<TypeMemoKey, BTupleType> tupleTypeMemo = new WeakHashMap<>(100);
private static final Map<TypeMemoKey, BMapType> mapTypeMemo = new WeakHashMap<>(100);
private static final Map<TypeMemoKey, BRecordType> recordTypeMemo = new WeakHashMap<>(100);
private static final Map<TypeMemoKey, BObjectType> objectTypeMemo = new WeakHashMap<>(100);
private static final Map<TypeMemoKey, BStreamType> streamTypeMemo = new WeakHashMap<>(100);
private static final Map<TypeMemoKey, BUnionType> unionTypeMemo = new WeakHashMap<>(100);
private static final Map<TypeMemoKey, BErrorType> errorTypeMemo = new WeakHashMap<>(100);
private static final Map<TypeMemoKey, BXmlType> xmlTypeMemo = new WeakHashMap<>(100);
private static final Map<TypeMemoKey, BJsonType> jsonTypeMemo = new WeakHashMap<>(100);
private static final Map<Type, BMapType> mapTypeCache = new IdentityHashMap<>();
/**
* Creates a new array type with given element type.
*
Expand Down Expand Up @@ -162,7 +147,7 @@ public static TupleType createTupleType(List<Type> typeList, Type restType, int
* @return the new tuple type
*/
public static TupleType createTupleType(List<Type> typeList, Type restType,
int typeFlags, boolean isCyclic, boolean readonly) {
int typeFlags, boolean isCyclic, boolean readonly) {
return new BTupleType(typeList, restType, typeFlags, isCyclic, readonly);
}

Expand All @@ -177,20 +162,18 @@ public static TupleType createTupleType(List<Type> typeList, Type restType,
* @return the new tuple type
*/
public static TupleType createTupleType(String name, Module pkg,
int typeFlags, boolean isCyclic, boolean readonly) {
TypeMemoKey key = new TypeMemoKey(name, pkg);
return tupleTypeMemo.computeIfAbsent(key,
createMappingFn(() -> new BTupleType(name, pkg, typeFlags, isCyclic, readonly)));
int typeFlags, boolean isCyclic, boolean readonly) {
return new BTupleType(name, pkg, typeFlags, isCyclic, readonly);
}

/**
* Create a {@code MapType} which represents the map type.
*
* @param constraint constraint type which particular map is bound to.
* @return the new map type
*/
* Create a {@code MapType} which represents the map type.
*
* @param constraint constraint type which particular map is bound to.
* @return the new map type
*/
public static MapType createMapType(Type constraint) {
return mapTypeCache.computeIfAbsent(constraint, (ignore) -> new BMapType(constraint));
return new BMapType(constraint);
}

/**
Expand All @@ -213,9 +196,7 @@ public static MapType createMapType(Type constraint, boolean readonly) {
* @return the new map type
*/
public static MapType createMapType(String typeName, Type constraint, Module module) {
TypeMemoKey key = new TypeMemoKey(typeName, module);
return mapTypeMemo.computeIfAbsent(key,
createMappingFn(() -> new BMapType(typeName, constraint, module)));
return new BMapType(typeName, constraint, module);
}

/**
Expand All @@ -228,9 +209,7 @@ public static MapType createMapType(String typeName, Type constraint, Module mod
* @return the new map type
*/
public static MapType createMapType(String typeName, Type constraint, Module module, boolean readonly) {
TypeMemoKey key = new TypeMemoKey(typeName, module);
return mapTypeMemo.computeIfAbsent(key,
createMappingFn(() -> new BMapType(typeName, constraint, module, readonly)));
return new BMapType(typeName, constraint, module, readonly);
}

/**
Expand All @@ -245,9 +224,7 @@ public static MapType createMapType(String typeName, Type constraint, Module mod
*/
public static RecordType createRecordType(String typeName, Module module, long flags, boolean sealed,
int typeFlags) {
TypeMemoKey key = new TypeMemoKey(typeName, module);
return recordTypeMemo.computeIfAbsent(key,
createMappingFn(() -> new BRecordType(typeName, typeName, module, flags, sealed, typeFlags)));
return new BRecordType(typeName, typeName, module, flags, sealed, typeFlags);
}

/**
Expand All @@ -263,7 +240,8 @@ public static RecordType createRecordType(String typeName, Module module, long f
* @return the new record type
*/
public static RecordType createRecordType(String typeName, Module module, long flags, Map<String, Field> fields,
Type restFieldType, boolean sealed, int typeFlags) {
Type restFieldType,
boolean sealed, int typeFlags) {
return new BRecordType(typeName, module, flags, fields, restFieldType, sealed, typeFlags);
}

Expand All @@ -276,8 +254,7 @@ public static RecordType createRecordType(String typeName, Module module, long f
* @return the new object type
*/
public static ObjectType createObjectType(String typeName, Module module, long flags) {
TypeMemoKey key = new TypeMemoKey(typeName, module);
return objectTypeMemo.computeIfAbsent(key, createMappingFn(() -> new BObjectType(typeName, module, flags)));
return new BObjectType(typeName, module, flags);
}

/**
Expand All @@ -302,9 +279,7 @@ public static StreamType createStreamType(Type constraint, Type completionType)
*/
public static StreamType createStreamType(String typeName, Type constraint,
Type completionType, Module modulePath) {
TypeMemoKey key = new TypeMemoKey(typeName, modulePath);
return streamTypeMemo.computeIfAbsent(key,
createMappingFn(() -> new BStreamType(typeName, constraint, completionType, modulePath)));
return new BStreamType(typeName, constraint, completionType, modulePath);
}

/**
Expand All @@ -330,9 +305,7 @@ public static StreamType createStreamType(Type constraint) {
*/
@Deprecated
public static StreamType createStreamType(String typeName, Type completionType, Module modulePath) {
TypeMemoKey key = new TypeMemoKey(typeName, modulePath);
return streamTypeMemo.computeIfAbsent(key,
createMappingFn(() -> new BStreamType(typeName, completionType, modulePath)));
return new BStreamType(typeName, completionType, modulePath);
}

/**
Expand Down Expand Up @@ -402,9 +375,7 @@ public static UnionType createUnionType(List<Type> memberTypes, int typeFlags, b
*/
public static UnionType createUnionType(List<Type> memberTypes, String name, Module pkg, int typeFlags,
boolean isCyclic, long flags) {
TypeMemoKey key = new TypeMemoKey(name, pkg);
return unionTypeMemo.computeIfAbsent(key,
createMappingFn(() -> new BUnionType(memberTypes, name, pkg, typeFlags, isCyclic, flags)));
return new BUnionType(memberTypes, name, pkg, typeFlags, isCyclic, flags);
}

/**
Expand All @@ -415,8 +386,7 @@ public static UnionType createUnionType(List<Type> memberTypes, String name, Mod
* @return the new error type
*/
public static ErrorType createErrorType(String typeName, Module module) {
TypeMemoKey key = new TypeMemoKey(typeName, module);
return errorTypeMemo.computeIfAbsent(key, createMappingFn(() -> new BErrorType(typeName, module)));
return new BErrorType(typeName, module);
}

/**
Expand All @@ -428,8 +398,7 @@ public static ErrorType createErrorType(String typeName, Module module) {
* @return the new error type
*/
public static ErrorType createErrorType(String typeName, Module module, Type detailType) {
TypeMemoKey key = new TypeMemoKey(typeName, module);
return errorTypeMemo.computeIfAbsent(key, createMappingFn(() -> new BErrorType(typeName, module, detailType)));
return new BErrorType(typeName, module, detailType);
}

/**
Expand Down Expand Up @@ -488,8 +457,7 @@ public static TableType createTableType(Type constraint, boolean readonly) {
* @return new xml type
*/
public static XmlType createXMLType(String typeName, Type constraint, Module module) {
TypeMemoKey key = new TypeMemoKey(typeName, module);
return xmlTypeMemo.computeIfAbsent(key, createMappingFn(() -> new BXmlType(typeName, constraint, module)));
return new BXmlType(typeName, constraint, module);
}

/**
Expand All @@ -502,8 +470,7 @@ public static XmlType createXMLType(String typeName, Type constraint, Module mod
* @return new xml type
*/
public static XmlType createXMLType(String typeName, Module module, int tag, boolean readonly) {
TypeMemoKey key = new TypeMemoKey(typeName, module);
return xmlTypeMemo.computeIfAbsent(key, createMappingFn(() -> new BXmlType(typeName, module, tag, readonly)));
return new BXmlType(typeName, module, tag, readonly);
}

/**
Expand All @@ -526,8 +493,7 @@ public static XmlType createXMLType(Type constraint, boolean readonly) {
* @return new xml type
*/
public static JsonType createJSONType(String typeName, Module module, boolean readonly) {
TypeMemoKey key = new TypeMemoKey(typeName, module);
return jsonTypeMemo.computeIfAbsent(key, createMappingFn(() -> new BJsonType(typeName, module, readonly)));
return new BJsonType(typeName, module, readonly);
}

/**
Expand All @@ -554,22 +520,4 @@ public static FiniteType createFiniteType(String typeName, Set<Object> values, i

private TypeCreator() {
}

public record TypeMemoKey(String typeName, Module module) {

}

public static void registerRecordType(String typeName, Module module, BRecordType recordType) {
TypeMemoKey key = new TypeMemoKey(typeName, module);
recordTypeMemo.put(key, recordType);
recordType.setLookupKey(key);
}

private static <E extends BType> Function<TypeMemoKey, E> createMappingFn(Supplier<E> innerSuppler) {
return (key) -> {
E bType = innerSuppler.get();
bType.setLookupKey(key);
return bType;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import io.ballerina.identifier.Utils;
import io.ballerina.runtime.api.Module;
import io.ballerina.runtime.api.TypeTags;
import io.ballerina.runtime.api.creators.TypeCreator;
import io.ballerina.runtime.api.creators.ValueCreator;
import io.ballerina.runtime.api.flags.SymbolFlags;
import io.ballerina.runtime.api.flags.TypeFlags;
Expand Down Expand Up @@ -95,7 +94,6 @@ public BRecordType(String typeName, String internalName, Module pkg, long flags,
this.sealed = sealed;
this.typeFlags = typeFlags;
this.readonly = SymbolFlags.isFlagOn(flags, SymbolFlags.READONLY);
registerWithTypeCreator();
}

/**
Expand Down Expand Up @@ -125,13 +123,6 @@ public BRecordType(String typeName, Module pkg, long flags, Map<String, Field> f
this.fields = fields;
}
this.internalName = typeName;
registerWithTypeCreator();
}

private void registerWithTypeCreator() {
if (this.typeName != null && this.pkg != null) {
TypeCreator.registerRecordType(this.typeName, this.pkg, this);
}
}

private Map<String, Field> getReadOnlyFields(Map<String, Field> fields) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import io.ballerina.runtime.api.Module;
import io.ballerina.runtime.api.TypeTags;
import io.ballerina.runtime.api.creators.ErrorCreator;
import io.ballerina.runtime.api.creators.TypeCreator;
import io.ballerina.runtime.api.types.IntersectionType;
import io.ballerina.runtime.api.types.Type;
import io.ballerina.runtime.api.types.semtype.CacheableTypeDescriptor;
Expand Down Expand Up @@ -54,7 +53,6 @@ public abstract non-sealed class BType extends SemType
private Type cachedReferredType = null;
private Type cachedImpliedType = null;
private volatile SemType cachedSemType = null;
private TypeCreator.TypeMemoKey lookupKey = null;
private volatile TypeCheckCache<CacheableTypeDescriptor> typeCheckCache;

protected BType(String typeName, Module pkg, Class<? extends Object> valueClass) {
Expand Down Expand Up @@ -288,10 +286,6 @@ public BType clone() {
}
}

public void setLookupKey(TypeCreator.TypeMemoKey lookupKey) {
this.lookupKey = lookupKey;
}

@Override
public boolean shouldCache() {
return true;
Expand Down

0 comments on commit 019797b

Please sign in to comment.