Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
dbf5dff
move *::stringSize to jdk.internal.util.DecimalDigits::stringSize
wenshao Sep 12, 2023
f553fb1
move StringLatin1::getChars to jdk.internal.util.DecimalDigits::getCh…
wenshao Sep 12, 2023
1ab86e2
fix build error
wenshao Sep 13, 2023
fe5263c
fix build error
wenshao Sep 13, 2023
1ef45c5
add comment
wenshao Sep 13, 2023
6d70c84
Merge remote-tracking branch 'upstream/master' into refactor_get_char…
wenshao Sep 22, 2023
4e76866
rebase from master
wenshao Sep 22, 2023
c0394e0
rebase from master & refactor digits
wenshao Sep 22, 2023
91db603
restore HexDigits & OctalDigits
wenshao Sep 22, 2023
97c7f02
restore HexDigits & OctalDigits
wenshao Sep 22, 2023
f1cb9fd
refactor HexDigits & OctalDigits & FormatItem, FormatItem#prepend pro…
wenshao Sep 23, 2023
074c48e
refactor HexDigits & OctalDigits & FormatItem, FormatItem#prepend pro…
wenshao Sep 23, 2023
4fda2f1
refactor FormatItem, remove MethodHandle for maintainable
wenshao Sep 23, 2023
4486768
move String.getChars to DecimalDigits, and refactor FormatItem
wenshao Sep 23, 2023
fd589a8
add CopyRight
wenshao Sep 23, 2023
a613e5e
fix compile error
wenshao Sep 23, 2023
3ace6b7
fix compile error
wenshao Sep 23, 2023
6c25505
remove unused code
wenshao Sep 23, 2023
1ab0fbd
bug fix for DecimalDigits#getCharsUTF16(int, int, byte[])
wenshao Sep 23, 2023
e29c5f6
refactor from #15836
wenshao Sep 23, 2023
e1c4fde
remove unused throws
wenshao Sep 23, 2023
a7c21d2
remove unused throws
wenshao Sep 23, 2023
ab6377e
refactor & bug fix
wenshao Sep 23, 2023
7f30978
fix build error
wenshao Sep 23, 2023
ebbb559
bug fix FormatItemDecimal::prepend
wenshao Sep 23, 2023
8ec66cc
restore StringUTF16.getChars(int,int,int,byte[]) and StringUTF16.getC…
wenshao Sep 24, 2023
d4f55dd
Revert FormatItem related changes
wenshao Sep 29, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/hotspot/share/opto/stringopts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1160,7 +1160,7 @@ bool StringConcat::validate_control_flow() {
return !fail;
}

// Mirror of Integer.stringSize() method, return the count of digits in integer,
// Mirror of DecimalDigits.stringSize() method, return the count of digits in integer,
Node* PhaseStringOpts::int_stringSize(GraphKit& kit, Node* arg) {
if (arg->is_Con()) {
// Constant integer. Compute constant length
Expand Down
17 changes: 11 additions & 6 deletions src/java.base/share/classes/java/lang/AbstractStringBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.stream.IntStream;
import java.util.stream.StreamSupport;
import jdk.internal.util.ArraysSupport;
import jdk.internal.util.DecimalDigits;
import jdk.internal.util.Preconditions;

import static java.lang.String.COMPACT_STRINGS;
Expand Down Expand Up @@ -829,12 +830,14 @@ public AbstractStringBuilder append(char c) {
*/
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);
DecimalDigits.getCharsLatin1(i, spaceNeeded, value);
} else {
StringUTF16.getChars(i, count, spaceNeeded, value);
StringUTF16.checkBoundsBeginEnd(count, spaceNeeded, value);
int pos = DecimalDigits.getCharsUTF16(i, spaceNeeded, value);
assert count == pos;
}
this.count = spaceNeeded;
return this;
Expand All @@ -854,12 +857,14 @@ public AbstractStringBuilder append(int i) {
*/
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);
DecimalDigits.getCharsLatin1(l, spaceNeeded, value);
} else {
StringUTF16.getChars(l, count, spaceNeeded, value);
StringUTF16.checkBoundsBeginEnd(count, spaceNeeded, value);
int pos = DecimalDigits.getCharsUTF16(l, spaceNeeded, value);
assert count == pos;
}
this.count = spaceNeeded;
return this;
Expand Down
33 changes: 4 additions & 29 deletions src/java.base/share/classes/java/lang/Integer.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

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;
Expand Down Expand Up @@ -426,14 +427,14 @@ private static void formatUnsignedIntUTF16(int val, int shift, byte[] buf, int l
*/
@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);
DecimalDigits.getCharsLatin1(i, size, buf);
return new String(buf, LATIN1);
} else {
byte[] buf = new byte[size * 2];
StringUTF16.getChars(i, size, buf);
DecimalDigits.getCharsUTF16(i, size, buf);
return new String(buf, UTF16);
}
}
Expand All @@ -456,32 +457,6 @@ public static String toUnsignedString(int i) {
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
Expand Down
33 changes: 4 additions & 29 deletions src/java.base/share/classes/java/lang/Long.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
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;
Expand Down Expand Up @@ -456,14 +457,14 @@ private static void formatUnsignedLong0UTF16(long val, int shift, byte[] buf, in
* @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);
DecimalDigits.getCharsLatin1(i, size, buf);
return new String(buf, LATIN1);
} else {
byte[] buf = new byte[size * 2];
StringUTF16.getChars(i, size, buf);
DecimalDigits.getCharsUTF16(i, size, buf);
return new String(buf, UTF16);
}
}
Expand All @@ -486,32 +487,6 @@ public static String toUnsignedString(long i) {
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
Expand Down
13 changes: 7 additions & 6 deletions src/java.base/share/classes/java/lang/StringConcatHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import jdk.internal.misc.Unsafe;
import jdk.internal.javac.PreviewFeature;
import jdk.internal.util.DecimalDigits;
import jdk.internal.util.FormatConcatItem;
import jdk.internal.vm.annotation.ForceInline;

Expand Down Expand Up @@ -98,7 +99,7 @@ static long mix(long lengthCoder, char value) {
* @return new length and coder
*/
static long mix(long lengthCoder, int value) {
return checkOverflow(lengthCoder + Integer.stringSize(value));
return checkOverflow(lengthCoder + DecimalDigits.stringSize(value));
}

/**
Expand All @@ -109,7 +110,7 @@ static long mix(long lengthCoder, int value) {
* @return new length and coder
*/
static long mix(long lengthCoder, long value) {
return checkOverflow(lengthCoder + Long.stringSize(value));
return checkOverflow(lengthCoder + DecimalDigits.stringSize(value));
}

/**
Expand Down Expand Up @@ -250,9 +251,9 @@ static long prepend(long indexCoder, byte[] buf, char value, String prefix) {
*/
private static long prepend(long indexCoder, byte[] buf, int value) {
if (indexCoder < UTF16) {
return StringLatin1.getChars(value, (int)indexCoder, buf);
return DecimalDigits.getCharsLatin1(value, (int)indexCoder, buf);
} else {
return StringUTF16.getChars(value, (int)indexCoder, buf) | UTF16;
return DecimalDigits.getCharsUTF16(value, (int)indexCoder, buf) | UTF16;
}
}

Expand Down Expand Up @@ -285,9 +286,9 @@ static long prepend(long indexCoder, byte[] buf, int value, String prefix) {
*/
private static long prepend(long indexCoder, byte[] buf, long value) {
if (indexCoder < UTF16) {
return StringLatin1.getChars(value, (int)indexCoder, buf);
return DecimalDigits.getCharsLatin1(value, (int)indexCoder, buf);
} else {
return StringUTF16.getChars(value, (int)indexCoder, buf) | UTF16;
return DecimalDigits.getCharsUTF16(value, (int)indexCoder, buf) | UTF16;
}
}

Expand Down
114 changes: 0 additions & 114 deletions src/java.base/share/classes/java/lang/StringLatin1.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,120 +79,6 @@ public static byte[] inflate(byte[] value, int off, int len) {
return ret;
}

/**
* Places characters representing the integer i into the
* character array buf. The characters are placed into
* the buffer backwards starting with the least significant
* digit at the specified index (exclusive), and working
* backwards from there.
*
* @implNote This method converts positive inputs into negative
* values, to cover the Integer.MIN_VALUE case. Converting otherwise
* (negative to positive) will expose -Integer.MIN_VALUE that overflows
* integer.
*
* @param i value to convert
* @param index next index, after the least significant digit
* @param buf target buffer, Latin1-encoded
* @return index of the most significant digit or minus sign, if present
*/
static int getChars(int i, int index, byte[] buf) {
// Used by trusted callers. Assumes all necessary bounds checks have been done by the caller.
int q;
int charPos = index;

boolean negative = i < 0;
if (!negative) {
i = -i;
}

// Generate two digits per iteration
while (i <= -100) {
q = i / 100;
charPos -= 2;
writeDigitPair(buf, charPos, (q * 100) - i);
i = q;
}

// We know there are at most two digits left at this point.
if (i < -9) {
charPos -= 2;
writeDigitPair(buf, charPos, -i);
} else {
buf[--charPos] = (byte)('0' - i);
}

if (negative) {
buf[--charPos] = (byte)'-';
}
return charPos;
}

/**
* Places characters representing the long i into the
* character array buf. The characters are placed into
* the buffer backwards starting with the least significant
* digit at the specified index (exclusive), and working
* backwards from there.
*
* @implNote This method converts positive inputs into negative
* values, to cover the Long.MIN_VALUE case. Converting otherwise
* (negative to positive) will expose -Long.MIN_VALUE that overflows
* long.
*
* @param i value to convert
* @param index next index, after the least significant digit
* @param buf target buffer, Latin1-encoded
* @return index of the most significant digit or minus sign, if present
*/
static int getChars(long i, int index, byte[] buf) {
// Used by trusted callers. Assumes all necessary bounds checks have been done by the caller.
long q;
int charPos = index;

boolean negative = (i < 0);
if (!negative) {
i = -i;
}

// Get 2 digits/iteration using longs until quotient fits into an int
while (i <= Integer.MIN_VALUE) {
q = i / 100;
charPos -= 2;
writeDigitPair(buf, charPos, (int)((q * 100) - i));
i = q;
}

// Get 2 digits/iteration using ints
int q2;
int i2 = (int)i;
while (i2 <= -100) {
q2 = i2 / 100;
charPos -= 2;
writeDigitPair(buf, charPos, (q2 * 100) - i2);
i2 = q2;
}

// We know there are at most two digits left at this point.
if (i2 < -9) {
charPos -= 2;
writeDigitPair(buf, charPos, -i2);
} else {
buf[--charPos] = (byte)('0' - i2);
}

if (negative) {
buf[--charPos] = (byte)'-';
}
return charPos;
}

private static void writeDigitPair(byte[] buf, int charPos, int value) {
short pair = DecimalDigits.digitPair(value);
buf[charPos] = (byte)(pair);
buf[charPos + 1] = (byte)(pair >> 8);
}

public static void getChars(byte[] value, int srcBegin, int srcEnd, char[] dst, int dstBegin) {
inflate(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);
}
Expand Down
Loading