Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ValarDragon
Copy link
Contributor

@ValarDragon ValarDragon commented Apr 13, 2024

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:

BenchmarkLegacyQuoTruncateMut-12         3642235               380.0 ns/op           152 B/op          3 allocs/op
BenchmarkLegacyQuoRoundupMut-12          3914364               387.6 ns/op           152 B/op          3 allocs/op

NEW:

BenchmarkLegacyQuoTruncateMut-12         6173737               216.9 ns/op            64 B/op          1 allocs/op
BenchmarkLegacyQuoRoundupMut-12          4863044               234.2 ns/op            64 B/op          1 allocs/op

Correctness of this is covered by existing tests. (And tested on Osmosis mainnet syncs for BigDecimal)

Summary by CodeRabbit

  • Refactor
    • Enhanced precision and rounding logic in decimal operations for improved accuracy.
  • Tests
    • Updated benchmarks in decimal operations to ensure accurate and independent calculations.

@ValarDragon ValarDragon requested a review from a team as a code owner April 13, 2024 11:32
Copy link
Contributor

coderabbitai bot commented Apr 13, 2024

Walkthrough

Walkthrough

The 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

Files Changes Summary
math/dec.go Updated multiplication logic and rounding in QuoTruncateMut and QuoRoundupMut functions.
math/dec_test.go Adjusted benchmarks to ensure independent calculations by using a base array for cloning in tests.
math/CHANGELOG.md Implemented speedup for LegacyDec.QuoTruncate and LegacyDec.QuoRoundUp in the cosmos-sdk.

Recent Review Details

Configuration used: .coderabbit.yml

Commits Files that changed from the base of the PR and between 7042594 and 8112297.
Files selected for processing (1)
  • math/CHANGELOG.md (1 hunks)
Additional Context Used
Path-based Instructions (1)
math/CHANGELOG.md (1)

Pattern **/*.md: "Assess the documentation for misspellings, grammatical errors, missing documentation and correctness"


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?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

This comment has been minimized.

@ValarDragon ValarDragon changed the title Significantly speedup Dec quo truncate and quo Roundup perf: Significantly speedup Dec quo truncate and quo Roundup Apr 13, 2024
Copy link
Contributor

@coderabbitai coderabbitai bot left a 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.
Copy link
Contributor

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.

Suggested change
* [#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.

Suggested change
* [#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

@julienrbrt julienrbrt changed the title perf: Significantly speedup Dec quo truncate and quo Roundup perf(math): Significantly speedup Dec quo truncate and quo Roundup Apr 15, 2024
Copy link
Contributor

@alpe alpe left a 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)
Copy link
Contributor

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!

@@ -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)
Copy link
Contributor

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

d.i.Mul(d.i, precisionReuse)
_, rem := d.i.QuoRem(d.i, d2.i, big.NewInt(0))
if rem.Sign() != 0 {
d.i.Add(d.i, oneInt)
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not correct for negative numbers. Instead you can do the following to achieve the same behaviour as before:

	if rem.Sign() > 0 && d.IsNegative() == d2.IsNegative() ||
		rem.Sign() < 0 && d.IsNegative() != d2.IsNegative() {

A test would be good to ensure this for regression. I wrote this for comparing with the old logic: https://gist.github.com/alpe/3eee84356d93b37be3e0e798c1c1464c

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah thank you, great catch!

b2 := math.LegacyNewDec(4371)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
sink = b1.QuoTruncateMut(b2)
sink = baseArr[i].QuoTruncateMut(b2)
Copy link
Contributor

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

@ValarDragon
Copy link
Contributor Author

Good catch for negative numbers!

@tac0turtle
Copy link
Member

gentle ping @ValarDragon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants