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.math.DoubleToDecimal;
import jdk.internal.math.FloatToDecimal;
import jdk.internal.util.DecimalDigits;
import java.io.IOException;
import java.nio.CharBuffer;
@ -836,7 +837,7 @@ abstract sealed class AbstractStringBuilder implements Appendable, CharSequence
*/
public AbstractStringBuilder append(int i) {
int count = this.count;
int spaceNeeded = count + Integer.stringSize(i);
int spaceNeeded = count + DecimalDigits.stringSize(i);
ensureCapacityInternal(spaceNeeded);
if (isLatin1()) {
StringLatin1.getChars(i, spaceNeeded, value);
@ -861,7 +862,7 @@ abstract sealed class AbstractStringBuilder implements Appendable, CharSequence
*/
public AbstractStringBuilder append(long l) {
int count = this.count;
int spaceNeeded = count + Long.stringSize(l);
int spaceNeeded = count + DecimalDigits.stringSize(l);
ensureCapacityInternal(spaceNeeded);
if (isLatin1()) {
StringLatin1.getChars(l, spaceNeeded, value);

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

View file

@ -34,6 +34,7 @@ import java.util.Objects;
import java.util.Optional;
import jdk.internal.misc.CDS;
import jdk.internal.util.DecimalDigits;
import jdk.internal.vm.annotation.ForceInline;
import jdk.internal.vm.annotation.IntrinsicCandidate;
import jdk.internal.vm.annotation.Stable;
@ -457,7 +458,7 @@ public final class Long extends Number
* @return a string representation of the argument in base&nbsp;10.
*/
public static String toString(long i) {
int size = stringSize(i);
int size = DecimalDigits.stringSize(i);
if (COMPACT_STRINGS) {
byte[] buf = new byte[size];
StringLatin1.getChars(i, size, buf);
@ -487,32 +488,6 @@ public final class Long extends Number
return toUnsignedString(i, 10);
}
/**
* Returns the string representation size for a given long value.
*
* @param x long 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(long x) {
int d = 1;
if (x >= 0) {
d = 0;
x = -x;
}
long p = -10;
for (int i = 1; i < 19; i++) {
if (x > p)
return i + d;
p = 10 * p;
}
return 19 + d;
}
/**
* Parses the string argument as a signed {@code long} in the
* radix specified by the second argument. The characters in the

View file

@ -26,6 +26,7 @@
package java.lang;
import jdk.internal.misc.Unsafe;
import jdk.internal.util.DecimalDigits;
import jdk.internal.vm.annotation.ForceInline;
import java.lang.invoke.MethodHandle;
@ -96,7 +97,7 @@ final class StringConcatHelper {
* @return new length and coder
*/
static long mix(long lengthCoder, int value) {
return checkOverflow(lengthCoder + Integer.stringSize(value));
return checkOverflow(lengthCoder + DecimalDigits.stringSize(value));
}
/**
@ -107,7 +108,7 @@ final class StringConcatHelper {
* @return new length and coder
*/
static long mix(long lengthCoder, long value) {
return checkOverflow(lengthCoder + Long.stringSize(value));
return checkOverflow(lengthCoder + DecimalDigits.stringSize(value));
}
/**

View file

@ -2623,10 +2623,6 @@ public final class System {
return StringConcatHelper.mix(lengthCoder, value);
}
public int stringSize(long i) {
return Long.stringSize(i);
}
public int getCharsLatin1(long i, int index, byte[] buf) {
return StringLatin1.getChars(i, index, buf);
}