-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
base: master
Are you sure you want to change the base?
Conversation
👋 Welcome back swen! A progress list of the required criteria for merging this PR into |
❗ This change is not yet ready to be integrated. |
* @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) { |
There was a problem hiding this comment.
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 ){ |
There was a problem hiding this comment.
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
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(); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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), '.'); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.
This reverts commit ddc2a86.
# 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; |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
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. |
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:
I agree with this sentiment... It was surprising to see how easily a VM There is always a trade-off between A and B, where: A = Code clarity, robustness vs. future changes, friendliness to new Where should the line be drawn? Personally (as a Java user) I'd accept 1% Performance is important but there should be some general guidelines and It's also worth pondering what's implied by the Java team evangelizing to Also: when does it become more appropriate to address a performance issue Just some random thoughts... -Archie -- |
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. |
/reviewers 2 reviewer |
@AlanBateman |
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:
Yes - apologies if it sounded like I was trying to single you out. The
Good point, but frankly, an irrelevant one. The key issue here is that if -Archie -- |
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.
|
@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! |
Keep it alive. |
@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 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 |
# 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
/open |
@wenshao This pull request is now open |
@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 |
@wenshao The pull request is being re-evaluated and the inactivity timeout has been reset. |
@wenshao this pull request can not be integrated into 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 |
Continue to complete PR #16006 and PR #21593 to improve BigDecimal::toString and BigDecimal::toPlainString performance and reduce duplicate code
Progress
Warnings
Issue
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