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

Merged
merged 8 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions math/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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


## [math/v1.3.0](https://github.com/cosmos/cosmos-sdk/releases/tag/math/v1.3.0) - 2024-02-22

### Features
Expand Down
13 changes: 7 additions & 6 deletions math/dec.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,11 +399,10 @@ func (d LegacyDec) QuoTruncate(d2 LegacyDec) LegacyDec {

// QuoTruncateMut mutable quotient truncate
func (d LegacyDec) QuoTruncateMut(d2 LegacyDec) LegacyDec {
// 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!

d.i.Quo(d.i, d2.i)

chopPrecisionAndTruncate(d.i)
if d.i.BitLen() > maxDecBitLen {
panic("Int overflow")
}
Expand All @@ -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

_, rem := d.i.QuoRem(d.i, d2.i, big.NewInt(0))
if rem.Sign() != 0 {
d.i.Add(d.i, oneInt)
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
}

chopPrecisionAndRoundUp(d.i)
if d.i.BitLen() > maxDecBitLen {
panic("Int overflow")
}
Expand Down
12 changes: 10 additions & 2 deletions math/dec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -668,11 +668,15 @@ func BenchmarkLegacyQuoMut(b *testing.B) {

func BenchmarkLegacyQuoTruncateMut(b *testing.B) {
b1 := math.LegacyNewDec(17e2 + 8371)
baseArr := make([]math.LegacyDec, b.N)
for i := 0; i < b.N; i++ {
baseArr[i] = b1.Clone()
}
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

}

if sink == nil {
Expand All @@ -697,11 +701,15 @@ func BenchmarkLegacySqrtOnMersennePrime(b *testing.B) {

func BenchmarkLegacyQuoRoundupMut(b *testing.B) {
b1 := math.LegacyNewDec(17e2 + 8371)
baseArr := make([]math.LegacyDec, b.N)
for i := 0; i < b.N; i++ {
baseArr[i] = b1.Clone()
}
b2 := math.LegacyNewDec(4371)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
sink = b1.QuoRoundupMut(b2)
sink = baseArr[i].QuoRoundupMut(b2)
}

if sink == nil {
Expand Down