On Wed, 13 Mar 2024 09:52:45 GMT, Shaojin Wen <d...@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:
> 
>   fix benchmark

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

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

This looks gnarly, and it's unclear if it's correct for invalid inputs like 
`""`, `"-"` and `"+"` since you're not testing for `len > 0` before going into 
the loop. Can you make sure there are tests covering such inputs and try to 
simplify this?

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

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

Reply via email to