RFR: 8347009: Speed up parseInt and parseLong [v6]
Raffaello Giulietti
rgiulietti at openjdk.org
Fri Jan 24 17:23:52 UTC 2025
On Tue, 14 Jan 2025 15:53:36 GMT, Shaojin Wen <swen at openjdk.org> wrote:
>> This is an optimization for decimal Integer.parseInt and Long.parseLong, which improves performance by about 10%. The optimization includes:
>> 1. Improve performance by parsing 2 numbers at a time, which has performance improvements for numbers with length >= 3.
>> 2. It uses charAt(0) for the first number. Assuming that the optimization can eliminate boundary checks, this will be more friendly to parsing numbers with length 1.
>> 3. It removes the reliance on the Character.digit method and eliminates the reliance on the CharacterDataLatin1#DIGITS cache array, which avoids performance degradation caused by cache misses.
>
> Shaojin Wen has updated the pull request incrementally with two additional commits since the last revision:
>
> - remove unused
> - Update src/java.base/share/classes/jdk/internal/util/DecimalDigits.java
>
> Co-authored-by: Chen Liang <liach at openjdk.org>
I believe the following implementation of `Integer.parseInt(String,int)` is correct, shorter, and more readable.
On my M1 Pro, the benchmark is even slightly faster than the proposed one, but I didn't check on other platforms.
The same holds for `Long.parseLong(String,int)` (simply copy the text and change the type or `result` to be `long`).
Please have a look.
public static int parseInt(String s, int radix)
throws NumberFormatException {
int len;
byte[] value;
if (s == null || radix != 10 || (len = (value = s.value()).length) == 0 || !s.isLatin1()) {
return parseInt0(s, radix);
}
int fc = value[0];
int result = Integer.isDigitLatin1(fc)
? '0' - fc
: len != 1 && (fc == '-' || fc == '+')
? 0
: 1; // or any value > 0
int i = 1;
int d;
while (i + 1 < len
&& (d = DecimalDigits.digit2(value, i)) != -1
&& MIN_VALUE / 100 <= result) {
result = result * 100 - d; // overflow from d => result > 0
i += 2;
}
if (i < len
&& Integer.isDigitLatin1(d = value[i])
&& MIN_VALUE / 10 <= result && result <= 0) {
result = result * 10 + '0' - d; // overflow from '0' - d => result > 0
i += 1;
}
if (i == len
& result <= 0
& (MIN_VALUE < result || fc == '-')) {
return fc == '-' ? result : -result;
}
throw NumberFormatException.forInputString(s);
}
-------------
PR Comment: https://git.openjdk.org/jdk/pull/22919#issuecomment-2613033520
More information about the core-libs-dev
mailing list