-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
perf(math): Significantly speedup Dec quo truncate and quo Roundup #20034
perf(math): Significantly speedup Dec quo truncate and quo Roundup #20034
Conversation
WalkthroughThe recent updates focus on optimizing decimal operations by refining precision multiplication and rounding logic. Benchmark tests have also been enhanced for better accuracy and independence of calculations. Changes
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
This comment has been minimized.
This comment has been minimized.
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.
Actionable comments posted: 2
@@ -36,6 +36,8 @@ Ref: https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.j | |||
|
|||
## [Unreleased] | |||
|
|||
* [#20034](https://github.com/cosmos/cosmos-sdk/pull/20034) Significantly speedup LegacyDec.QuoTruncate and LegacyDec.QuoRoundUp. |
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.
Correct the spelling of "speedup" to "speed up" as it should be two words when used as a verb.
- Significantly speedup LegacyDec.QuoTruncate and LegacyDec.QuoRoundUp.
+ Significantly speed up LegacyDec.QuoTruncate and LegacyDec.QuoRoundUp.
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
* [#20034](https://github.com/cosmos/cosmos-sdk/pull/20034) Significantly speedup LegacyDec.QuoTruncate and LegacyDec.QuoRoundUp. | |
* [#20034](https://github.com/cosmos/cosmos-sdk/pull/20034) Significantly speed up LegacyDec.QuoTruncate and LegacyDec.QuoRoundUp. |
Add a space after the period to separate the sentences properly.
- Significantly speed up LegacyDec.QuoTruncate and LegacyDec.QuoRoundUp.## [math/v1.3.0](https://github.com/cosmos/cosmos-sdk/releases/tag/math/v1.3.0) - 2024-02-22
+ Significantly speed up LegacyDec.QuoTruncate and LegacyDec.QuoRoundUp. ## [math/v1.3.0](https://github.com/cosmos/cosmos-sdk/releases/tag/math/v1.3.0) - 2024-02-22
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
* [#20034](https://github.com/cosmos/cosmos-sdk/pull/20034) Significantly speedup LegacyDec.QuoTruncate and LegacyDec.QuoRoundUp. | |
* [#20034](https://github.com/cosmos/cosmos-sdk/pull/20034) Significantly speed up LegacyDec.QuoTruncate and LegacyDec.QuoRoundUp. ## [math/v1.3.0](https://github.com/cosmos/cosmos-sdk/releases/tag/math/v1.3.0) - 2024-02-22 |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
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.
Thanks for the PR! Very nice work but the behaviour in QuoRoundupMut
is different now for negative numbers.
// multiply precision twice | ||
d.i.Mul(d.i, squaredPrecisionReuse) | ||
// multiply precision once | ||
d.i.Mul(d.i, precisionReuse) |
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.
Although it was using higher precision before, the additional numbers where just cropped. Good finding!
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
@@ -418,10 +417,12 @@ func (d LegacyDec) QuoRoundUp(d2 LegacyDec) LegacyDec { | |||
// QuoRoundupMut mutable quotient, round up | |||
func (d LegacyDec) QuoRoundupMut(d2 LegacyDec) LegacyDec { | |||
// multiply precision twice | |||
d.i.Mul(d.i, squaredPrecisionReuse) | |||
d.i.Quo(d.i, d2.i) | |||
d.i.Mul(d.i, precisionReuse) |
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.
nit: the code comment is not correct anymore
b2 := math.LegacyNewDec(4371) | ||
b.ReportAllocs() | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
sink = b1.QuoTruncateMut(b2) | ||
sink = baseArr[i].QuoTruncateMut(b2) |
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 fix. Do you want to add more number examples to support your cases? You could run them via
b.Run("", func(b *testing.B) {})
like table tests
Good catch for negative numbers! |
gentle ping @ValarDragon |
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
809884a
to
7c77d21
Compare
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 have added more test cases and coverage for QuoTruncateMut
to the branch. All succeeded with the old version, too so that we have confidence that the changes don't break existing behaviour
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.
LGTM!
* main: feat(server/v2): introduce cometbft v2 (#20483) refactor(x/upgrade): migrate to appmodule.VersionMap (#20485) docs: code guidelines changes (#20482) feat(cosmovisor): load cosmovisor configuration from toml file (#19995) perf(math): Significantly speedup Dec quo truncate and quo Roundup (#20034) fix: Bump CometBFT versions (#20481)
* main: (120 commits) chore: update protoc-gen-swagger to protoc-gen-openapiv2 (#20448) ci: Add GitHub Action for go mod tidy and mocks (#20501) chore: Address linter issues (#20486) fix: wrap errors in auto CLI service registration (#20493) chore: fix comment (#20498) chore: fix the note box syntax error (#20499) feat(server/v2): introduce cometbft v2 (#20483) refactor(x/upgrade): migrate to appmodule.VersionMap (#20485) docs: code guidelines changes (#20482) feat(cosmovisor): load cosmovisor configuration from toml file (#19995) perf(math): Significantly speedup Dec quo truncate and quo Roundup (#20034) fix: Bump CometBFT versions (#20481) refactor(core): remove redundant ExecMode (#20322) feat(store/v2): pruning manager (#20430) chore: force reload sonar cloud (#20480) refactor(x/accounts): reuse calculated sum in `func safeAdd` (#20458) refactor: remove `defer` in loop (#20223) ci: remove livness test (#20474) build(deps): Bump bufbuild/buf-setup-action from 1.32.1 to 1.32.2 (#20477) chore: migrate a few diagrams to mermaid (#20473) ...
Significantly speedsup Dec.QuoTruncate and Dec.QuoRoundUp.
This is state compatible, as Quo is always floor division. note that QuoRem is ~no overhead relative to Quo in the big.Int implementation for large words (which all Dec's in practice are) We have tested state compatability for this optimization on Osmosis' BigDec, which is the same implementation.
The speedup shown in the benchmark uses small values, the impact is much larger at larger values. (So a benchmark with bigger numbers would boast better speeds) (I also corrected the benchmark, before it would just be doing 0/b2 in the benchmark).
Performance on this small input (again impact much higher at real numbers we see) is already 33% improvement:
OLD perf on new benchmark code:
NEW:
Correctness of this is covered by existing tests. (And tested on Osmosis mainnet syncs for BigDecimal)
Summary by CodeRabbit