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

Reuse the existing semtype port tests to test the runtime semtype implementation #42841

Closed
Prev Previous commit
Next Next commit
Refactor caches to use DATs
heshanpadmasiri committed Jul 22, 2024

Verified

This commit was signed with the committer’s verified signature.
targos Michaël Zasso
commit b5d632678629a9e476f52da188845ae88356ec77
Original file line number Diff line number Diff line change
@@ -18,33 +18,28 @@

package io.ballerina.runtime.api.types.SemType;

import java.util.HashMap;
import java.util.Map;

public final class BasicTypeCode {

private final static Map<Integer, BasicTypeCode> cache = new HashMap<>();

static final int CODE_NIL = 0x00;
static final int CODE_BOOLEAN = 0x01;
static final int CODE_INT = 0x02;
static final int CODE_FLOAT = 0x03;
static final int CODE_DECIMAL = 0x04;
static final int CODE_STRING = 0x05;
static final int CODE_ERROR = 0x06;
static final int CODE_TYPEDESC = 0x07;
static final int CODE_HANDLE = 0x08;
static final int CODE_FUNCTION = 0x09;
static final int CODE_FUTURE = 0x0A;
static final int CODE_STREAM = 0x0B;
static final int CODE_LIST = 0x0C;
static final int CODE_MAPPING = 0x0D;
static final int CODE_TABLE = 0x0E;
static final int CODE_XML = 0x0F;
static final int CODE_OBJECT = 0x10;
static final int CODE_CELL = 0x11;
static final int CODE_UNDEF = 0x12;
static final int CODE_B_TYPE = 0x13;
public static final int CODE_NIL = 0x00;
public static final int CODE_BOOLEAN = 0x01;
public static final int CODE_INT = 0x02;
public static final int CODE_FLOAT = 0x03;
public static final int CODE_DECIMAL = 0x04;
public static final int CODE_STRING = 0x05;
public static final int CODE_ERROR = 0x06;
public static final int CODE_TYPEDESC = 0x07;
public static final int CODE_HANDLE = 0x08;
public static final int CODE_FUNCTION = 0x09;
public static final int CODE_FUTURE = 0x0A;
public static final int CODE_STREAM = 0x0B;
public static final int CODE_LIST = 0x0C;
public static final int CODE_MAPPING = 0x0D;
public static final int CODE_TABLE = 0x0E;
public static final int CODE_XML = 0x0F;
public static final int CODE_OBJECT = 0x10;
public static final int CODE_CELL = 0x11;
public static final int CODE_UNDEF = 0x12;
public static final int CODE_B_TYPE = 0x13;

// Inherently immutable
public static final BasicTypeCode BT_NIL = from(CODE_NIL);
@@ -75,7 +70,7 @@ public final class BasicTypeCode {
public static final BasicTypeCode BT_B_TYPE = from(CODE_B_TYPE);

// Helper bit fields (does not represent basic type tag)
static final int VT_COUNT = BT_OBJECT.code + 1;
static final int VT_COUNT = CODE_OBJECT + 1;
static final int VT_MASK = (1 << VT_COUNT) - 1;

static final int VT_COUNT_INHERENTLY_IMMUTABLE = 0x0A;
@@ -88,10 +83,29 @@ private BasicTypeCode(int code) {
}

public static BasicTypeCode from(int code) {
return cache.computeIfAbsent(code, BasicTypeCode::new);
if (BasicTypeCodeCache.isCached(code)) {
return BasicTypeCodeCache.cache[code];
}
return new BasicTypeCode(code);
}

public int code() {
return code;
}

private final static class BasicTypeCodeCache {

private static final BasicTypeCode[] cache;
static {
cache = new BasicTypeCode[CODE_B_TYPE + 2];
for (int i = CODE_NIL; i < CODE_B_TYPE + 1; i++) {
cache[i] = new BasicTypeCode(i);
}
}

private static boolean isCached(int code) {
return 0 < code && code < VT_COUNT;
}

}
}
Original file line number Diff line number Diff line change
@@ -21,26 +21,29 @@
import io.ballerina.runtime.api.Module;
import io.ballerina.runtime.api.types.BasicTypeBitSet;
import io.ballerina.runtime.api.types.IntersectionType;
import io.ballerina.runtime.api.types.SemType.BasicTypeCode;
import io.ballerina.runtime.api.types.SemType.SemTypeHelper;
import io.ballerina.runtime.api.types.Type;

import java.util.HashMap;
import java.util.Map;
import static io.ballerina.runtime.api.types.SemType.BasicTypeCode.CODE_B_TYPE;
import static io.ballerina.runtime.api.types.SemType.BasicTypeCode.CODE_NIL;

public final class BBasicTypeBitSet implements BasicTypeBitSet {

private final int all;
private final BTypeAdapter adapter;

private final static Map<Integer, BBasicTypeBitSet> cache = new HashMap<>();

private BBasicTypeBitSet(int all) {
this.all = all;
this.adapter = new BTypeAdapter(this);
}

public static BasicTypeBitSet from(int all) {
return cache.computeIfAbsent(all, BBasicTypeBitSet::new);
int index = BBasicTypeBitSetCache.cacheIndex(all);
if (index != BBasicTypeBitSetCache.NotFound) {
return BBasicTypeBitSetCache.cache[index];
}
return new BBasicTypeBitSet(all);
}

@Override
@@ -132,4 +135,27 @@ public int all() {
public String toString() {
return SemTypeHelper.stringRepr(this);
}
}

// TODO: see if we can use Application CDS to make this even faster
private final static class BBasicTypeBitSetCache {

private static final BBasicTypeBitSet[] cache;
private static final int NotFound = -1;
static {
cache = new BBasicTypeBitSet[BasicTypeCode.CODE_B_TYPE + 2];
for (int i = CODE_NIL; i < CODE_B_TYPE + 1; i++) {
cache[i] = new BBasicTypeBitSet(1 << i);
}
}

private static int cacheIndex(int all) {
// TODO: check this is getting unrolled, otherwise use a switch
for (int i = CODE_NIL; i < CODE_B_TYPE + 1; i++) {
if (all == (1 << i)) {
return i;
}
}
return NotFound;
}
}
}