Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance of array add operation and type checker #43701

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ public TypeCheckCache(T owner) {
}

public Optional<Boolean> cachedTypeCheckResult(T other) {
if (other.equals(owner)) {
return Optional.of(true);
}
return Optional.ofNullable(cachedResults.get(other));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ 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);
Context cx = context();
if (isSubType(cx, sourceType, targetType)) {
return true;
}
Expand Down Expand Up @@ -605,6 +605,9 @@ private static boolean isSubTypeWithInherentType(Context cx, Object sourceValue,
}

private static boolean isSubType(Context cx, 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(cx, sourceCacheableType, targetCacheableType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,6 @@
}

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);
Expand Down Expand Up @@ -661,28 +660,36 @@
}

public void addInt(long index, long value) {
Type sourceType = TypeChecker.getType(value);
if (intValues != null) {
prepareForAdd(index, value, intValues.length);
if (sourceType == this.elementType) {
prepareForAddWithoutTypeCheck(index, intValues.length);
} else {
prepareForAdd(index, value, intValues.length);
}
intValues[(int) index] = value;
return;
}

prepareForAdd(index, value, byteValues.length);
if (sourceType == this.elementType) {
prepareForAddWithoutTypeCheck(index, byteValues.length);

Check warning on line 674 in bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/ArrayValueImpl.java

View check run for this annotation

Codecov / codecov/patch

bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/ArrayValueImpl.java#L674

Added line #L674 was not covered by tests
} else {
prepareForAdd(index, value, 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;
}

Expand All @@ -692,7 +699,12 @@
}

private void addBString(long index, BString value) {
prepareForAdd(index, value, bStringValues.length);
Type sourceType = TypeChecker.getType(value);
if (sourceType == this.elementType) {
prepareForAddWithoutTypeCheck(index, bStringValues.length);

Check warning on line 704 in bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/ArrayValueImpl.java

View check run for this annotation

Codecov / codecov/patch

bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/ArrayValueImpl.java#L704

Added line #L704 was not covered by tests
} else {
prepareForAdd(index, value, bStringValues.length);
}
bStringValues[(int) index] = value;
}

Expand Down
Loading