RFR: 8347009: Speed up parseInt and parseLong [v24]
Shaojin Wen
swen at openjdk.org
Thu Dec 4 14:45:12 UTC 2025
On Thu, 4 Dec 2025 14:02:16 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 one additional commit since the last revision:
>
> remove unsafe
In my latest implementation, I removed `Unsafe` from the `DecimalDigits::digit2` method, which resulted in a performance decrease, but it's still better than the master branch. Below are my performance tests on x64 and aarch64:
# 1. Test Script
git remote add wenshao git at github.com:wenshao/jdk.git
git fetch wenshao
# base (master banch)
git checkout 9ea0dcd3eaf07658aeb92ba0defb5098e1f7b192
make test TEST="micro:java.lang.Longs.parseLong"
make test TEST="micro:java.lang.Integers.parseInt"
# Digit2 (current)
git checkout cebefd7b89f2dbf29bb13c4903060707a5574e67
make test TEST="micro:java.lang.Longs.parseLong"
make test TEST="micro:java.lang.Integers.parseInt"
# Digit2_Unsafe (before)
git checkout c58aa7071867f386407f33defe2b346176fc1e91
make test TEST="micro:java.lang.Longs.parseLong"
make test TEST="micro:java.lang.Integers.parseInt"
# 2. Performance Results on MacBook M1 Pro
* Raw Data
# base
Benchmark (size) Mode Cnt Score Error Units
Integers.parseInt 500 avgt 15 2.371 ± 0.012 us/op
Longs.parseLong 500 avgt 15 2.731 ± 0.030 us/op
# digit2
Benchmark (size) Mode Cnt Score Error Units
Integers.parseInt 500 avgt 15 2.221 ± 0.009 us/op
Longs.parseLong 500 avgt 15 2.517 ± 0.016 us/op
# digit2_unsafe
Benchmark (size) Mode Cnt Score Error Units
Integers.parseInt 500 avgt 15 2.114 ± 0.012 us/op
Longs.parseLong 500 avgt 15 2.344 ± 0.028 us/op
* Comparison
| Benchmark | base | Digit2 | Digit2_Unsafe | base vs Digit2 (%) | Digit2 vs Digit2_Unsafe (%) |
|-----------|--------|--------|---------------|----------------------|------------------------------|
| Integers.parseInt | 2.371 | 2.221 | 2.114 | +6.33% | +4.82% |
| Longs.parseLong | 2.731 | 2.517 | 2.344 | +7.84% | +6.87% |
# 3. Performance Results on Aliyun_ECS_c9a (x64, AMD AMD EPYC™ Turin)
* Row Data
# base
Benchmark (size) Mode Cnt Score Error Units
Integers.parseInt 500 avgt 15 2.541 ± 0.022 us/op
Longs.parseLong 500 avgt 15 2.941 ± 0.014 us/op
# digit2
Benchmark (size) Mode Cnt Score Error Units
Integers.parseInt 500 avgt 15 2.542 ± 0.003 us/op
Longs.parseLong 500 avgt 15 2.527 ± 0.014 us/op
# digit2_unsafe
Benchmark (size) Mode Cnt Score Error Units
Integers.parseInt 500 avgt 15 2.244 ± 0.010 us/op
Longs.parseLong 500 avgt 15 2.432 ± 0.033 us/op
* Comparison
| benchmark | master | digit2 | digit2_unsafe | master vs digit2 | digit2 vs digit2_unsafe |
|-----------|--------|--------|---------------|------------------|------------------------|
| Integers.parseInt | 2.541 | 2.542 | 2.244 | -0.04% | 13.28% |
| Longs.parseLong | 2.941 | 2.527 | 2.432 | 16.38% | 3.91% |
-------------
PR Comment: https://git.openjdk.org/jdk/pull/22919#issuecomment-3612587178
More information about the core-libs-dev
mailing list