RFR: 8347009: Speed ​​up parseInt and parseLong [v6]

Shaojin Wen swen at openjdk.org
Fri Jan 24 19:02:47 UTC 2025


On Fri, 24 Jan 2025 18:42:44 GMT, Raffaello Giulietti <rgiulietti at openjdk.org> wrote:

> 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);
>     }
> ```

Why is `&` used here instead of `&&`?

-------------

PR Comment: https://git.openjdk.org/jdk/pull/22919#issuecomment-2613194796


More information about the core-libs-dev mailing list