8278831: Use table lookup for the last two bytes in Integer.getChars

Reviewed-by: jlaskey, rriggs
This commit is contained in:
Claes Redestad 2022-01-17 11:01:55 +00:00
parent 431bd9a66d
commit 71ca85f5a6
4 changed files with 26 additions and 31 deletions

View file

@ -517,13 +517,9 @@ public final class Integer extends Number
} }
// We know there are at most two digits left at this point. // We know there are at most two digits left at this point.
q = i / 10; buf[--charPos] = DigitOnes[-i];
r = (q * 10) - i; if (i < -9) {
buf[--charPos] = (byte)('0' + r); buf[--charPos] = DigitTens[-i];
// Whatever left is the remaining digit.
if (q < 0) {
buf[--charPos] = (byte)('0' - q);
} }
if (negative) { if (negative) {

View file

@ -565,13 +565,9 @@ public final class Long extends Number
} }
// We know there are at most two digits left at this point. // We know there are at most two digits left at this point.
q2 = i2 / 10; buf[--charPos] = Integer.DigitOnes[-i2];
r = (q2 * 10) - i2; if (i2 < -9) {
buf[--charPos] = (byte)('0' + r); buf[--charPos] = Integer.DigitTens[-i2];
// Whatever left is the remaining digit.
if (q2 < 0) {
buf[--charPos] = (byte)('0' - q2);
} }
if (negative) { if (negative) {

View file

@ -1549,13 +1549,9 @@ final class StringUTF16 {
} }
// We know there are at most two digits left at this point. // We know there are at most two digits left at this point.
q = i / 10; putChar(buf, --charPos, Integer.DigitOnes[-i]);
r = (q * 10) - i; if (i < -9) {
putChar(buf, --charPos, '0' + r); putChar(buf, --charPos, Integer.DigitTens[-i]);
// Whatever left is the remaining digit.
if (q < 0) {
putChar(buf, --charPos, '0' - q);
} }
if (negative) { if (negative) {
@ -1604,13 +1600,9 @@ final class StringUTF16 {
} }
// We know there are at most two digits left at this point. // We know there are at most two digits left at this point.
q2 = i2 / 10; putChar(buf, --charPos, Integer.DigitOnes[-i2]);
r = (q2 * 10) - i2; if (i2 < -9) {
putChar(buf, --charPos, '0' + r); putChar(buf, --charPos, Integer.DigitTens[-i2]);
// Whatever left is the remaining digit.
if (q2 < 0) {
putChar(buf, --charPos, '0' - q2);
} }
if (negative) { if (negative) {

View file

@ -53,6 +53,7 @@ public class Integers {
private int size; private int size;
private String[] strings; private String[] strings;
private int[] intsTiny;
private int[] intsSmall; private int[] intsSmall;
private int[] intsBig; private int[] intsBig;
@ -60,10 +61,12 @@ public class Integers {
public void setup() { public void setup() {
Random r = new Random(0); Random r = new Random(0);
strings = new String[size]; strings = new String[size];
intsTiny = new int[size];
intsSmall = new int[size]; intsSmall = new int[size];
intsBig = new int[size]; intsBig = new int[size];
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
strings[i] = "" + (r.nextInt(10000) - (5000)); strings[i] = "" + (r.nextInt(10000) - (5000));
intsTiny[i] = r.nextInt(99);
intsSmall[i] = 100 * i + i + 103; intsSmall[i] = 100 * i + i + 103;
intsBig[i] = ((100 * i + i) << 24) + 4543 + i * 4; intsBig[i] = ((100 * i + i) << 24) + 4543 + i * 4;
} }
@ -91,6 +94,14 @@ public class Integers {
} }
} }
/** Performs toString on very small values, just one or two digits. */
@Benchmark
public void toStringTiny(Blackhole bh) {
for (int i : intsTiny) {
bh.consume(Integer.toString(i));
}
}
/** Performs toString on large values, roughly 10 digits. */ /** Performs toString on large values, roughly 10 digits. */
@Benchmark @Benchmark
public void toStringBig(Blackhole bh) { public void toStringBig(Blackhole bh) {