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

Shaojin Wen duke at openjdk.org
Thu Mar 21 00:43:24 UTC 2024


On Wed, 20 Mar 2024 22:56:38 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:
> 
>   use while instead for

Should we declare the BigDecimal(CharSequence,MathContext) method as public? Scenarios like [MysqlBinaryValueDecoder#decodeDecimal](https://github.com/mysql/mysql-connector-j/blob/release/8.x/src/main/protocol-impl/java/com/mysql/cj/protocol/a/MysqlBinaryValueDecoder.java#L275) can be used to avoid a memory allocation.

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

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


More information about the core-libs-dev mailing list