Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 5 commits into from
Closed
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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
@@ -1417,7 +1417,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");
@@ -1447,15 +1447,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.substring(index), radix);

This comment has been minimized.

@cl4es

cl4es Aug 10, 2021
Member Outdated

Possibly a follow-up, but I think using parseInt/-Long(nm, index, nm.length(), radix) could give an additional speed-up in these cases (by avoiding a substring allocation).

This comment has been minimized.

@stsypanov

stsypanov Aug 10, 2021
Author Contributor Outdated

Good point! Let me check this.

This comment has been minimized.

@stsypanov

stsypanov Aug 10, 2021
Author Contributor Outdated

Indeed, looks like getting rid of substring() call makes things faster:

before

Benchmark                    (size)  Mode  Cnt   Score   Error  Units
Integers.decode                 500  avgt   15  11.444 ? 0.949  us/op
Integers.parseInt               500  avgt   15   8.669 ? 0.152  us/op
Integers.toStringBig            500  avgt   15  10.295 ? 0.612  us/op
Integers.toStringSmall          500  avgt   15   7.020 ? 0.581  us/op

Benchmark                    (size)  Mode  Cnt   Score   Error  Units
Longs.decode                    500  avgt   15  29.568 ? 9.785  us/op
Longs.repetitiveSubtraction     500  avgt   15   0.820 ? 0.153  us/op
Longs.toStringBig               500  avgt   15  13.418 ? 0.744  us/op
Longs.toStringSmall             500  avgt   15   8.180 ? 0.780  us/op


after

Benchmark                    (size)  Mode  Cnt   Score   Error  Units
Integers.decode                 500  avgt   15   7.377 ? 0.040  us/op
Integers.parseInt               500  avgt   15   8.720 ? 0.230  us/op
Integers.toStringBig            500  avgt   15  10.328 ? 0.266  us/op
Integers.toStringSmall          500  avgt   15   6.913 ? 0.178  us/op

Benchmark                    (size)  Mode  Cnt   Score   Error  Units
Longs.decode                    500  avgt   15   8.373 ? 0.708  us/op
Longs.repetitiveSubtraction     500  avgt   15   0.771 ? 0.003  us/op
Longs.toStringBig               500  avgt   15  13.126 ? 0.079  us/op
Longs.toStringSmall             500  avgt   15   6.915 ? 0.259  us/op
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;
}
@@ -1269,7 +1269,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");
@@ -1299,15 +1299,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.substring(index), 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;
}
@@ -264,8 +264,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);
}