RFR: 8327791: Optimization for new BigDecimal(String) [v4]

Shaojin Wen duke at openjdk.org
Mon Mar 11 20:58:13 UTC 2024


On Mon, 11 Mar 2024 20:41:25 GMT, Shaojin Wen <duke at openjdk.org> wrote:

>> The current BigDecimal(String) constructor calls String#toCharArray, which has a memory allocation.
>> 
>> 
>> public BigDecimal(String val) {
>>     this(val.toCharArray(), 0, val.length()); // allocate char[]
>> }
>> 
>> 
>> When the length is greater than 18, create a char[]
>> 
>> 
>> boolean isCompact = (len <= MAX_COMPACT_DIGITS); // 18
>> if (!isCompact) {
>>     // ...
>> } else {
>>     char[] coeff = new char[len]; // allocate char[]
>>     // ...
>> }
>> 
>> 
>> This PR eliminates the two memory allocations mentioned above, resulting in an approximate 60% increase in performance..
>
> Shaojin Wen has updated the pull request incrementally with one additional commit since the last revision:
> 
>   1. bug fix for CharBuffer catch IndexOutOfBoundsException
>   2. reorder if statement
>   3. Improve the performance of !isCompact branch

In (03 [#bb45da4d](https://github.com/openjdk/jdk/pull/18177/files/bb45da4da1c0fcd79db85dc9c1ce17b6b3dfd8a3) ) I have fixed the build error and further optimized the branch of !isCompact, so that the performance in the BigDecimal(char[]) scenario is better than the master version.


-Benchmark                                      Mode  Cnt    Score   Error  Units #master
-BigDecimals.testConstructorWithSmallCharArray  avgt   15   16.488 ? 0.054  ns/op
-BigDecimals.testConstructorWithLargeCharArray  avgt   15   90.583 ? 1.523  ns/op
-BigDecimals.testConstructorWithHugeCharArray   avgt   15   90.683 ? 1.623  ns/op
-BigDecimals.testConstructorWithCharArray       avgt   15   47.418 ? 0.473  ns/op
-BigDecimals.testConstructorWithSmallString     avgt   15   19.725 ? 0.049  ns/op
-BigDecimals.testConstructorWithLargeString     avgt   15  113.567 ? 1.470  ns/op
-BigDecimals.testConstructorWithHugeString      avgt   15  119.712 ? 6.230  ns/op
-BigDecimals.testConstructorWithString          avgt   15   67.046 ? 0.979  ns/op


+Benchmark                                      Mode  Cnt    Score   Error  Units (01 #61b5531b)
+BigDecimals.testConstructorWithSmallCharArray  avgt   15   14.322 ? 0.063  ns/op +15.12%
+BigDecimals.testConstructorWithLargeCharArray  avgt   15   74.090 ? 0.299  ns/op +22.26%
+BigDecimals.testConstructorWithHugeCharArray   avgt   15   74.372 ? 0.461  ns/op +21.93%
+BigDecimals.testConstructorWithCharArray       avgt   15   41.606 ? 0.284  ns/op +13.96%
+BigDecimals.testConstructorWithSmallString     avgt   15   15.019 ? 0.100  ns/op +31.33%
+BigDecimals.testConstructorWithLargeString     avgt   15   70.226 ? 0.240  ns/op +61.71%
+BigDecimals.testConstructorWithHugeString      avgt   15   70.153 ? 0.455  ns/op +70.64%
+BigDecimals.testConstructorWithString          avgt   15   40.064 ? 0.298  ns/op +67.36%


+Benchmark                                      Mode  Cnt    Score   Error  Units (03 #bb45da4d)
+BigDecimals.testConstructorWithSmallCharArray  avgt   15   22.450 ? 0.334  ns/op -26.55%
+BigDecimals.testConstructorWithLargeCharArray  avgt   15   88.087 ? 1.393  ns/op +2.83%
+BigDecimals.testConstructorWithHugeCharArray   avgt   15   87.643 ? 1.081  ns/op +3.46%
+BigDecimals.testConstructorWithCharArray       avgt   15   51.357 ? 2.389  ns/op -7.66%
+BigDecimals.testConstructorWithSmallString     avgt   15   16.892 ? 0.377  ns/op +11.78%
+BigDecimals.testConstructorWithLargeString     avgt   15   65.103 ? 0.219  ns/op +74.44%
+BigDecimals.testConstructorWithHugeString      avgt   15   64.475 ? 0.464  ns/op +85.67%
+BigDecimals.testConstructorWithString          avgt   15   36.582 ? 0.286  ns/op +83.27%

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

PR Comment: https://git.openjdk.org/jdk/pull/18177#issuecomment-1989429207


More information about the core-libs-dev mailing list