On Tue, 12 Mar 2024 10:34:33 GMT, Shaojin Wen <[email protected]> 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:
>
> refactor CharArraySequence
src/java.base/share/classes/java/math/BigInteger.java line 608:
> 606: * Constructs a new BigInteger using a char array with radix=10.
> 607: * Sign is precalculated outside and not allowed in the val. The
> {@code val}
> 608: * array is assumed to be unchanged for the duration of the
> constructor
This removed comment about the array being assumed to be unchanged is still
applicable to the `CharSequence` (since it'll be backed by the passed in
`char[]`).
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/18177#discussion_r1521309524