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

Reduce runtime type checker overhead #42038

Closed
Closed
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
@@ -0,0 +1,154 @@
/*
*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* /
*
*/

package io.ballerina.runtime.api;

import io.ballerina.runtime.api.types.Type;

import java.util.List;
import java.util.stream.Stream;

public record SimpleType(long all, long some) {

public SimpleType intersection(SimpleType other) {
long all = this.all & other.all;
long some = (this.some | this.all) & (other.some | other.all);
some &= ~all;
return new SimpleType(all, some);
}

public SimpleType union(SimpleType other) {
long all = this.all | other.all;
long some = this.some | other.some;
some &= ~all;
return new SimpleType(all, some);
}

public SimpleType diff(SimpleType other) {
long all = this.all & ~(other.all | other.some);
long some = (this.all | this.some) & ~(other.all);
some &= ~all;
return new SimpleType(all, some);
}

@Override
public String toString() {
Tag[] allTags =
Stream.of(Tag.values()).filter(tag -> (all & (1L << tag.ordinal())) != 0)
.toArray(Tag[]::new);
Tag[] someTags =
Stream.of(Tag.values()).filter(tag -> (some & (1L << tag.ordinal())) != 0)
.toArray(Tag[]::new);
StringBuilder sb = new StringBuilder();
sb.append("SimpleType all: {");
for (Tag tag : allTags) {
sb.append(tag).append(", ");
}
sb.append("} some: {");
for (Tag tag : someTags) {
sb.append(tag).append(", ");
}
sb.append("}");
return sb.toString();
}

public enum Tag {
NIL,
BOOLEAN,
INT,
FLOAT,
DECIMAL,
STRING,
ERROR,
TYPEDESC,
HANDLE,
FUNCTION,

// Inherently mutable
FUTURE,
STREAM,

// Selectively immutable
LIST,
MAPPING,
TABLE,
XML,
OBJECT,
CELL
}

public static class Builder {

// Helper values to construct types
public static final long NONE = 0;
public static final long ALL = (1L << (Tag.values().length + 1)) - 1;

public static long basicTypeUnionBitset(Tag... basicTypes) {
long bits = 0;
for (Tag basicType : basicTypes) {
bits |= (1L << basicType.ordinal());
}
return bits;
}

public static long basicTypeBitset(Tag basicType) {
return 1L << basicType.ordinal();
}

// TODO: container is not a good name?
public static SimpleType createConstrainedType(Type constraint, Tag baseType) {
SimpleType constraintSimpleType = constraint.getSimpleType();
if (constraintSimpleType.all == ALL) {
// no constraint mean it is the base type
return new SimpleType(Builder.basicTypeBitset(baseType), Builder.NONE);
}
return new SimpleType(Builder.NONE, Builder.basicTypeBitset(baseType));
}

public static long except(Tag... basicTypes) {
long bits = ALL;
for (Tag basicType : basicTypes) {
bits &= ~(1L << basicType.ordinal());
}
return bits;
}

public static SimpleType asSome(SimpleType type) {
return new SimpleType(NONE, type.all | type.some);
}

public static SimpleType intersection(List<Type> types) {
if (types.isEmpty()) {
return new SimpleType(NONE, NONE);
}
return types.stream().skip(1).map(Type::getSimpleType)
.reduce(types.get(0).getSimpleType(), SimpleType::intersection);
}

public static SimpleType union(List<Type> types) {
if (types.isEmpty()) {
return new SimpleType(NONE, NONE);
}
return types.stream().skip(1).map(Type::getSimpleType)
.reduce(types.get(0).getSimpleType(), SimpleType::union);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package io.ballerina.runtime.api.types;

import io.ballerina.runtime.api.Module;
import io.ballerina.runtime.api.SimpleType;

/**
* {@code Type} represents a type in Ballerina.
Expand All @@ -31,6 +32,8 @@
*/
public interface Type {

SimpleType getSimpleType();

/**
* Get the default value of the type. This is the value of an uninitialized variable of this type.
* For value types, this is same as the value get from {@code BType#getInitValue()}.
Expand Down Expand Up @@ -91,4 +94,9 @@ public interface Type {
void setImmutableType(IntersectionType immutableType);

Module getPkg();

// TODO: add documentation
// void setImpliedType(Type impliedType);
//
// Type getImpliedType();
}
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,16 @@ public static Type getReferredType(Type type) {
* else returns the original type
*/
public static Type getImpliedType(Type type) {
type = getReferredType(type);
// Type memo = type.getImpliedType();
// if (memo != null) {
// return memo;
// }
Type result = getReferredType(type);

if (type.getTag() == TypeTags.INTERSECTION_TAG) {
return getImpliedType(((IntersectionType) type).getEffectiveType());
if (result.getTag() == TypeTags.INTERSECTION_TAG) {
result = getImpliedType(((IntersectionType) result).getEffectiveType());
}

return type;
// type.setImpliedType(result);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package io.ballerina.runtime.api.values;

import io.ballerina.runtime.api.SimpleType;
import io.ballerina.runtime.api.types.Type;

import java.util.Map;
Expand All @@ -30,6 +31,10 @@
*/
public interface BValue {

// TODO: remove this default implementation once all standard library classes are fixed
default SimpleType getSimpleType() {
return getType().getSimpleType();
}
/**
* Method to perform a deep copy, recursively copying all structural values and their members.
*
Expand Down
Loading
Loading