mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-26 22:34:27 +02:00
8337167: StringSize deduplication
Reviewed-by: liach, rriggs
This commit is contained in:
parent
487450cb5e
commit
7f11935461
8 changed files with 40 additions and 95 deletions
|
@ -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);
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 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
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -121,6 +121,8 @@ import java.util.concurrent.ConcurrentMap;
|
|||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import jdk.internal.util.DecimalDigits;
|
||||
|
||||
import sun.text.spi.JavaTimeDateTimePatternProvider;
|
||||
import sun.util.locale.provider.CalendarDataUtility;
|
||||
import sun.util.locale.provider.LocaleProviderAdapter;
|
||||
|
@ -2908,24 +2910,6 @@ public final class DateTimeFormatterBuilder {
|
|||
return new NumberPrinterParser(field, minWidth, maxWidth, signStyle, this.subsequentWidth + subsequentWidth);
|
||||
}
|
||||
|
||||
/*
|
||||
* Copied from Long.stringSize
|
||||
*/
|
||||
private 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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean format(DateTimePrintContext context, StringBuilder buf) {
|
||||
Long valueLong = context.getValue(field);
|
||||
|
@ -2934,7 +2918,7 @@ public final class DateTimeFormatterBuilder {
|
|||
}
|
||||
long value = getValue(context, valueLong);
|
||||
DecimalStyle decimalStyle = context.getDecimalStyle();
|
||||
int size = stringSize(value);
|
||||
int size = DecimalDigits.stringSize(value);
|
||||
if (value < 0) {
|
||||
size--;
|
||||
}
|
||||
|
@ -3369,17 +3353,6 @@ public final class DateTimeFormatterBuilder {
|
|||
return false;
|
||||
}
|
||||
|
||||
// Simplified variant of Integer.stringSize that assumes positive values
|
||||
private static int stringSize(int x) {
|
||||
int p = 10;
|
||||
for (int i = 1; i < 10; i++) {
|
||||
if (x < p)
|
||||
return i;
|
||||
p = 10 * p;
|
||||
}
|
||||
return 10;
|
||||
}
|
||||
|
||||
private static final int[] TENS = new int[] {
|
||||
1,
|
||||
10,
|
||||
|
@ -3400,7 +3373,7 @@ public final class DateTimeFormatterBuilder {
|
|||
}
|
||||
int val = field.range().checkValidIntValue(value, field);
|
||||
DecimalStyle decimalStyle = context.getDecimalStyle();
|
||||
int stringSize = stringSize(val);
|
||||
int stringSize = DecimalDigits.stringSize(val);
|
||||
char zero = decimalStyle.getZeroDigit();
|
||||
if (val == 0 || stringSize < 10 - maxWidth) {
|
||||
// 0 or would round down to 0
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue