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

j3graham duke at openjdk.org
Sun Jan 5 07:10:08 UTC 2025


On Sun, 5 Jan 2025 07:07:15 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 11 additional commits since the last revision:
> 
>  - emptyInput -> nullInput
>  - use CharacterDataLatin1.instance::isDigit
>  - add comments
>  - reduce codeSize
>  - bug fix for bound check
>  - reduce codeSize
>  - remove unused code
>  - code format
>  - bug fix
>  - bug fix
>  - ... and 1 more: https://git.openjdk.org/jdk/compare/9ea08455...fd51c1ce

src/java.base/share/classes/java/lang/Integer.java line 576:

> 574:             throw NumberFormatException.forInputString(s);
> 575:         }
> 576:         return parseInt(s, 0, len, radix);

I  think this call will result in different exception messages in some cases. Consolidating the exception messages between the String and the CharSequence parsing methods would likely allow for even more code simplification,  but currently it appears there was effort to preserve the exact message text in exceptions.

src/java.base/share/classes/java/lang/NumberFormatException.java line 96:

> 94:     }
> 95: 
> 96:     static NumberFormatException emptyInput() {

A better method name might be nullInput, since an empty (but non null) string doesn’t use this

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

PR Review Comment: https://git.openjdk.org/jdk/pull/22919#discussion_r1903186757
PR Review Comment: https://git.openjdk.org/jdk/pull/22919#discussion_r1903186179


More information about the core-libs-dev mailing list