RFR: 8310929: Optimization for Integer.toString [v3]

Chen Liang liach at openjdk.org
Fri Jun 30 02:40:54 UTC 2023


On Fri, 30 Jun 2023 01:03:30 GMT, 温绍锦 <duke at openjdk.org> wrote:

>> Speed up Integer.toString with a precomputed cache array of length 1000, use division by 1000, so that each iteration can calculate three digits.  
>> 
>> this optimization can also be used in StringBuilder#appent(int) and Long#toString.
>> 
>> # Benchmark Result
>> 
>> 
>> bash configure --with-jmh=build/jmh/jars
>> make test TEST="micro:java.lang.Integers.toString*"
>> 
>> 
>> ## 1. [aliyun_ecs_c8i.xlarge](https://help.aliyun.com/document_detail/25378.html#c8i)
>> * cpu : intel xeon sapphire rapids (x64)
>> ``` diff
>> -Benchmark               (size)  Mode  Cnt  Score   Error  Units (baseline)
>> -Integers.toStringBig       500  avgt   15  6.811 ± 0.016  us/op
>> -Integers.toStringSmall     500  avgt   15  4.794 ± 0.011  us/op
>> -Integers.toStringTiny      500  avgt   15  3.752 ± 0.065  us/op
>> 
>> +Benchmark               (size)  Mode  Cnt  Score   Error  Units (PR Update 01 3355d211)
>> +Integers.toStringBig       500  avgt   15  5.148 ± 0.044  us/op (+32.30%)
>> +Integers.toStringSmall     500  avgt   15  3.820 ± 0.016  us/op (+25.48%)
>> +Integers.toStringTiny      500  avgt   15  3.426 ± 0.010  us/op (+9.51%)
>> 
>> +Benchmark               (size)  Mode  Cnt  Score   Error  Units (PR Update 02 62b499d6)
>> +Integers.toStringBig       500  avgt   15  5.971 ± 0.014  us/op (+14.06%)
>> +Integers.toStringSmall     500  avgt   15  4.346 ± 0.016  us/op (+10.30%)
>> +Integers.toStringTiny      500  avgt   15  3.671 ± 0.019  us/op (+2.20%)
>> 
>> 
>> ## 2. [aliyun_ecs_c8a.xlarge](https://help.aliyun.com/document_detail/25378.html#c8a)
>> * cpu : amd epc genoa (x64)
>> ``` diff
>> -Benchmark               (size)  Mode  Cnt  Score   Error  Units (baseline)
>> -Integers.toStringBig       500  avgt   15  6.762 ± 0.018  us/op
>> -Integers.toStringSmall     500  avgt   15  4.481 ± 0.004  us/op
>> -Integers.toStringTiny      500  avgt   15  2.761 ± 0.012  us/op
>> 
>> +Benchmark               (size)  Mode  Cnt  Score   Error  Units (PR Update 01 3355d211)
>> +Integers.toStringBig       500  avgt   15  4.901 ± 0.005  us/op (+37.97%)
>> +Integers.toStringSmall     500  avgt   15  3.515 ± 0.204  us/op (+27.48%)
>> +Integers.toStringTiny      500  avgt   15  2.698 ± 0.190  us/op (+2.33%)
>> 
>> +Benchmark               (size)  Mode  Cnt  Score   Error  Units  (PR Update 02 62b499d6)
>> +Integers.toStringBig       500  avgt   15  5.493 ± 0.007  us/op (+23.10%)
>> +Integers.toStringSmall     500  avgt   15  3.714 ± 0.006  us/op (+20.65%)
>> +Integers.toStringTiny      500  avgt   15  2.786 ± 0.015  us/op (-0.89...
>
> 温绍锦 has updated the pull request incrementally with one additional commit since the last revision:
> 
>   improve based on getChars

Changes requested by liach (Author).

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

> 550:             i = q;
> 551:             charPos -= 2;
> 552:             Unsafe.getUnsafe().putShortUnaligned(buf, Unsafe.ARRAY_BYTE_BASE_OFFSET + charPos, DigitPacks[r]);

Suggestion:

            Unsafe.getUnsafe().putShortUnaligned(buf, Unsafe.ARRAY_BYTE_BASE_OFFSET + charPos, DigitPacks[r], false);

You should explicitly specify the endianness, which is platform dependent.

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

PR Review: https://git.openjdk.org/jdk/pull/14699#pullrequestreview-1506384387
PR Review Comment: https://git.openjdk.org/jdk/pull/14699#discussion_r1247350891


More information about the core-libs-dev mailing list