-
Notifications
You must be signed in to change notification settings - Fork 6.2k
8315585: Optimization for decimal to string #15555
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
8315585: Optimization for decimal to string #15555
Conversation
|
👋 Welcome back wenshao! A progress list of the required criteria for merging this PR into |
Webrevs
|
|
@cl4es can you help me to review this PR? |
|
@liach can you help me to review this PR? |
|
I think there is way too much duplication of logic going on here, primarily with On the motivation side I think it'd be helpful if you could point to some larger benchmarks, frameworks et.c. where optimizing |
|
Reducing code duplication with JavaLangAccess is a good suggestion, I'm waiting for #14699 to be merged. I am the author of the json library fastjson and fastjson2. When serializing BigDecimal type data to JSON, I found that BigDecimal has performance improvements. Including the following methods:
These optimizations are not only useful for fastjson and fastjson2, but also for gson/jackson and many other libraries. The optimization of #9410 is doubleValue, which is not related to toString/toPlainString. |
|
/reviewers 2 |
|
Besides a review of the performance changes, myself or someone else who works on BigInteger and BigDecimal will need to review the changes. From a quick look, it may be appropriate to add addition test cases to explicitly exercise the new code paths. |
|
I have added test, set breakpoints by debugging to ensure that all branches of changed code have been tested. |
|
@cl4es I have pushed a new commit to reduce duplicate code |
cl4es
left a comment
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.
Seems like a step in the right direction w.r.t. code duplication. There's still a lot going on in this PR so it'll take some time to digest. Is there some way to split this into a series of enhancements for easier review? The jla.newStringLatin1NoRepl additions could be split out and done first - along with evidence that it helps in other places like UUID and HexFormat.
src/java.base/share/classes/jdk/internal/access/JavaLangAccess.java
Outdated
Show resolved
Hide resolved
|
The new performance data is as follows: 1. benchmark scriptsh make/devkit/createJMHBundle.sh
bash configure --with-jmh=build/jmh/jars
make test TEST="micro:java.math.BigDecimals.*ToPlainString"
make test TEST="micro:java.math.BigDecimals.*ToEngineering"2. benchmark environment
3. benchmark result3.1. aliyun_ecs_c8i.xlarge
-Benchmark Mode Cnt Score Error Units (baseline)
-BigDecimals.testHugeToPlainString avgt 15 188.687 ? 0.210 ns/op
-BigDecimals.testLargeToPlainString avgt 15 36.547 ? 0.083 ns/op
-BigDecimals.testSmallToPlainString avgt 15 34.270 ? 0.075 ns/op
-BigDecimals.testToPlainString avgt 15 1661.588 ? 25.568 ns/op
+Benchmark Mode Cnt Score Error Units (optimized)
+BigDecimals.testHugeToPlainString avgt 15 117.868 ? 0.390 ns/op (+60.08%)
+BigDecimals.testLargeToPlainString avgt 15 15.903 ? 0.069 ns/op (+129.81%)
+BigDecimals.testSmallToPlainString avgt 15 13.502 ? 0.051 ns/op (+153.81%)
+BigDecimals.testToPlainString avgt 15 1556.691 ? 19.407 ns/op (+6.73%)
-Benchmark Mode Cnt Score Error Units (baseline)
-BigDecimals.testHugeToEngineeringString avgt 15 207.858 ? 4.166 ns/op
-BigDecimals.testLargeToEngineeringString avgt 15 35.723 ? 2.772 ns/op
-BigDecimals.testSmallToEngineeringString avgt 15 15.083 ? 0.052 ns/op
-BigDecimals.testToEngineeringString avgt 15 1715.916 ? 37.066 ns/op
+Benchmark Mode Cnt Score Error Units (optimized)
+BigDecimals.testHugeToEngineeringString avgt 15 142.110 ? 0.987 ns/op (+46.26%)
+BigDecimals.testLargeToEngineeringString avgt 15 12.509 ? 0.056 ns/op (+185.57%)
+BigDecimals.testSmallToEngineeringString avgt 15 10.579 ? 0.066 ns/op (+42.57%)
+BigDecimals.testToEngineeringString avgt 15 1646.783 ? 3.060 ns/op (+4.19%)3.2. aliyun_ecs_c8a.xlarge
-Benchmark Mode Cnt Score Error Units (baseline)
-BigDecimals.testHugeToEngineeringString avgt 15 300.712 ? 8.526 ns/op
-BigDecimals.testLargeToEngineeringString avgt 15 42.108 ? 0.149 ns/op
-BigDecimals.testSmallToEngineeringString avgt 15 24.333 ? 0.076 ns/op
-BigDecimals.testToEngineeringString avgt 15 2300.080 ? 12.640 ns/op
+Benchmark Mode Cnt Score Error Units (optimized)
+BigDecimals.testHugeToEngineeringString avgt 15 161.471 ? 1.303 ns/op (+86.23%)
+BigDecimals.testLargeToEngineeringString avgt 15 24.847 ? 0.111 ns/op (+69.46%)
+BigDecimals.testSmallToEngineeringString avgt 15 21.529 ? 0.088 ns/op (+13.02%)
+BigDecimals.testToEngineeringString avgt 15 2016.281 ? 21.728 ns/op (+14.07%)
-Benchmark Mode Cnt Score Error Units (baseline)
-BigDecimals.testHugeToPlainString avgt 15 277.892 ? 1.485 ns/op
-BigDecimals.testLargeToPlainString avgt 15 42.669 ? 0.075 ns/op
-BigDecimals.testSmallToPlainString avgt 15 38.928 ? 0.402 ns/op
-BigDecimals.testToPlainString avgt 15 2245.593 ? 20.488 ns/op
+Benchmark Mode Cnt Score Error Units (optimized)
+BigDecimals.testHugeToPlainString avgt 15 154.470 ? 7.259 ns/op (+79.90%)
+BigDecimals.testLargeToPlainString avgt 15 29.892 ? 0.290 ns/op (+42.74%)
+BigDecimals.testSmallToPlainString avgt 15 25.605 ? 0.109 ns/op (+52.03%)
+BigDecimals.testToPlainString avgt 15 2027.892 ? 12.792 ns/op (+10.73%)MaxBookPro M1 Pro-Benchmark Mode Cnt Score Error Units (baseline)
-BigDecimals.testHugeToEngineeringString avgt 15 250.340 ? 4.911 ns/op
-BigDecimals.testLargeToEngineeringString avgt 15 83.954 ? 7.603 ns/op
-BigDecimals.testSmallToEngineeringString avgt 15 17.321 ? 0.083 ns/op
-BigDecimals.testToEngineeringString avgt 15 1874.298 ? 71.820 ns/op
+Benchmark Mode Cnt Score Error Units (optimized)
+BigDecimals.testHugeToEngineeringString avgt 15 104.412 ? 17.752 ns/op (+139.76%)
+BigDecimals.testLargeToEngineeringString avgt 15 14.617 ? 0.380 ns/op (+474.35%)
+BigDecimals.testSmallToEngineeringString avgt 15 11.960 ? 0.021 ns/op (+44.82%)
+BigDecimals.testToEngineeringString avgt 15 1622.669 ? 81.835 ns/op (+15.50%)
-Benchmark Mode Cnt Score Error Units (baseline)
-BigDecimals.testHugeToPlainString avgt 15 188.559 ? 9.283 ns/op
-BigDecimals.testLargeToPlainString avgt 15 22.894 ? 0.102 ns/op
-BigDecimals.testSmallToPlainString avgt 15 24.029 ? 0.164 ns/op
-BigDecimals.testToPlainString avgt 15 1858.483 ? 56.941 ns/op
+Benchmark Mode Cnt Score Error Units (optimized)
+BigDecimals.testHugeToPlainString avgt 15 87.264 ? 0.677 ns/op (+116.07%)
+BigDecimals.testLargeToPlainString avgt 15 16.665 ? 0.045 ns/op (+37.37%)
+BigDecimals.testSmallToPlainString avgt 15 13.467 ? 0.091 ns/op (+78.42%)
+BigDecimals.testToPlainString avgt 15 1593.317 ? 84.553 ns/op (+16.64%) |
Thank you for taking the time to help with the review. I will split these changes into multiple PRs to make the changes in each PR smaller and easier to review. I have created a new PR #16006 |
|
@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 can not be integrated into git checkout optimization_for_decimal_to_string
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 |
BigDecimal is a commonly used class in business development, It is often necessary to perform toString or toPlainString operations on BigDecimal.
The current version uses StringBuilder resulting in multiple memory allocations, I made a modification to improve performance.
Because BigDecimal uses stringCache to cache the result of toString, the performance of toString needs special treatment before testing, such as clearing stringCache by unsafe operation before each test, The performance of toString is similar to that of toEngineering.
The performance data is as follows:
1. benchmark script
2. benchmark environment
3. benchmark result
Progress
Issue
Reviewing
Using
gitCheckout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/15555/head:pull/15555$ git checkout pull/15555Update a local copy of the PR:
$ git checkout pull/15555$ git pull https://git.openjdk.org/jdk.git pull/15555/headUsing Skara CLI tools
Checkout this PR locally:
$ git pr checkout 15555View PR using the GUI difftool:
$ git pr show -t 15555Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/15555.diff
Webrev
Link to Webrev Comment