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

Convert sdk.Int to BigDec #6409

Merged
merged 17 commits into from
Sep 20, 2023
Merged

Convert sdk.Int to BigDec #6409

merged 17 commits into from
Sep 20, 2023

Conversation

hieuvubk
Copy link
Contributor

@hieuvubk hieuvubk commented Sep 14, 2023

Closes: #6370

What is the purpose of the change

Create a func BigDecFromSDKInt to convert sdk.Int => BigDec instead of converting sdk.Int => sdk.Dec => BigDec
Benchmark:

OLD:
BenchmarkBigDecFromInt-16        2074845               579.7 ns/op           640 B/op         18 allocs/op

NEW:
BenchmarkBigDecFromInt-16        3566844               312.9 ns/op           312 B/op          9 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.

Copy link
Member

@p0mvn p0mvn 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 change, please see comment

Comment on lines +587 to +589
func BigDecFromSDKInt(i Int) BigDec {
return NewBigDecFromBigIntWithPrec(i.BigInt(), 0)
}
Copy link
Member

Choose a reason for hiding this comment

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

Note that this is not mutative. See that BigInt() reinitializes the underlying buffer.

Let's still keep this, please. However, I'm also wondering if we can make a mutative version that would reuse the input's buffer?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I see, currently it's hard to access Int.i directly without BigInt()

Copy link
Member

Choose a reason for hiding this comment

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

Could you make an issue to consider this in the future and potentially talk to the SDK team about exposing such API?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Created an issue for tracking #17772

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 tried returning Int.i in my local, performance is improved but the potential risks are unclear

Copy link
Member

@pysel pysel left a comment

Choose a reason for hiding this comment

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

thanks! left a small nit, lgtm when Roman's comment is addressed

@@ -582,6 +582,12 @@ func BigDecFromDec(d Dec) BigDec {
return NewBigDecFromBigIntWithPrec(d.BigInt(), PrecisionDec)
}

// BigDecFromSDKInt returns the BigDec representation of an sdkInt.
Copy link
Member

Choose a reason for hiding this comment

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

nit: I think we do not reference sdk anymore, Int is defined in osmomath package

Suggested change
// BigDecFromSDKInt returns the BigDec representation of an sdkInt.
// BigDecFromSDKInt returns the BigDec representation of an Int.

@p0mvn p0mvn marked this pull request as draft September 16, 2023 01:38
@p0mvn p0mvn marked this pull request as ready for review September 16, 2023 01:39
@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 A:no-changelog V:state/compatible/no_backport State machine compatible PR, depends on prior breaks labels Sep 16, 2023
@p0mvn
Copy link
Member

p0mvn commented Sep 16, 2023

@hieuvubk please add a changelog entry and create an issue. Thanks!

@p0mvn p0mvn marked this pull request as draft September 16, 2023 01:42
@p0mvn
Copy link
Member

p0mvn commented Sep 16, 2023

Marking as draft until the requests are resolved

@hieuvubk
Copy link
Contributor Author

@hieuvubk please add a changelog entry and create an issue. Thanks!

Added!
Tracking issue: #17772

@hieuvubk hieuvubk marked this pull request as ready for review September 16, 2023 08:43
@p0mvn
Copy link
Member

p0mvn commented Sep 16, 2023

Thanks @hieuvubk

There are more TODOs left related to Dec() functions with no reallocations:

// TODO (perf): consider Dec() function that does not reallocate

Are you planning on working on that as well? @hieuvubk

@hieuvubk
Copy link
Contributor Author

hieuvubk commented Sep 16, 2023

Thanks @hieuvubk

There are more TODOs left related to Dec() functions with no reallocations:

// TODO (perf): consider Dec() function that does not reallocate

Are you planning on working on that as well? @hieuvubk

I thought #6261 should resolve Dec(), also Im working on QuoRoundUp & QuoTruncate

@p0mvn
Copy link
Member

p0mvn commented Sep 17, 2023

@hieuvubk could you investigate the e2e failure please? I tried rerunning it a few times, and it didn't work. From logs, unclear what the issue is so might require investigations

@hieuvubk
Copy link
Contributor Author

@hieuvubk could you investigate the e2e failure please? I tried rerunning it a few times, and it didn't work. From logs, unclear what the issue is so might require investigations

I think it's due to a version conflict of osmomath because go.mod is taking the version of osmomath as v0.0.7-0.20230911120014-b14342e08daf while my change hasn't been tagged yet.

@p0mvn
Copy link
Member

p0mvn commented Sep 18, 2023

@hieuvubk could you investigate the e2e failure please? I tried rerunning it a few times, and it didn't work. From logs, unclear what the issue is so might require investigations

I think it's due to a version conflict of osmomath because go.mod is taking the version of osmomath as v0.0.7-0.20230911120014-b14342e08daf while my change hasn't been tagged yet.

Please run:

go get github.com/osmosis-labs/osmosis/osmomath@4225d5f

Should update the tag to the one taken from a commit on your branch. You should see updates in go.mod and go.sum. Please also run go work sync after and make sure that there are no errors

@hieuvubk
Copy link
Contributor Author

thanks @p0mvn it works!

Copy link
Member

@p0mvn p0mvn left a comment

Choose a reason for hiding this comment

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

One more request before merge please @hieuvubk

// TODO (perf): consider better conversion helpers to minimize reallocations.
amountBigDec := osmomath.BigDecFromDec(amount.ToLegacyDec())
amountBigDec := osmomath.BigDecFromSDKInt(amount)
Copy link
Member

Choose a reason for hiding this comment

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

Let's keep these comments please.

Do you mint creating an issue for this osmosis side and linking it to cosmos/cosmos-sdk#17772?

Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@hieuvubk hieuvubk requested a review from p0mvn September 19, 2023 07:40
CHANGELOG.md Outdated Show resolved Hide resolved
@p0mvn p0mvn merged commit e2b521d into main Sep 20, 2023
1 check passed
@p0mvn p0mvn deleted the hieu/bigdec_from_sdk_int branch September 20, 2023 01:32
mergify bot pushed a commit that referenced this pull request Sep 20, 2023
* refactor/test(CL): Stricter rounding behavior in CL math methods; unit tests at low price level

* comment updates

* convert int => bigdec instead of int => dec => bigdec

* tests

* Liquidity1 lack

* add changelog endpoint

* update go mod

* keep comments

* go mod

* fix

* Update CHANGELOG.md

---------

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

# Conflicts:
#	CHANGELOG.md
#	go.mod
#	go.sum
#	osmomath/decimal_test.go
#	x/concentrated-liquidity/math/math.go
p0mvn added a commit that referenced this pull request Sep 20, 2023
* Convert sdk.Int to BigDec (#6409)

* refactor/test(CL): Stricter rounding behavior in CL math methods; unit tests at low price level

* comment updates

* convert int => bigdec instead of int => dec => bigdec

* tests

* Liquidity1 lack

* add changelog endpoint

* update go mod

* keep comments

* go mod

* fix

* Update CHANGELOG.md

---------

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

# Conflicts:
#	CHANGELOG.md
#	go.mod
#	go.sum
#	osmomath/decimal_test.go
#	x/concentrated-liquidity/math/math.go

* resolve conflict

* go mod tidy

* update go mod

* Update CHANGELOG.md

---------

Co-authored-by: Hieu Vu <[email protected]>
Co-authored-by: roman <[email protected]>
@github-actions github-actions bot mentioned this pull request Mar 1, 2024
@github-actions github-actions bot mentioned this pull request May 15, 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
3 participants