Skip to content

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)

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
@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
# Conflicts:
#	src/java.base/share/classes/java/math/BigDecimal.java
@openjdk openjdk bot removed the merge-conflict Pull request has merge conflict with target branch label Aug 23, 2025
…Latin1Bytes

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
@bridgekeeper
Copy link

bridgekeeper bot commented Oct 19, 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 Oct 20, 2025

/touch

@openjdk
Copy link

openjdk bot commented Oct 20, 2025

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

@bridgekeeper
Copy link

bridgekeeper bot commented Dec 15, 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 Dec 15, 2025

/touch

@openjdk
Copy link

openjdk bot commented Dec 15, 2025

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

buf.append(coeff.charAt(0)); // first character
if (coeffLen > 1) { // more to come
buf.append('.')
.append(coeff, 1, coeffLen);
Copy link
Member

Choose a reason for hiding this comment

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

Is this right that the old end is coeffLen - 1 but the new end is coeffLen? I don't see why there should be no more -1.

@wenshao wenshao requested a review from liach December 16, 2025 06:23
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

Development

Successfully merging this pull request may close these issues.

5 participants