Skip to content

Convert sdk.Int to BigDec#6409

Merged
p0mvn merged 17 commits intomainfrom
hieu/bigdec_from_sdk_int
Sep 20, 2023
Merged

Convert sdk.Int to BigDec#6409
p0mvn merged 17 commits intomainfrom
hieu/bigdec_from_sdk_int

Conversation

@hieuvubk
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Collaborator

@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

return NewBigDecFromBigIntWithPrec(d.BigInt(), PrecisionDec)
}

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

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
Copy Markdown
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
Copy Markdown
Member

p0mvn commented Sep 16, 2023

Marking as draft until the requests are resolved

@hieuvubk
Copy link
Copy Markdown
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
Copy Markdown
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

Copy link
Copy Markdown
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

// our liquidity calculations will be off from our theoretical calculations within our tests.
// TODO (perf): consider better conversion helpers to minimize reallocations.
amountBigDec := osmomath.BigDecFromDec(amount.ToLegacyDec())
amountBigDec := osmomath.BigDecFromSDKInt(amount)
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
@p0mvn p0mvn merged commit e2b521d into main Sep 20, 2023
@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 <roman@osmosis.team>
(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 <roman@osmosis.team>
(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 <72878483+hieuvubk@users.noreply.github.com>
Co-authored-by: roman <roman@osmosis.team>
@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