From e453cffbfe4dadaae4fa691f84a1e3a9ed91280d Mon Sep 17 00:00:00 2001 From: Heshan Padmasiri Date: Sun, 6 Oct 2024 11:12:29 +0530 Subject: [PATCH] Avoid duplicate record type creation --- .../runtime/api/creators/TypeCreator.java | 39 ++++++++++++++++++- .../runtime/internal/types/BRecordType.java | 3 ++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/creators/TypeCreator.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/creators/TypeCreator.java index 7a7c51dc4ab8..365f252b2463 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/creators/TypeCreator.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/creators/TypeCreator.java @@ -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. @@ -58,6 +59,8 @@ */ public final class TypeCreator { + private static final Map registeredRecordTypes = new ConcurrentHashMap<>(); + /** * Creates a new array type with given element type. * @@ -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); } @@ -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 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); } @@ -520,4 +530,29 @@ public static FiniteType createFiniteType(String typeName, Set 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; + } + } } diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/types/BRecordType.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/types/BRecordType.java index 23ba31d58d14..2640dc96dd1e 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/types/BRecordType.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/types/BRecordType.java @@ -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; @@ -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); } /** @@ -123,6 +125,7 @@ public BRecordType(String typeName, Module pkg, long flags, Map f this.fields = fields; } this.internalName = typeName; + TypeCreator.registerRecordType(this); } private Map getReadOnlyFields(Map fields) {