Skip to content

Commit

Permalink
WIP: semtype flyweight
Browse files Browse the repository at this point in the history
A
  • Loading branch information
heshanpadmasiri committed Feb 24, 2025
1 parent ef28551 commit 68f099c
Show file tree
Hide file tree
Showing 44 changed files with 705 additions and 412 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com).
*
* 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.types;

import io.ballerina.runtime.api.types.semtype.SemType;
import io.ballerina.runtime.api.types.semtype.TypeCheckCache;

public record ImmutableSemTypeFlyweight(SemType semType, int typeId, TypeCheckCache typeCheckCache)
implements SemTypeFlyweight {

@Override
public void setSemType(SemType semType) {
throw new UnsupportedOperationException("Cannot mutate immutable semType flyweight");
}

@Override
public void setTypeId(int typeId) {
throw new UnsupportedOperationException("Cannot mutate immutable semType flyweight");
}

@Override
public void setTypeCheckCache(TypeCheckCache typeCheckCache) {
throw new UnsupportedOperationException("Cannot mutate immutable semType flyweight");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com).
*
* 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.types;

import io.ballerina.runtime.api.types.semtype.SemType;
import io.ballerina.runtime.api.types.semtype.TypeCheckCache;
import io.ballerina.runtime.internal.types.TypeCheckableType;

import java.util.concurrent.atomic.AtomicReference;

public class MutableSemTypeFlyweight implements SemTypeFlyweight {

AtomicReference<SemType> ref = new AtomicReference<>();
int typeId = -1;
TypeCheckCache typeCheckCache = null;

@Override
public SemType semType() {
return ref.get();
}

@Override
public int typeId() {
return typeId;
}

@Override
public TypeCheckCache typeCheckCache() {
return typeCheckCache;
}

@Override
public void setSemType(SemType semType) {
if (semType instanceof TypeCheckableType wrapper) {
ref.set(wrapper.semTypeFlyweight.semType());
} else {
ref.set(semType);
}
}

@Override
public void setTypeId(int typeId) {
this.typeId = typeId;
}

@Override
public void setTypeCheckCache(TypeCheckCache typeCheckCache) {
this.typeCheckCache = typeCheckCache;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com).
*
* 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.types;

import io.ballerina.runtime.api.types.semtype.SemType;
import io.ballerina.runtime.api.types.semtype.TypeCheckCache;

public interface SemTypeFlyweight {

SemType semType();

int typeId();

TypeCheckCache typeCheckCache();

void setSemType(SemType semType);

void setTypeId(int typeId);

void setTypeCheckCache(TypeCheckCache typeCheckCache);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,14 @@
* specific language governing permissions and limitations
* under the License.
*/
package io.ballerina.runtime.api.types.semtype;

/**
* Abstraction on top of bit set used to represent union of "all" of a given basic type.
*
* @since 2201.12.0
*/
public sealed class BasicTypeBitSet permits SemType {

private int all;

public BasicTypeBitSet(int all) {
this.all = all;
}
package io.ballerina.runtime.api.types.semtype;

protected void setAll(int all) {
this.all = all;
}
public interface BasicTypeBitSet {

public final int all() {
return all;
}
int all();

public BasicTypeBitSet union(BasicTypeBitSet basicTypeBitSet) {
return new BasicTypeBitSet(all() | basicTypeBitSet.all());
}
BasicTypeBitSet union(BasicTypeBitSet basicTypeBitSet);

public BasicTypeBitSet intersection(BasicTypeBitSet basicTypeBitSet) {
return new BasicTypeBitSet(all() & basicTypeBitSet.all());
}
BasicTypeBitSet intersection(BasicTypeBitSet basicTypeBitSet);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com).
*
* 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.types.semtype;

/**
* Abstraction on top of bit set used to represent union of "all" of a given basic type.
*
* @since 2201.12.0
*/
public sealed class BasicTypeBitSetImpl implements BasicTypeBitSet permits SemTypeImpl {

private final int all;

public BasicTypeBitSetImpl(int all) {
this.all = all;
}

@Override
public int all() {
return all;
}

@Override
public BasicTypeBitSet union(BasicTypeBitSet basicTypeBitSet) {
return new BasicTypeBitSetImpl(all() | basicTypeBitSet.all());
}

@Override
public BasicTypeBitSet intersection(BasicTypeBitSet basicTypeBitSet) {
return new BasicTypeBitSetImpl(all() & basicTypeBitSet.all());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ public final class Builder {
private static final SemType INHERENTLY_IMMUTABLE = SemType.from(VT_INHERENTLY_IMMUTABLE);

private static final SemType INNER = getBasicTypeUnion(VAL.all() | from(BasicTypeCode.BT_UNDEF).all());
private static final SemType ANY = getBasicTypeUnion(BasicTypeCode.VT_MASK & ~(1 << BasicTypeCode.BT_ERROR.code()));
private static final SemType ANY =
getBasicTypeUnion(BasicTypeCode.VT_MASK & ~(1 << BasicTypeCode.BT_ERROR.code()));
private static final SemType SIMPLE_OR_STRING =
getBasicTypeUnion((1 << BasicTypeCode.BT_NIL.code())
| (1 << BasicTypeCode.BT_BOOLEAN.code())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,7 @@ public static boolean isNever(SemType t) {
}

public static boolean isSubType(Context cx, SemType t1, SemType t2) {
boolean res = isEmpty(cx, diff(t1, t2));
return res;
return isEmpty(cx, diff(t1, t2));
}

public static boolean isSubtypeSimple(SemType t1, SemType t2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ final class PredefinedTypeEnv {
basicSubType(BT_CELL, BCellSubType.createDelegate(bddAtom(atomCellMappingArray.get()))));
private final Supplier<ListAtomicType> listAtomicThreeElement = new ConcurrentLazySupplierWithCallback<>(
() -> new ListAtomicType(
FixedLengthArray.from(new SemType[]{cellSemTypeListSubtypeMapping.get(), cellSemTypeVal.get()}, 3),
FixedLengthArray.from(new SemType[]{cellSemTypeListSubtypeMapping.get(), cellSemTypeVal.get()},
3),
cellSemTypeVal.get()),
this::addInitializedListAtom
);
Expand Down Expand Up @@ -182,7 +183,8 @@ final class PredefinedTypeEnv {
() -> basicSubType(BT_CELL, BCellSubType.createDelegate(bddAtom(atomCellMappingArrayRO.get()))));
private final Supplier<ListAtomicType> listAtomicThreeElementRO = new ConcurrentLazySupplierWithCallback<>(
() -> new ListAtomicType(
FixedLengthArray.from(new SemType[]{cellSemTypeListSubtypeMappingRO.get(), cellSemTypeVal.get()},
FixedLengthArray.from(
new SemType[]{cellSemTypeListSubtypeMappingRO.get(), cellSemTypeVal.get()},
3),
cellSemTypeUndef.get()),
this::addInitializedListAtom
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,68 +19,36 @@
package io.ballerina.runtime.api.types.semtype;

import io.ballerina.runtime.api.types.Type;
import io.ballerina.runtime.internal.types.TypeCheckableType;
import io.ballerina.runtime.internal.types.semtype.MutableSemType;

/**
* Represent a type in runtime.
*
* @since 2201.12.0
*/
public sealed class SemType extends BasicTypeBitSet permits TypeCheckableType {

private int some;
private SubType[] subTypeData;
private static final SemType NEVER = new SemType(0, 0, null);

protected SemType(int all, int some, SubType[] subTypeData) {
super(all);
this.some = some;
this.subTypeData = subTypeData;
}
public interface SemType extends BasicTypeBitSet {

protected SemType() {
this(-1, -1, null);
}
SemType NEVER = new SemTypeImpl(0, 0, null);

public static SemType from(int all) {
static SemType from(int all) {
if (all == 0) {
return NEVER;
}
return new SemType(all, 0, null);
}

public static SemType from(int all, int some, SubType[] subTypes) {
return new SemType(all, some, subTypes);
}

public final int some() {
return some;
return new SemTypeImpl(all, 0, null);
}

public final SubType[] subTypeData() {
return subTypeData;
static SemType from(int all, int some, SubType[] subTypes) {
return new SemTypeImpl(all, some, subTypes);
}

public final SubType subTypeByCode(int code) {
if ((some() & (1 << code)) == 0) {
return null;
}
int someMask = (1 << code) - 1;
int some = some() & someMask;
return subTypeData()[Integer.bitCount(some)];
}

protected void setSome(int some, SubType[] subTypeData) {
this.some = some;
this.subTypeData = subTypeData;
}

public static SemType tryInto(Context cx, Type type) {
static SemType tryInto(Context cx, Type type) {
if (type instanceof MutableSemType mutableSemType) {
mutableSemType.updateInnerSemTypeIfNeeded(cx);
}

return (SemType) type;
}

int all();

int some();

SubType[] subTypeData();

SubType subTypeByCode(int code);
}
Loading

0 comments on commit 68f099c

Please sign in to comment.