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

Claes Redestad redestad at openjdk.org
Wed Mar 20 21:04:27 UTC 2024


On Thu, 14 Mar 2024 00:00:53 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:
> 
>   bug fix for CharArraySequence#charAt

src/java.base/share/classes/java/math/BigDecimal.java line 598:

> 596:                 // First compact case, we need not to preserve the character
> 597:                 // and we can just compute the value in place.
> 598:                 for (; ; c = val.charAt(++offset)) {

This needs to be simplified. Let's at least do this, which is more honest: 

                while (true) {
                    ...
                    c = val.charAt(++offset);
                }

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

PR Review Comment: https://git.openjdk.org/jdk/pull/18177#discussion_r1532864646


More information about the core-libs-dev mailing list