Skip to content
Permalink
Browse files
8267844: Replace Integer/Long.valueOf() with Integer/Long.parse*() wh…
…ere applicable

Reviewed-by: redestad
  • Loading branch information
stsypanov authored and cl4es committed Aug 12, 2021
1 parent d38b314 commit b29fbad940808c59f30e60222a9ca7a23c8e54b9
@@ -201,8 +201,7 @@ private static int getNumOfUnit(String mode, int offset, int blockSize)
if (mode.length() > offset) {
int numInt;
try {
Integer num = Integer.valueOf(mode.substring(offset));
numInt = num.intValue();
numInt = Integer.parseInt(mode.substring(offset));
result = numInt >> 3;
} catch (NumberFormatException e) {
throw new NoSuchAlgorithmException
@@ -1413,7 +1413,7 @@ public static Integer decode(String nm) throws NumberFormatException {
int radix = 10;
int index = 0;
boolean negative = false;
Integer result;
int result;

if (nm.isEmpty())
throw new NumberFormatException("Zero length string");
@@ -1443,15 +1443,15 @@ else if (nm.startsWith("0", index) && nm.length() > 1 + index) {
throw new NumberFormatException("Sign character in wrong position");

try {
result = Integer.valueOf(nm.substring(index), radix);
result = negative ? Integer.valueOf(-result.intValue()) : result;
result = parseInt(nm, index, nm.length(), radix);
result = negative ? -result : result;
} catch (NumberFormatException e) {
// 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
// rethrown.
String constant = negative ? ("-" + nm.substring(index))
: nm.substring(index);
result = Integer.valueOf(constant, radix);
result = parseInt(constant, radix);
}
return result;
}
@@ -1254,7 +1254,7 @@ public static Long decode(String nm) throws NumberFormatException {
int radix = 10;
int index = 0;
boolean negative = false;
Long result;
long result;

if (nm.isEmpty())
throw new NumberFormatException("Zero length string");
@@ -1284,15 +1284,15 @@ else if (nm.startsWith("0", index) && nm.length() > 1 + index) {
throw new NumberFormatException("Sign character in wrong position");

try {
result = Long.valueOf(nm.substring(index), radix);
result = negative ? Long.valueOf(-result.longValue()) : result;
result = parseLong(nm, index, nm.length(), radix);
result = negative ? -result : result;
} catch (NumberFormatException e) {
// 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
// rethrown.
String constant = negative ? ("-" + nm.substring(index))
: nm.substring(index);
result = Long.valueOf(constant, radix);
result = parseLong(constant, radix);
}
return result;
}
@@ -272,8 +272,8 @@ public static void saveProperties(Map<String, String> props) {
s = props.get("java.class.version");
int index = s.indexOf('.');
try {
classFileMajorVersion = Integer.valueOf(s.substring(0, index));
classFileMinorVersion = Integer.valueOf(s.substring(index+1, s.length()));
classFileMajorVersion = Integer.parseInt(s.substring(0, index));
classFileMinorVersion = Integer.parseInt(s.substring(index + 1));
} catch (NumberFormatException e) {
throw new InternalError(e);
}
@@ -76,6 +76,13 @@ public void parseInt(Blackhole bh) {
}
}

@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. */
@Benchmark
public void toStringSmall(Blackhole bh) {
@@ -35,6 +35,7 @@
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;

import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

@BenchmarkMode(Mode.AverageTime)
@@ -48,16 +49,20 @@
@Param("500")
private int size;

private String[] strings;
private long[] longArraySmall;
private long[] longArrayBig;

@Setup
public void setup() {
var random = ThreadLocalRandom.current();
strings = new String[size];
longArraySmall = new long[size];
longArrayBig = new long[size];
for (int i = 0; i < size; i++) {
strings[i] = "" + (random.nextLong(10000) - 5000);
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 void toStringSmall(Blackhole bh) {
}
}

@Benchmark
public void decode(Blackhole bh) {
for (String s : strings) {
bh.consume(Long.decode(s));
}
}

/** Performs toString on large values, around 10 digits. */
@Benchmark
public void toStringBig(Blackhole bh) {

1 comment on commit b29fbad

@openjdk-notifier

This comment has been minimized.

Copy link

@openjdk-notifier openjdk-notifier bot commented on b29fbad Aug 12, 2021

Please sign in to comment.