8337167: StringSize deduplication

Reviewed-by: liach, rriggs
This commit is contained in:
Shaojin Wen 2024-07-26 07:08:33 +00:00 committed by Chen Liang
parent 487450cb5e
commit 7f11935461
8 changed files with 40 additions and 95 deletions

View file

@ -27,6 +27,7 @@ package java.lang;
import jdk.internal.misc.CDS;
import jdk.internal.misc.VM;
import jdk.internal.util.DecimalDigits;
import jdk.internal.vm.annotation.ForceInline;
import jdk.internal.vm.annotation.IntrinsicCandidate;
import jdk.internal.vm.annotation.Stable;
@ -427,7 +428,7 @@ public final class Integer extends Number
*/
@IntrinsicCandidate
public static String toString(int i) {
int size = stringSize(i);
int size = DecimalDigits.stringSize(i);
if (COMPACT_STRINGS) {
byte[] buf = new byte[size];
StringLatin1.getChars(i, size, buf);
@ -457,32 +458,6 @@ public final class Integer extends Number
return Long.toString(toUnsignedLong(i));
}
/**
* Returns the string representation size for a given int value.
*
* @param x int value
* @return string size
*
* @implNote There are other ways to compute this: e.g. binary search,
* but values are biased heavily towards zero, and therefore linear search
* wins. The iteration results are also routinely inlined in the generated
* code after loop unrolling.
*/
static int stringSize(int x) {
int d = 1;
if (x >= 0) {
d = 0;
x = -x;
}
int p = -10;
for (int i = 1; i < 10; i++) {
if (x > p)
return i + d;
p = 10 * p;
}
return 10 + d;
}
/**
* Parses the string argument as a signed integer in the radix
* specified by the second argument. The characters in the string