Skip to content

8315585: Optimization for decimal to string #23310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 31 commits into
base: master
Choose a base branch
from

Conversation

wenshao
Copy link
Contributor

@wenshao wenshao commented Jan 25, 2025

Continue to complete PR #16006 and PR #21593 to improve BigDecimal::toString and BigDecimal::toPlainString performance and reduce duplicate code


Progress

  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change must be properly reviewed (2 reviews required, with at least 2 Reviewers)

Warnings

 ⚠️ Patch contains a binary file (src/java.desktop/share/classes/javax/swing/beaninfo/images/JAppletColor16.gif)
 ⚠️ Patch contains a binary file (src/java.desktop/share/classes/javax/swing/beaninfo/images/JAppletColor32.gif)
 ⚠️ Patch contains a binary file (src/java.desktop/share/classes/javax/swing/beaninfo/images/JAppletMono16.gif)
 ⚠️ Patch contains a binary file (src/java.desktop/share/classes/javax/swing/beaninfo/images/JAppletMono32.gif)
 ⚠️ Patch contains a binary file (src/java.desktop/share/classes/javax/swing/doc-files/JRootPane-1.gif)
 ⚠️ Patch contains a binary file (test/jdk/javax/rmi/ssl/keystore)
 ⚠️ Patch contains a binary file (test/jdk/javax/rmi/ssl/truststore)
 ⚠️ Patch contains a binary file (test/jdk/jdk/internal/loader/URLClassPath/testclasses.jar)
 ⚠️ Patch contains a binary file (test/jdk/sun/net/www/protocol/https/HttpsClient/dnsstore)
 ⚠️ Patch contains a binary file (test/jdk/sun/net/www/protocol/https/HttpsClient/ipstore)

Issue

  • JDK-8315585: Optimization for decimal to string (Enhancement - P4)

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/23310/head:pull/23310
$ git checkout pull/23310

Update a local copy of the PR:
$ git checkout pull/23310
$ git pull https://git.openjdk.org/jdk.git pull/23310/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 23310

View PR using the GUI difftool:
$ git pr show -t 23310

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/23310.diff

Using Webrev

Link to Webrev Comment

@wenshao wenshao marked this pull request as draft January 25, 2025 07:26
@bridgekeeper
Copy link

bridgekeeper bot commented Jan 25, 2025

👋 Welcome back swen! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Jan 25, 2025

❗ This change is not yet ready to be integrated.
See the Progress checklist in the description for automated requirements.

@openjdk
Copy link

openjdk bot commented Jan 25, 2025

@wenshao The following label will be automatically applied to this pull request:

  • core-libs

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the core-libs core-libs-dev@openjdk.org label Jan 25, 2025
* @param buf target buffer, UTF16-coded.
* @return index of the most significant digit or minus sign, if present
*/
public static int getChars(long i, int index, char[] buf) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before dropping this method, there is another candidate to use it here :

private void developLongDigits( int decExponent, long lvalue, int insignificantDigits ){

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took a look at the FloatingDecimal related code. The relevant code in FloatingDecimal can be refactored and simplified so that the removed code does not need to be used.

I submitted a PR to simplify FloatingDecimal #23311

@liach
Copy link
Member

liach commented Jan 26, 2025

A great cleanup that consolidates scale2, unscaledAbsString, and unscaledString.

} else if (insertionPoint > 0) { /* Point goes inside intVal */
buf = new StringBuilder(intString);
buf.insert(insertionPoint, '.');
buf = new StringBuilder();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could calculate the precise size for the StringBuilder

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The performance will degrade if you precompute the length of the StringBuilder.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That’s interesting - should the precomputed lengths be removed in the other ones as well?

buf.insert(0, '-');
buf.append('-');
buf.append(intString)
.insert(insertionPoint + (signum < 0 ? 1 : 0), '.');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of the insert, could do an append of the prefix, then dot, then the suffix.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

buf.append(intString, 0, insertionPoint)
   .append('.')
   .append(intString, insertionPoint, intString.length());

This is another way to write it, but the performance will be reduced.

}

private String layoutCharsE(boolean sci, String coeff, int coeffLen, long adjusted) {
StringBuilder buf = new StringBuilder(32);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@wenshao wenshao changed the title Optimization for decimal to string 8315585 : Optimization for decimal to string Jan 27, 2025
@wenshao wenshao changed the title 8315585 : Optimization for decimal to string 8315585: Optimization for decimal to string Jan 27, 2025
# Conflicts:
#	src/java.base/share/classes/jdk/internal/util/DecimalDigits.java
# Conflicts:
#	src/java.base/share/classes/jdk/internal/util/DecimalDigits.java
}
return buf.toString();
}

private static String scale2(int intCompact) {
int highInt = intCompact / 100;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something to experiment with here:

        int highInt = intCompact / 100;
        int lowInt = intCompact - highInt * 100;
        short packed=DecimalDigits.pair(lowInt);

        return new StringBuilder()
                .append(highInt)
                .append('.')
                .append((char) (packed & 0xFF))
                .append((char) (packed >> 8))
                .toString();

C2 seems to be able to optimize out the SB here, so it might do as well as newStringNoRepl.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good suggestion, but DecimalDigits no longer provides a pair method

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right - you’d need to add it back to try this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested it on a MacBook M1 Max machine and the results were different from what you said. Using StringBuilder + digitPair would degrade performance.

git remote add wenshao git@github.com:wenshao/jdk.git
git fetch wenshao

# use JLA
git checkout f9af0b0203145b95d82a2067a10bde61b0915dd7
make test TEST="micro:java.math.BigDecimals.smallScale2"

# use digitPair
git checkout 4124121933e08ab185f3f879856197ed15caafd7
make test TEST="micro:java.math.BigDecimals.smallScale2"
Benchmark                                   Mode  Cnt  Score   Error  Units (f9af0b02031)
BigDecimals.smallScale2EngineeringToString  avgt   15  9.658 ? 0.228  ns/op
BigDecimals.smallScale2LayoutCharsToString  avgt   15  9.597 ? 0.047  ns/op
BigDecimals.smallScale2PlainToString        avgt   15  9.759 ? 0.054  ns/op


Benchmark                                   Mode  Cnt   Score   Error  Units (4124121933e)
BigDecimals.smallScale2EngineeringToString  avgt   15  18.763 ? 0.332  ns/op
BigDecimals.smallScale2LayoutCharsToString  avgt   15  18.738 ? 0.214  ns/op
BigDecimals.smallScale2PlainToString        avgt   15  18.992 ? 0.226  ns/op

@jddarcy
Copy link
Member

jddarcy commented Feb 4, 2025

Can we please have a pause on the sequence of "make XYZ toString faster" PRs until there is some wider discussion of goals, etc.? Thanks.

@mlbridge
Copy link

mlbridge bot commented Feb 4, 2025

Mailing list message from Archie Cobbs on core-libs-dev:

On Tue, Feb 4, 2025 at 2:40?PM Joe Darcy <darcy at openjdk.org> wrote:

Can we please have a pause on the sequence of "make XYZ toString faster"
PRs until there is some wider discussion of goals, etc.? Thanks.

I agree with this sentiment... It was surprising to see how easily a VM
crash can sneak in.

There is always a trade-off between A and B, where:

A = Code clarity, robustness vs. future changes, friendliness to new
developers, minimizing obscure bugs (and security holes), etc...
B = Performance

Where should the line be drawn? Personally (as a Java user) I'd accept 1%
slower vs. 1% less likely to crash any day...

Performance is important but there should be some general guidelines and
maybe some specific policies. E.g. should there be a higher number of
reviews required whenever Unsafe is used purely for performance reasons?

It's also worth pondering what's implied by the Java team evangelizing to
the rest of the world to stop using Unsafe, while at the same time adding
it more and more ourselves (when not strictly required). In theory we
should instead be eating our own dog food (or better yet, improving its
quality).

Also: when does it become more appropriate to address a performance issue
in Hotspot instead of in Java source? If some optimization eliminates an
array range check that is clearly not needed, it might be feasible (and
much more widely beneficial) to teach Hotstpot how to figure that out
itself, etc.

Just some random thoughts...

-Archie

--
Archie L. Cobbs
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20250204/116c8508/attachment.htm>

@wenshao
Copy link
Contributor Author

wenshao commented Feb 4, 2025

I think you are talking about the problem of PR #23420, which is caused by the use of thread-unsafe StringBuilder in multi-threaded scenarios. This problem is very obscure and I didn't consider it before. I have started to solve this problem and have submitted PR #23427. After it is completed, I will continue to submit PR to redo PR #19626 in a thread-safe way.

The above problem does not affect toString, because it only occurs when StringBuilder is used in a multi-threaded scenario.

@AlanBateman
Copy link
Contributor

/reviewers 2 reviewer

@openjdk
Copy link

openjdk bot commented Feb 5, 2025

@AlanBateman
The total number of required reviews for this PR (including the jcheck configuration and the last /reviewers command) is now set to 2 (with at least 2 Reviewers).

@mlbridge
Copy link

mlbridge bot commented Feb 7, 2025

Mailing list message from Archie Cobbs on core-libs-dev:

On Tue, Feb 4, 2025 at 5:26?PM Shaojin Wen <swen at openjdk.org> wrote:

I think you are talking about the problem of PR #23420, which is caused by
the use of thread-unsafe StringBuilder in multi-threaded scenarios. This
problem is very obscure and I didn't consider it before. I have started to
solve this problem and have submitted PR #23427. After it is completed, I
will continue to submit PR to redo PR #19626 in a thread-safe way.

Yes - apologies if it sounded like I was trying to single you out. The
optimizations you've been doing are looking great. It's just that this
example is a good data point in the larger discussion about what the
general policy should be, etc.

The above problem does not affect toString, because it only occurs when
StringBuilder is used in a multi-threaded scenario.

Good point, but frankly, an irrelevant one. The key issue here is that if
plain, ordinary, non-native-invoking Java bytecode can corrupt memory
and/or crash the JVM, then that's a Big Problem??. It doesn't matter how
contrived the code that makes it happen is.

-Archie

--
Archie L. Cobbs
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20250207/67028683/attachment.htm>

@mlbridge
Copy link

mlbridge bot commented Feb 8, 2025

Mailing list message from Paul Sandoz on core-libs-dev:

I would like to amplify this point ? undermining Java?s integrity is a big deal. Every time we use unsafe mechanisms within the JDK we risk doing that. The more complex such code is the harder it is reason about whether overall it is safe [*]. We need to balance reasoning about code, quality, and maintenance of against narrowly measured performance benefits that increase the risk of some integrity violation.

Paul.

[*] And even if it is not so complex, others may not be aware of the subtleties when refactoring. Unsafe allocation that does not zero memory is particular worrisome in this regard.

On Feb 7, 2025, at 7:42?AM, Archie Cobbs <archie.cobbs at gmail.com> wrote:
Good point, but frankly, an irrelevant one. The key issue here is that if plain, ordinary, non-native-invoking Java bytecode can corrupt memory and/or crash the JVM, then that's a Big Problem??. It doesn't matter how contrived the code that makes it happen is.

-Archie

--
Archie L. Cobbs

@bridgekeeper
Copy link

bridgekeeper bot commented Mar 5, 2025

@wenshao This pull request has been inactive for more than 4 weeks and will be automatically closed if another 4 weeks passes without any activity. To avoid this, simply add a new comment to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration!

@wenshao
Copy link
Contributor Author

wenshao commented Mar 9, 2025

Keep it alive.

@bridgekeeper
Copy link

bridgekeeper bot commented Apr 6, 2025

@wenshao This pull request has been inactive for more than 4 weeks and will be automatically closed if another 4 weeks passes without any activity. To avoid this, simply add a new comment to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration!

@bridgekeeper
Copy link

bridgekeeper bot commented May 4, 2025

@wenshao This pull request has been inactive for more than 8 weeks and will now be automatically closed. If you would like to continue working on this pull request in the future, feel free to reopen it! This can be done using the /open pull request command.

@bridgekeeper bridgekeeper bot closed this May 4, 2025
# Conflicts:
#	src/java.base/share/classes/java/math/BigDecimal.java
#	src/java.base/share/classes/jdk/internal/util/DecimalDigits.java
#	test/micro/org/openjdk/bench/java/math/BigDecimals.java
@wenshao
Copy link
Contributor Author

wenshao commented May 25, 2025

/open

@openjdk openjdk bot reopened this May 25, 2025
@openjdk
Copy link

openjdk bot commented May 25, 2025

@wenshao This pull request is now open

@wenshao wenshao marked this pull request as draft May 25, 2025 09:47
@openjdk openjdk bot removed the rfr Pull request is ready for review label May 25, 2025
@bridgekeeper
Copy link

bridgekeeper bot commented Jul 20, 2025

@wenshao This pull request has been inactive for more than 8 weeks and will be automatically closed if another 8 weeks passes without any activity. To avoid this, simply issue a /touch or /keepalive command to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration!

@wenshao
Copy link
Contributor Author

wenshao commented Jul 20, 2025

/touch

@openjdk
Copy link

openjdk bot commented Jul 20, 2025

@wenshao The pull request is being re-evaluated and the inactivity timeout has been reset.

@openjdk
Copy link

openjdk bot commented Aug 20, 2025

@wenshao this pull request can not be integrated into master due to one or more merge conflicts. To resolve these merge conflicts and update this pull request you can run the following commands in the local repository for your personal fork:

git checkout dec_to_str_202501
git fetch https://git.openjdk.org/jdk.git master
git merge FETCH_HEAD
# resolve conflicts and follow the instructions given by git merge
git commit -m "Merge master"
git push

@openjdk openjdk bot added the merge-conflict Pull request has merge conflict with target branch label Aug 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core-libs core-libs-dev@openjdk.org merge-conflict Pull request has merge conflict with target branch
Development

Successfully merging this pull request may close these issues.

5 participants