Skip to content

Commit

Permalink
Avoid duplicate record type creation
Browse files Browse the repository at this point in the history
  • Loading branch information
heshanpadmasiri committed Oct 6, 2024
1 parent 019797b commit e453cff
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

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

private static final Map<TypeIdentifier, BRecordType> registeredRecordTypes = new ConcurrentHashMap<>();

/**
* Creates a new array type with given element type.
*
Expand Down Expand Up @@ -224,6 +227,10 @@ public static MapType createMapType(String typeName, Type constraint, Module mod
*/
public static RecordType createRecordType(String typeName, Module module, long flags, boolean sealed,
int typeFlags) {
BRecordType memo = registeredRecordType(typeName, module);
if (memo != null) {
return memo;
}
return new BRecordType(typeName, typeName, module, flags, sealed, typeFlags);
}

Expand All @@ -240,8 +247,11 @@ 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) {
BRecordType memo = registeredRecordType(typeName, module);
if (memo != null) {
return memo;
}
return new BRecordType(typeName, module, flags, fields, restFieldType, sealed, typeFlags);
}

Expand Down Expand Up @@ -520,4 +530,29 @@ public static FiniteType createFiniteType(String typeName, Set<Object> values, i

private TypeCreator() {
}

private static BRecordType registeredRecordType(String typeName, Module pkg) {
if (typeName == null || pkg == null) {
return null;
}
return registeredRecordTypes.get(new TypeIdentifier(typeName, pkg));
}

public static void registerRecordType(BRecordType recordType) {
String name = recordType.getName();
Module pkg = recordType.getPackage();
if (name == null || pkg == null) {
return;
}
TypeIdentifier typeIdentifier = new TypeIdentifier(name, pkg);
registeredRecordTypes.put(typeIdentifier, recordType);
}

public record TypeIdentifier(String typeName, Module pkg) {

public TypeIdentifier {
assert typeName != null;
assert pkg != null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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 @@ -94,6 +95,7 @@ public BRecordType(String typeName, String internalName, Module pkg, long flags,
this.sealed = sealed;
this.typeFlags = typeFlags;
this.readonly = SymbolFlags.isFlagOn(flags, SymbolFlags.READONLY);
TypeCreator.registerRecordType(this);
}

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

private Map<String, Field> getReadOnlyFields(Map<String, Field> fields) {
Expand Down

0 comments on commit e453cff

Please sign in to comment.