From f0875b774a9e1e89461148f513f64b27ae0751c2 Mon Sep 17 00:00:00 2001 From: warunalakshitha Date: Wed, 11 Dec 2024 14:07:09 +0530 Subject: [PATCH 1/5] Improve perf of array add --- .../internal/values/ArrayValueImpl.java | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/ArrayValueImpl.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/ArrayValueImpl.java index 6413e10fd648..7dbc05e78e81 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/ArrayValueImpl.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/ArrayValueImpl.java @@ -614,7 +614,6 @@ public void convertStringAndAddRefValue(long index, Object value) { } public void addRefValue(long index, Object value) { - Type type = TypeChecker.getType(value); switch (this.elementReferredType.getTag()) { case TypeTags.BOOLEAN_TAG: prepareForAdd(index, value, booleanValues.length); @@ -662,27 +661,31 @@ public void setArrayRefTypeForcefully(ArrayType type, int size) { public void addInt(long index, long value) { if (intValues != null) { - prepareForAdd(index, value, intValues.length); + prepareForAddWithoutTypeCheck(index, intValues.length); intValues[(int) index] = value; return; } - - prepareForAdd(index, value, byteValues.length); + if (!TypeChecker.isByteLiteral(value)) { + throw ErrorCreator.createError(getModulePrefixedReason(ARRAY_LANG_LIB, + INHERENT_TYPE_VIOLATION_ERROR_IDENTIFIER), ErrorHelper.getErrorDetails( + ErrorCodes.INCOMPATIBLE_TYPE, this.elementType, TypeChecker.getType(value))); + } + prepareForAddWithoutTypeCheck(index, byteValues.length); byteValues[(int) index] = (byte) ((Long) value).intValue(); } private void addBoolean(long index, boolean value) { - prepareForAdd(index, value, booleanValues.length); + prepareForAddWithoutTypeCheck(index, booleanValues.length); booleanValues[(int) index] = value; } private void addByte(long index, byte value) { - prepareForAdd(index, value, byteValues.length); + prepareForAddWithoutTypeCheck(index, byteValues.length); byteValues[(int) index] = value; } private void addFloat(long index, double value) { - prepareForAdd(index, value, floatValues.length); + prepareForAddWithoutTypeCheck(index, floatValues.length); floatValues[(int) index] = value; } @@ -692,7 +695,7 @@ private void addString(long index, String value) { } private void addBString(long index, BString value) { - prepareForAdd(index, value, bStringValues.length); + prepareForAddWithoutTypeCheck(index, bStringValues.length); bStringValues[(int) index] = value; } From 7e5f313649853949f817d4dfdddfa77a5048debd Mon Sep 17 00:00:00 2001 From: warunalakshitha Date: Wed, 11 Dec 2024 14:07:44 +0530 Subject: [PATCH 2/5] Improve perf of type checker for equal types --- .../main/java/io/ballerina/runtime/internal/TypeChecker.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/TypeChecker.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/TypeChecker.java index b898315b7878..25f62dbef3ad 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/TypeChecker.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/TypeChecker.java @@ -257,12 +257,15 @@ public static boolean anyToJBoolean(Object sourceVal) { * @return true if the value belongs to the given type, false otherwise */ public static boolean checkIsType(Object sourceVal, Type targetType) { - Context cx = context(); Type sourceType = getType(sourceVal); + if (sourceType == targetType || (sourceType.getTag() == targetType.getTag() && sourceType.equals(targetType))) { + return true; + } if (isSubType(sourceType, targetType)) { return true; } SemType sourceSemType = SemType.tryInto(sourceType); + Context cx = context(); return couldInherentTypeBeDifferent(sourceSemType) && isSubTypeWithInherentType(cx, sourceVal, SemType.tryInto(targetType)); } From 0ccf4215c827eae9e20fa170d300cb135f33eba9 Mon Sep 17 00:00:00 2001 From: warunalakshitha Date: Wed, 11 Dec 2024 14:32:46 +0530 Subject: [PATCH 3/5] Remove redundant equal check in TypeCheckCache --- .../io/ballerina/runtime/api/types/semtype/TypeCheckCache.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/semtype/TypeCheckCache.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/semtype/TypeCheckCache.java index cf9bc820da2a..a3ef0861072f 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/semtype/TypeCheckCache.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/types/semtype/TypeCheckCache.java @@ -29,9 +29,6 @@ public TypeCheckCache(T owner) { } public Optional cachedTypeCheckResult(T other) { - if (other.equals(owner)) { - return Optional.of(true); - } return Optional.ofNullable(cachedResults.get(other)); } From 2b57fa411d18605d8ff7c3102a650430cb38e8dd Mon Sep 17 00:00:00 2001 From: warunalakshitha Date: Thu, 12 Dec 2024 14:29:05 +0530 Subject: [PATCH 4/5] Fix test with int sub types --- .../ballerina/runtime/internal/values/ArrayValueImpl.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/ArrayValueImpl.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/ArrayValueImpl.java index 7dbc05e78e81..2fec0ee525e1 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/ArrayValueImpl.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/ArrayValueImpl.java @@ -661,7 +661,12 @@ public void setArrayRefTypeForcefully(ArrayType type, int size) { public void addInt(long index, long value) { if (intValues != null) { - prepareForAddWithoutTypeCheck(index, intValues.length); + if (elementType == PredefinedTypes.TYPE_INT) { + prepareForAddWithoutTypeCheck(index, intValues.length); + } else { + // We need type checker for int subtypes + prepareForAdd(index, value, intValues.length); + } intValues[(int) index] = value; return; } From 64f300525a94102a575151e7ad4fe0c4a62f34cf Mon Sep 17 00:00:00 2001 From: warunalakshitha Date: Mon, 16 Dec 2024 10:09:59 +0530 Subject: [PATCH 5/5] Fix bstring array tests --- .../runtime/internal/TypeChecker.java | 8 ++++---- .../internal/values/ArrayValueImpl.java | 20 +++++++++++-------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/TypeChecker.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/TypeChecker.java index 25f62dbef3ad..6362696460b7 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/TypeChecker.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/TypeChecker.java @@ -258,9 +258,6 @@ public static boolean anyToJBoolean(Object sourceVal) { */ public static boolean checkIsType(Object sourceVal, Type targetType) { Type sourceType = getType(sourceVal); - if (sourceType == targetType || (sourceType.getTag() == targetType.getTag() && sourceType.equals(targetType))) { - return true; - } if (isSubType(sourceType, targetType)) { return true; } @@ -604,7 +601,10 @@ private static boolean isSubTypeWithInherentType(Context cx, Object sourceValue, .orElse(false); } - private static synchronized boolean isSubType(Type source, Type target) { + private static boolean isSubType(Type source, Type target) { + if (source == target || (source.getTag() == target.getTag() && source.equals(target))) { + return true; + } if (source instanceof CacheableTypeDescriptor sourceCacheableType && target instanceof CacheableTypeDescriptor targetCacheableType) { return isSubTypeWithCache(sourceCacheableType, targetCacheableType); diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/ArrayValueImpl.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/ArrayValueImpl.java index 2fec0ee525e1..8325e6781c78 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/ArrayValueImpl.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/ArrayValueImpl.java @@ -660,22 +660,21 @@ public void setArrayRefTypeForcefully(ArrayType type, int size) { } public void addInt(long index, long value) { + Type sourceType = TypeChecker.getType(value); if (intValues != null) { - if (elementType == PredefinedTypes.TYPE_INT) { + if (sourceType == this.elementType) { prepareForAddWithoutTypeCheck(index, intValues.length); } else { - // We need type checker for int subtypes prepareForAdd(index, value, intValues.length); } intValues[(int) index] = value; return; } - if (!TypeChecker.isByteLiteral(value)) { - throw ErrorCreator.createError(getModulePrefixedReason(ARRAY_LANG_LIB, - INHERENT_TYPE_VIOLATION_ERROR_IDENTIFIER), ErrorHelper.getErrorDetails( - ErrorCodes.INCOMPATIBLE_TYPE, this.elementType, TypeChecker.getType(value))); + if (sourceType == this.elementType) { + prepareForAddWithoutTypeCheck(index, byteValues.length); + } else { + prepareForAdd(index, value, byteValues.length); } - prepareForAddWithoutTypeCheck(index, byteValues.length); byteValues[(int) index] = (byte) ((Long) value).intValue(); } @@ -700,7 +699,12 @@ private void addString(long index, String value) { } private void addBString(long index, BString value) { - prepareForAddWithoutTypeCheck(index, bStringValues.length); + Type sourceType = TypeChecker.getType(value); + if (sourceType == this.elementType) { + prepareForAddWithoutTypeCheck(index, bStringValues.length); + } else { + prepareForAdd(index, value, bStringValues.length); + } bStringValues[(int) index] = value; }