On Tue, 14 Jan 2025 15:53:36 GMT, Shaojin Wen <s...@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 <li...@openjdk.org> Correction: I forgot to add a check for `result <= 0` in the `while` condition. Without it, `Integer.parseInt("0214748369900")` wrongly returns `5100`, for example. The correction has no statistical impact on the benchmarks on my platform. 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 <= 0) { 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-2613166860