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[]
// ...
}
We can eliminate these two memory allocations to enhance performance.
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[]
// ...
}
We can eliminate these two memory allocations to enhance performance.
- links to
-
Review openjdk/jdk/18177