8267844: Replace Integer/Long.valueOf() with Integer/Long.parse*() where applicable

Reviewed-by: redestad
This commit is contained in:
Sergey Tsypanov 2021-08-12 10:03:52 +00:00 committed by Claes Redestad
parent d38b31438d
commit b29fbad940
6 changed files with 31 additions and 13 deletions

View file

@ -201,8 +201,7 @@ final class CipherCore {
if (mode.length() > offset) { if (mode.length() > offset) {
int numInt; int numInt;
try { try {
Integer num = Integer.valueOf(mode.substring(offset)); numInt = Integer.parseInt(mode.substring(offset));
numInt = num.intValue();
result = numInt >> 3; result = numInt >> 3;
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
throw new NoSuchAlgorithmException throw new NoSuchAlgorithmException

View file

@ -1413,7 +1413,7 @@ public final class Integer extends Number
int radix = 10; int radix = 10;
int index = 0; int index = 0;
boolean negative = false; boolean negative = false;
Integer result; int result;
if (nm.isEmpty()) if (nm.isEmpty())
throw new NumberFormatException("Zero length string"); throw new NumberFormatException("Zero length string");
@ -1443,15 +1443,15 @@ public final class Integer extends Number
throw new NumberFormatException("Sign character in wrong position"); throw new NumberFormatException("Sign character in wrong position");
try { try {
result = Integer.valueOf(nm.substring(index), radix); result = parseInt(nm, index, nm.length(), radix);
result = negative ? Integer.valueOf(-result.intValue()) : result; result = negative ? -result : result;
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
// If number is Integer.MIN_VALUE, we'll end up here. The next line // If number is Integer.MIN_VALUE, we'll end up here. The next line
// handles this case, and causes any genuine format error to be // handles this case, and causes any genuine format error to be
// rethrown. // rethrown.
String constant = negative ? ("-" + nm.substring(index)) String constant = negative ? ("-" + nm.substring(index))
: nm.substring(index); : nm.substring(index);
result = Integer.valueOf(constant, radix); result = parseInt(constant, radix);
} }
return result; return result;
} }

View file

@ -1254,7 +1254,7 @@ public final class Long extends Number
int radix = 10; int radix = 10;
int index = 0; int index = 0;
boolean negative = false; boolean negative = false;
Long result; long result;
if (nm.isEmpty()) if (nm.isEmpty())
throw new NumberFormatException("Zero length string"); throw new NumberFormatException("Zero length string");
@ -1284,15 +1284,15 @@ public final class Long extends Number
throw new NumberFormatException("Sign character in wrong position"); throw new NumberFormatException("Sign character in wrong position");
try { try {
result = Long.valueOf(nm.substring(index), radix); result = parseLong(nm, index, nm.length(), radix);
result = negative ? Long.valueOf(-result.longValue()) : result; result = negative ? -result : result;
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
// If number is Long.MIN_VALUE, we'll end up here. The next line // If number is Long.MIN_VALUE, we'll end up here. The next line
// handles this case, and causes any genuine format error to be // handles this case, and causes any genuine format error to be
// rethrown. // rethrown.
String constant = negative ? ("-" + nm.substring(index)) String constant = negative ? ("-" + nm.substring(index))
: nm.substring(index); : nm.substring(index);
result = Long.valueOf(constant, radix); result = parseLong(constant, radix);
} }
return result; return result;
} }

View file

@ -272,8 +272,8 @@ public class VM {
s = props.get("java.class.version"); s = props.get("java.class.version");
int index = s.indexOf('.'); int index = s.indexOf('.');
try { try {
classFileMajorVersion = Integer.valueOf(s.substring(0, index)); classFileMajorVersion = Integer.parseInt(s.substring(0, index));
classFileMinorVersion = Integer.valueOf(s.substring(index+1, s.length())); classFileMinorVersion = Integer.parseInt(s.substring(index + 1));
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
throw new InternalError(e); throw new InternalError(e);
} }

View file

@ -76,6 +76,13 @@ public class Integers {
} }
} }
@Benchmark
public void decode(Blackhole bh) {
for (String s : strings) {
bh.consume(Integer.decode(s));
}
}
/** Performs toString on small values, just a couple of digits. */ /** Performs toString on small values, just a couple of digits. */
@Benchmark @Benchmark
public void toStringSmall(Blackhole bh) { public void toStringSmall(Blackhole bh) {

View file

@ -35,6 +35,7 @@ import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup; import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole; import org.openjdk.jmh.infra.Blackhole;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@BenchmarkMode(Mode.AverageTime) @BenchmarkMode(Mode.AverageTime)
@ -48,16 +49,20 @@ public class Longs {
@Param("500") @Param("500")
private int size; private int size;
private String[] strings;
private long[] longArraySmall; private long[] longArraySmall;
private long[] longArrayBig; private long[] longArrayBig;
@Setup @Setup
public void setup() { public void setup() {
var random = ThreadLocalRandom.current();
strings = new String[size];
longArraySmall = new long[size]; longArraySmall = new long[size];
longArrayBig = new long[size]; longArrayBig = new long[size];
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
strings[i] = "" + (random.nextLong(10000) - 5000);
longArraySmall[i] = 100L * i + i + 103L; longArraySmall[i] = 100L * i + i + 103L;
longArrayBig[i] = ((100L * i + i) << 32) + 4543 + i * 4; longArrayBig[i] = ((100L * i + i) << 32) + 4543 + i * 4L;
} }
} }
@ -69,6 +74,13 @@ public class Longs {
} }
} }
@Benchmark
public void decode(Blackhole bh) {
for (String s : strings) {
bh.consume(Long.decode(s));
}
}
/** Performs toString on large values, around 10 digits. */ /** Performs toString on large values, around 10 digits. */
@Benchmark @Benchmark
public void toStringBig(Blackhole bh) { public void toStringBig(Blackhole bh) {