Encapsulate boxing caches marking them side-effect free. Marking the cache operations as side-effect free allows the J2CL WASM pipeline to remove boxing code when the results are not used. PiperOrigin-RevId: 399339138 Change-Id: Ia17efa779823ced63c2cd98f7abfbac4ae0f9688
diff --git a/user/super/com/google/gwt/emul/java/lang/Byte.java b/user/super/com/google/gwt/emul/java/lang/Byte.java index bc87eed..d5f0f1f 100644 --- a/user/super/com/google/gwt/emul/java/lang/Byte.java +++ b/user/super/com/google/gwt/emul/java/lang/Byte.java
@@ -32,6 +32,17 @@ private static class BoxedValues { // Box all values according to JLS private static Byte[] boxedValues = new Byte[256]; + + // This method should be marked with @HasNoSideEffects but it seems to trigger a bug + // in the optimizing pipeling and breaks one test. + private static Byte get(byte b) { + int rebase = b + 128; + Byte result = BoxedValues.boxedValues[rebase]; + if (result == null) { + result = BoxedValues.boxedValues[rebase] = new Byte(b); + } + return result; + } } public static int compare(byte x, byte y) { @@ -60,12 +71,7 @@ } public static Byte valueOf(byte b) { - int rebase = b + 128; - Byte result = BoxedValues.boxedValues[rebase]; - if (result == null) { - result = BoxedValues.boxedValues[rebase] = new Byte(b); - } - return result; + return BoxedValues.get(b); } public static Byte valueOf(String s) throws NumberFormatException {
diff --git a/user/super/com/google/gwt/emul/java/lang/Character.java b/user/super/com/google/gwt/emul/java/lang/Character.java index 3733f73..4b01e4d 100644 --- a/user/super/com/google/gwt/emul/java/lang/Character.java +++ b/user/super/com/google/gwt/emul/java/lang/Character.java
@@ -19,6 +19,7 @@ import java.io.Serializable; import javaemul.internal.NativeRegExp; +import javaemul.internal.annotations.HasNoSideEffects; /** * Wraps a native <code>char</code> as an object. @@ -104,6 +105,15 @@ private static class BoxedValues { // Box values according to JLS - from \u0000 to \u007f private static Character[] boxedValues = new Character[128]; + + @HasNoSideEffects + private static Character get(char c) { + Character result = BoxedValues.boxedValues[c]; + if (result == null) { + result = BoxedValues.boxedValues[c] = new Character(c); + } + return result; + } } public static final Class<Character> TYPE = Character.class; @@ -426,11 +436,7 @@ public static Character valueOf(char c) { if (c < 128) { - Character result = BoxedValues.boxedValues[c]; - if (result == null) { - result = BoxedValues.boxedValues[c] = new Character(c); - } - return result; + return BoxedValues.get(c); } return new Character(c); }
diff --git a/user/super/com/google/gwt/emul/java/lang/Integer.java b/user/super/com/google/gwt/emul/java/lang/Integer.java index 1e2f584..a56e31e 100644 --- a/user/super/com/google/gwt/emul/java/lang/Integer.java +++ b/user/super/com/google/gwt/emul/java/lang/Integer.java
@@ -16,6 +16,7 @@ package java.lang; import javaemul.internal.JsUtils; +import javaemul.internal.annotations.HasNoSideEffects; /** * Wraps a primitive <code>int</code> as an object. @@ -34,6 +35,16 @@ private static class BoxedValues { // Box values according to JLS - between -128 and 127 private static Integer[] boxedValues = new Integer[256]; + + @HasNoSideEffects + private static Integer get(int i) { + int rebase = i + 128; + Integer result = boxedValues[rebase]; + if (result == null) { + result = boxedValues[rebase] = new Integer(i); + } + return result; + } } /** @@ -233,12 +244,7 @@ public static Integer valueOf(int i) { if (i > -129 && i < 128) { - int rebase = i + 128; - Integer result = BoxedValues.boxedValues[rebase]; - if (result == null) { - result = BoxedValues.boxedValues[rebase] = new Integer(i); - } - return result; + return BoxedValues.get(i); } return new Integer(i); }
diff --git a/user/super/com/google/gwt/emul/java/lang/Long.java b/user/super/com/google/gwt/emul/java/lang/Long.java index da22734..693efeb 100644 --- a/user/super/com/google/gwt/emul/java/lang/Long.java +++ b/user/super/com/google/gwt/emul/java/lang/Long.java
@@ -16,6 +16,7 @@ package java.lang; import javaemul.internal.LongUtils; +import javaemul.internal.annotations.HasNoSideEffects; /** Wraps a primitive <code>long</code> as an object. */ public final class Long extends Number implements Comparable<Long> { @@ -24,6 +25,16 @@ static class BoxedValues { // Box values according to JLS - between -128 and 127 static Long[] boxedValues = new Long[256]; + + @HasNoSideEffects + private static Long get(long l) { + int rebase = (int) l + 128; + Long result = BoxedValues.boxedValues[rebase]; + if (result == null) { + result = BoxedValues.boxedValues[rebase] = new Long(l); + } + return result; + } } public static final long MAX_VALUE = 0x7fffffffffffffffL; @@ -206,16 +217,11 @@ return String.valueOf(buf, cursor, bufLen - cursor); } - public static Long valueOf(long i) { - if (i > -129 && i < 128) { - int rebase = (int) i + 128; - Long result = BoxedValues.boxedValues[rebase]; - if (result == null) { - result = BoxedValues.boxedValues[rebase] = new Long(i); - } - return result; + public static Long valueOf(long l) { + if (l > -129 && l < 128) { + return BoxedValues.get(l); } - return new Long(i); + return new Long(l); } public static Long valueOf(String s) throws NumberFormatException {
diff --git a/user/super/com/google/gwt/emul/java/lang/Short.java b/user/super/com/google/gwt/emul/java/lang/Short.java index c42d03c..15e9b38 100644 --- a/user/super/com/google/gwt/emul/java/lang/Short.java +++ b/user/super/com/google/gwt/emul/java/lang/Short.java
@@ -15,6 +15,8 @@ */ package java.lang; +import javaemul.internal.annotations.HasNoSideEffects; + /** * Wraps a primitive <code>short</code> as an object. */ @@ -32,6 +34,16 @@ private static class BoxedValues { // Box values according to JLS - between -128 and 127 private static Short[] boxedValues = new Short[256]; + + @HasNoSideEffects + private static Short get(short s) { + int rebase = s + 128; + Short result = BoxedValues.boxedValues[rebase]; + if (result == null) { + result = BoxedValues.boxedValues[rebase] = new Short(s); + } + return result; + } } public static int compare(short x, short y) { @@ -65,12 +77,7 @@ public static Short valueOf(short s) { if (s > -129 && s < 128) { - int rebase = s + 128; - Short result = BoxedValues.boxedValues[rebase]; - if (result == null) { - result = BoxedValues.boxedValues[rebase] = new Short(s); - } - return result; + return BoxedValues.get(s); } return new Short(s); }