classlib: add cache to Byte and Short valueOf; fix Byte.parseByte (#832)

This commit is contained in:
Ivan Hetman 2023-10-26 09:09:08 +03:00 committed by GitHub
parent 4b6c4bd3d3
commit 3ac0078e2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 6 deletions

View File

@ -21,6 +21,7 @@ public class TByte extends TNumber implements TComparable<TByte> {
public static final Class<Byte> TYPE = byte.class; public static final Class<Byte> TYPE = byte.class;
public static final int SIZE = 8; public static final int SIZE = 8;
public static final int BYTES = 1; public static final int BYTES = 1;
private static TByte[] byteCache = ensureByteCache();
private final byte value; private final byte value;
public TByte(byte value) { public TByte(byte value) {
@ -56,9 +57,16 @@ public class TByte extends TNumber implements TComparable<TByte> {
return value; return value;
} }
public static TByte valueOf(byte value) { public static TByte valueOf(byte i) {
// TODO: add caching return byteCache[i + 128];
return new TByte(value); }
private static TByte[] ensureByteCache() {
TByte[] byteCache = new TByte[256];
for (int j = 0; j < byteCache.length; ++j) {
byteCache[j] = new TByte((byte) (j - 128));
}
return byteCache;
} }
public static String toString(byte value) { public static String toString(byte value) {
@ -111,7 +119,7 @@ public class TByte extends TNumber implements TComparable<TByte> {
public static byte parseByte(String s, int radix) throws TNumberFormatException { public static byte parseByte(String s, int radix) throws TNumberFormatException {
int value = TInteger.parseInt(s, radix); int value = TInteger.parseInt(s, radix);
if (value < MIN_VALUE || value >= MAX_VALUE) { if (value < MIN_VALUE || value > MAX_VALUE) {
throw new TNumberFormatException(); throw new TNumberFormatException();
} }
return (byte) value; return (byte) value;

View File

@ -21,6 +21,7 @@ public class TShort extends TNumber implements TComparable<TShort> {
public static final Class<Short> TYPE = short.class; public static final Class<Short> TYPE = short.class;
public static final int SIZE = 16; public static final int SIZE = 16;
public static final int BYTES = SIZE / Byte.SIZE; public static final int BYTES = SIZE / Byte.SIZE;
private static TShort[] shortCache;
private final short value; private final short value;
public TShort(short value) { public TShort(short value) {
@ -56,8 +57,21 @@ public class TShort extends TNumber implements TComparable<TShort> {
return value; return value;
} }
public static TShort valueOf(short value) { public static TShort valueOf(short i) {
return new TShort(value); if (i >= -128 && i <= 127) {
ensureShortCache();
return shortCache[i + 128];
}
return new TShort(i);
}
private static void ensureShortCache() {
if (shortCache == null) {
shortCache = new TShort[256];
for (int j = 0; j < shortCache.length; ++j) {
shortCache[j] = new TShort((short) (j - 128));
}
}
} }
public static String toString(short value) { public static String toString(short value) {

View File

@ -16,6 +16,7 @@
package org.teavm.classlib.java.lang; package org.teavm.classlib.java.lang;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -48,4 +49,11 @@ public class ByteTest {
public void bytes() { public void bytes() {
assertEquals(1, Byte.BYTES); assertEquals(1, Byte.BYTES);
} }
@Test
public void cache() {
for (int b = Byte.MIN_VALUE; b <= Byte.MAX_VALUE; b++) {
assertSame(Byte.valueOf((byte) b), (byte) b);
}
}
} }