RFR: 8315585: Optimization for decimal to string
Johannes Graham
duke at openjdk.org
Tue Jan 28 08:12:29 UTC 2025
On Sat, 25 Jan 2025 07:25:40 GMT, Shaojin Wen <swen at openjdk.org> wrote:
> Continue to complete PR #16006 and PR #21593 to improve BigDecimal::toString and BigDecimal::toPlainString performance and reduce duplicate code
src/java.base/share/classes/java/math/BigDecimal.java line 3538:
> 3536: return (signum < 0 ? "-0." : "0.").concat(intString);
> 3537: } else if (insertionPoint > 0) { /* Point goes inside intVal */
> 3538: buf = new StringBuilder();
Could calculate the precise size for the StringBuilder
src/java.base/share/classes/java/math/BigDecimal.java line 3542:
> 3540: buf.append('-');
> 3541: buf.append(intString)
> 3542: .insert(insertionPoint + (signum < 0 ? 1 : 0), '.');
Instead of the insert, could do an append of the prefix, then dot, then the suffix.
src/java.base/share/classes/java/math/BigDecimal.java line 4209:
> 4207:
> 4208: private String layoutCharsE(boolean sci, String coeff, int coeffLen, long adjusted) {
> 4209: StringBuilder buf = new StringBuilder(32);
The comment above about constructing a buffer probably belongs here. Also could calculate a better approximation of the size - possibly coeffLen+14 if that comment is accurate.
src/java.base/share/classes/jdk/internal/util/DecimalDigits.java line 347:
> 345: * @return index of the most significant digit or minus sign, if present
> 346: */
> 347: public static int getChars(long i, int index, char[] buf) {
Before dropping this method, there is another candidate to use it here : https://github.com/openjdk/jdk/blob/1d2eb2fbaea700fc77b644b5eb5a8a7c40ede108/src/java.base/share/classes/jdk/internal/math/FloatingDecimal.java#L367
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/23310#discussion_r1929857010
PR Review Comment: https://git.openjdk.org/jdk/pull/23310#discussion_r1929857288
PR Review Comment: https://git.openjdk.org/jdk/pull/23310#discussion_r1929860496
PR Review Comment: https://git.openjdk.org/jdk/pull/23310#discussion_r1929495730
More information about the core-libs-dev
mailing list