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

BigDec operation speedups #8006

Merged
merged 10 commits into from
Apr 11, 2024
Merged

BigDec operation speedups #8006

merged 10 commits into from
Apr 11, 2024

Conversation

ValarDragon
Copy link
Member

@ValarDragon ValarDragon commented Apr 10, 2024

Changes:

  • Rename precisionReuse -> defaultBigDecPrecisionReuse
  • Rename precisionReuseSDK -> precisionReuseSDKDec
  • Use squaredPrecisionReuse properly (saving many internal ops). This is clearly state compat because we are replacing double Mul's with a single one that will commute.
  • expose precisionReuse as an argument more consistently
  • Remove redundant copies in many BigDec operations
  • Speedup common BigDec X Dec ops
    • Just needs tests
  • Speedup Quo w/ rounding - Update: My intuition was wrong for roundup, Now looking into RoundDown
    • Truncate was significantly sped up. (Saved the chop routine, and operate on something two word sizes smaller!)

Its easiest to review this PR commit by commit

Copy link
Contributor

coderabbitai bot commented Apr 10, 2024

Walkthrough

The update in version v24.0.1 enhances the efficiency of BigDec operations by restructuring variable naming and management in decimal arithmetic operations. These changes aim to improve precision and calculation accuracy, optimizing performance and computational integrity in mathematical functions.

Changes

File(s) Change Summary
osmomath/decimal.go Renaming and reorganization of precision-related variables; introduction of squaredPrecisionReuse.
osmomath/decimal_test.go Update in TestApproxRoot function to use AbsMut instead of Abs.
x/concentrated-liquidity/math/math.go Logic update in CalcAmount1Delta function for absolute value calculation.
x/gamm/pool-models/stableswap/amm.go Mutating operations on osmomath.BigDec types for in-place modifications.

🐰✨
Oh hark! A change, a tweak, a twist,
In math where decimals coexist.
Precision fine like morning dew,
Code hops along, fresh and new.
Cheers to devs, with brains so bright,
Crafting code by moon's soft light. 🌙
🎉🥕


Recent Review Details

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between dfe1b43 and 47d4f52.
Files ignored due to path filters (10)
  • go.mod is excluded by !**/*.mod
  • go.sum is excluded by !**/*.sum
  • osmomath/go.mod is excluded by !**/*.mod
  • osmomath/go.sum is excluded by !**/*.sum
  • osmoutils/go.mod is excluded by !**/*.mod
  • osmoutils/go.sum is excluded by !**/*.sum
  • x/epochs/go.mod is excluded by !**/*.mod
  • x/epochs/go.sum is excluded by !**/*.sum
  • x/ibc-hooks/go.mod is excluded by !**/*.mod
  • x/ibc-hooks/go.sum is excluded by !**/*.sum
Files selected for processing (4)
  • osmomath/decimal.go (22 hunks)
  • osmomath/decimal_test.go (1 hunks)
  • x/concentrated-liquidity/math/math.go (1 hunks)
  • x/gamm/pool-models/stableswap/amm.go (2 hunks)
Files not reviewed due to errors (1)
  • (no review received)
Files skipped from review as they are similar to previous changes (1)
  • osmomath/decimal.go
Additional comments not posted (2)
x/concentrated-liquidity/math/math.go (1)

100-100: Verify the removal of the GT check in CalcAmount1Delta.

osmomath/decimal_test.go (1)

621-621: Ensure that the mutation from AbsMut() does not unintentionally affect subsequent operations where expected or res might be reused in the TestApproxRoot function.


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.

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-labs/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.

@ValarDragon ValarDragon added A:backport/v23.x backport patches to v23.x branch A:backport/v24.x backport patches to v24.x branch labels Apr 10, 2024
@ValarDragon ValarDragon added the V:state/compatible/backport State machine compatible PR, should be backported label Apr 10, 2024
@ValarDragon ValarDragon changed the title Rename precisionReuse and use squaredPrecisionReuse properly BigDec operation speedups Apr 10, 2024
@ValarDragon
Copy link
Member Author

I'll edit several CL methods in a second PR

@ValarDragon ValarDragon requested a review from mattverse as a code owner April 10, 2024 09:47
@github-actions github-actions bot added the C:x/gamm Changes, features and bugs related to the gamm module. label Apr 10, 2024
Copy link
Member

@czarcas7ic czarcas7ic left a comment

Choose a reason for hiding this comment

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

I think the go.mod at the highest level needs to be updated to use this version of osmomath in order to ensure all tests pass, and not just the osmomath specific tests (it LGTM, but would make sense to do this prior to merge)

Comment on lines +363 to +365
if d.i.BitLen() > maxDecBitLen {
panic("Int overflow")
}
Copy link
Member

Choose a reason for hiding this comment

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

nit: this logic gets reused 5/6 times in this PR, maybe could be a single line helper.

Copy link
Member Author

Choose a reason for hiding this comment

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

Lets do that in a second PR, agreed tho

@czarcas7ic czarcas7ic closed this Apr 10, 2024
@czarcas7ic czarcas7ic reopened this Apr 10, 2024
@ValarDragon ValarDragon merged commit b56059e into main Apr 11, 2024
1 check passed
@ValarDragon ValarDragon deleted the dev/bigdec_speedups branch April 11, 2024 07:10
mergify bot pushed a commit that referenced this pull request Apr 11, 2024
* Rename precisionReuse and use squaredPrecisionReuse properly

* Remove redundant copies for chop operations

* Speedup RoundUpMut failed, just committing benchmark

* CL notes

* Speedup Truncate routines by saving ~2 word sizes

* Add BigDec x Dec fns

* Minor approx root spedup

* Driveby, better mut usage in stableswap

* Add AbsMut

* Auto: update go.mod after push to dev/bigdec_speedups that modified dependencies locally

---------

Co-authored-by: github-actions <[email protected]>
(cherry picked from commit b56059e)

# Conflicts:
#	CHANGELOG.md
#	go.mod
#	go.sum
#	osmomath/go.mod
#	osmomath/go.sum
#	osmoutils/go.mod
#	osmoutils/go.sum
#	x/epochs/go.mod
#	x/epochs/go.sum
#	x/ibc-hooks/go.mod
#	x/ibc-hooks/go.sum
mergify bot pushed a commit that referenced this pull request Apr 11, 2024
* Rename precisionReuse and use squaredPrecisionReuse properly

* Remove redundant copies for chop operations

* Speedup RoundUpMut failed, just committing benchmark

* CL notes

* Speedup Truncate routines by saving ~2 word sizes

* Add BigDec x Dec fns

* Minor approx root spedup

* Driveby, better mut usage in stableswap

* Add AbsMut

* Auto: update go.mod after push to dev/bigdec_speedups that modified dependencies locally

---------

Co-authored-by: github-actions <[email protected]>
(cherry picked from commit b56059e)

# Conflicts:
#	go.mod
#	go.sum
#	osmomath/go.mod
#	osmomath/go.sum
#	osmoutils/go.mod
#	osmoutils/go.sum
#	x/epochs/go.mod
#	x/epochs/go.sum
#	x/ibc-hooks/go.mod
#	x/ibc-hooks/go.sum
mergify bot pushed a commit that referenced this pull request Apr 11, 2024
* Rename precisionReuse and use squaredPrecisionReuse properly

* Remove redundant copies for chop operations

* Speedup RoundUpMut failed, just committing benchmark

* CL notes

* Speedup Truncate routines by saving ~2 word sizes

* Add BigDec x Dec fns

* Minor approx root spedup

* Driveby, better mut usage in stableswap

* Add AbsMut

* Auto: update go.mod after push to dev/bigdec_speedups that modified dependencies locally

---------

Co-authored-by: github-actions <[email protected]>
(cherry picked from commit b56059e)

# Conflicts:
#	go.mod
#	go.sum
#	osmomath/go.mod
#	osmomath/go.sum
#	osmoutils/go.mod
#	osmoutils/go.sum
#	x/epochs/go.mod
#	x/epochs/go.sum
#	x/ibc-hooks/go.mod
#	x/ibc-hooks/go.sum
p0mvn added a commit that referenced this pull request Apr 12, 2024
ValarDragon added a commit that referenced this pull request Apr 15, 2024
* BigDec operation speedups (backport #8006)

* Speedup quo round up, start CL speedup integration (#8014)

* Speedup quo round up

* Code reuse

* Missed a code re-use point

* Add future notes

* Comment cleanup

* Auto: update go.mod after push to dev/speedup_quoroundup that modified dependencies locally

* Add new fn: NewBigDecFromDecMulDec

* Auto: update go.mod after push to dev/speedup_quoroundup that modified dependencies locally

* Remove some extra ops from CL

* Further perf notes

* Make faster QuoRoundUpNextIntMut

* Auto: update go.mod after push to dev/speedup_quoroundup that modified dependencies locally

* Remove another 2 BigDec ops

* Add another dec op

* Auto: update go.mod after push to dev/speedup_quoroundup that modified dependencies locally

* Start moving some liquidity calls to Dec not BigDec

* One more BigDec x Dec op

* Auto: update go.mod after push to dev/speedup_quoroundup that modified dependencies locally

* Another liq BigDec -> Dec

* Missed one step

* Move another Liquidity BigDec -> Dec

* Minor spread reward update

* Make CalcAmount1Dec use Dec for Liquidity

* Make one more op mutative

* One more speedup

* Fix test

* Speedup SpotPrice impl

---------

Co-authored-by: github-actions <[email protected]>

* updates

* updates

---------

Co-authored-by: Dev Ojha <[email protected]>
Co-authored-by: github-actions <[email protected]>
@github-actions github-actions bot mentioned this pull request Apr 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A:backport/v23.x backport patches to v23.x branch A:backport/v24.x backport patches to v24.x branch C:x/concentrated-liquidity C:x/epochs C:x/gamm Changes, features and bugs related to the gamm module. V:state/compatible/backport State machine compatible PR, should be backported
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants