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

osmomath: Mutative version for QuoRoundUp #6437

Merged
merged 9 commits into from
Sep 20, 2023
Merged

osmomath: Mutative version for QuoRoundUp #6437

merged 9 commits into from
Sep 20, 2023

Conversation

hieuvubk
Copy link
Contributor

@hieuvubk hieuvubk commented Sep 19, 2023

Closes: #6370

What is the purpose of the change

Create new mutative version of QuoRoundUp

Benchmark

OLD:
BenchmarkLegacyQuoRoundup-16             1799961               685.7 ns/op           432 B/op          6 allocs/op

NEW:
BenchmarkLegacyQuoRoundup-16             4316427               272.0 ns/op           128 B/op          2 allocs/op

Testing and Verifying

(Please pick one of the following options)

This change is a trivial rework / code cleanup without any test coverage.

(or)

This change is already covered by existing tests, such as (please describe tests).

(or)

This change added tests and can be verified as follows:

(example:)

  • Added unit test that validates ...
  • Added integration tests for end-to-end deployment with ...
  • Extended integration test for ...
  • Manually verified the change by ...

Documentation and Release Note

  • Does this pull request introduce a new feature or user-facing behavior changes?
  • Changelog entry added to Unreleased section of CHANGELOG.md?

Where is the change documented?

  • Specification (x/{module}/README.md)
  • Osmosis documentation site
  • Code comments?
  • N/A

@github-actions
Copy link
Contributor

Important Notice

This PR modifies an in-repo Go module. It is one of:

  • osmomath
  • osmoutils
  • x/ibc-hooks
  • x/epochs

The dependent Go modules, especially the root one, will have to be
updated to reflect the changes. Failing to do so might cause e2e to fail.

Please follow the instructions below:

  1. Open https://github.com/osmosis/osmosis/actions/workflows/go-mod-auto-bump.yml
  2. Provide the current branch name
  3. On success, confirm if an automated commit corretly updated the go.mod and go.sum files

Please let us know if you need any help.

@hieuvubk hieuvubk added the V:state/compatible/no_backport State machine compatible PR, depends on prior breaks label Sep 19, 2023
@hieuvubk hieuvubk changed the title Mutative version for QuoRoundUp osmomath: Mutative version for QuoRoundUp Sep 19, 2023
@hieuvubk
Copy link
Contributor Author

hieuvubk commented Sep 19, 2023

I made some changes to reduce reallocations max, the new benchmark results:

Diff sign:

BenchmarkLegacyQuoRoundup-16            38720631                26.45 ns/op            0 B/op          0 allocs/op

Same sign:

BenchmarkLegacyQuoRoundup-16          4658977               238.8 ns/op            80 B/op          1 allocs/op

@p0mvn
Copy link
Member

p0mvn commented Sep 19, 2023

I made some changes to reduce reallocations max, the new benchmark results:

Diff sign:

BenchmarkLegacyQuoRoundup-16            38720631                26.45 ns/op            0 B/op          0 allocs/op

Same sign:

BenchmarkLegacyQuoRoundup-16          4658977               238.8 ns/op            80 B/op          1 allocs/op

Nice!

Non-blocking tip: there is a tool called benchstat that helps to compare benchmark results. Check out: https://docs.osmosis.zone/osmosis-core/guides/performance

Comment on lines 700 to 720
func chopPrecisionAndRoundUpMut(d *big.Int, precisionReuse *big.Int) *big.Int {
// remove the negative and add it back when returning
if d.Sign() == -1 {
// make d positive, compute chopped value, and then un-mutate d
d = d.Neg(d)
// truncate since d is negative...
d = chopPrecisionAndTruncateMut(d)
d = d.Neg(d)
return d
}

// get the truncated quotient and remainder
_, rem := d.QuoRem(d, precisionReuse, big.NewInt(0))

if rem.Sign() == 0 { // remainder is zero
return d
}

return d.Add(d, oneInt)
}

Copy link
Member

Choose a reason for hiding this comment

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

How does this differ from chopPrecisionAndRoundUp? I'm not seeing any difference

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm using chopPrecisionAndTruncateMut that uses d as result. Also use d as QuoRem res instead of declaring new quo variable

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the context. I've made commits directly to the branch with changes that were missing from approval.

What was added:

  • remove the non-mutative chop version since we can now use the mutative one + copy the input
  • covered non-mutative QuoRoundUp with a test

Commit: 0622c30

@p0mvn p0mvn added V:state/compatible/backport State machine compatible PR, should be backported A:backport/v19.x backport patches to v19.x branch and removed V:state/compatible/no_backport State machine compatible PR, depends on prior breaks labels Sep 20, 2023
@p0mvn p0mvn merged commit b6db671 into main Sep 20, 2023
1 check passed
@p0mvn p0mvn deleted the hieu/quoroundup_mut branch September 20, 2023 15:41
mergify bot pushed a commit that referenced this pull request Sep 20, 2023
* mutative version for QuoRoundUp

* change log & go mod

* actual mutative input

* testing

* no reuse global var zeroInt

* update go.mod

* reduce duplication and cover non-mutative with a test

* update go.mod

---------

Co-authored-by: roman <[email protected]>
(cherry picked from commit b6db671)

# Conflicts:
#	CHANGELOG.md
#	go.mod
#	go.sum
#	x/concentrated-liquidity/math/math.go
p0mvn added a commit that referenced this pull request Sep 20, 2023
* osmomath: Mutative version for QuoRoundUp (backport #6437)

* fix bug

* go mod

* go mod

---------

Co-authored-by: roman <[email protected]>
@github-actions github-actions bot mentioned this pull request Mar 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A:backport/v19.x backport patches to v19.x branch C:x/concentrated-liquidity V:state/compatible/backport State machine compatible PR, should be backported
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat(osmomath): introduce more mutative math helpers for BigDec
2 participants