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

Remove using an iterator from updating TWAP records #7266

Merged
merged 4 commits into from
Feb 2, 2024

Conversation

ValarDragon
Copy link
Member

This shows up in benchmarks as being a slowdown on getting TWAP records. It will affect gas, but should only do so in EndBlock logic which should not be gas metered.

We see this iterator call being the cause of around .4% of State machine processing speed and even persists in the IAVL v2 benchmarks as a real expense.

@ValarDragon ValarDragon added V:state/compatible/backport State machine compatible PR, should be backported A:backport/v21.x backport patches to v21.x branch labels Jan 8, 2024
@github-actions github-actions bot added the C:x/twap Changes to the twap module label Jan 8, 2024
@@ -151,6 +151,23 @@ func (k Keeper) GetAllMostRecentRecordsForPool(ctx sdk.Context, poolId uint64) (
return types.GetAllMostRecentTwapsForPool(store, poolId)
}

// GetAllMostRecentRecordsForPool returns all most recent twap records
// (in state representation) for the provided pool id.
func (k Keeper) GetAllMostRecentRecordsForPoolWithDenoms(ctx sdk.Context, poolId uint64, denoms []string) ([]types.TwapRecord, error) {
Copy link
Member

@mattverse mattverse Jan 8, 2024

Choose a reason for hiding this comment

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

Can we have unit test for this method?

// (in state representation) for the provided pool id.
func (k Keeper) GetAllMostRecentRecordsForPoolWithDenoms(ctx sdk.Context, poolId uint64, denoms []string) ([]types.TwapRecord, error) {
store := ctx.KVStore(k.storeKey)
// if length != 2, use iterator
Copy link
Member

@mattverse mattverse Jan 8, 2024

Choose a reason for hiding this comment

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

nice 🌮

@czarcas7ic
Copy link
Member

I think we should add changelog for this, but is soft suggestion

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.

LGTM, I think we should have the unit test as Matt suggested though.

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.

LGTM

Comment on lines +95 to +99
func GetMostRecentTwapForPool(store sdk.KVStore, poolId uint64, denom1, denom2 string) (TwapRecord, error) {
key := FormatMostRecentTWAPKey(poolId, denom1, denom2)
bz := store.Get(key)
return ParseTwapFromBz(bz)
}
Copy link
Member

Choose a reason for hiding this comment

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

nit: would it be better to have this within keeper since it touches store?

Copy link
Member

Choose a reason for hiding this comment

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

I think having this here makes sense given prev code structure (e.g GetAllMostRecentTwapsForPool also lives in types/keys.go)

@faddat
Copy link
Member

faddat commented Jan 9, 2024

Agree about the unit test

@czarcas7ic czarcas7ic added the A:backport/v22.x backport patches to v22.x branch label Jan 12, 2024
Copy link
Contributor

This pull request has been automatically marked as stale because it has not had any recent activity. It will be closed if no further activity occurs. Thank you!

@github-actions github-actions bot added the Stale label Jan 27, 2024
@mattverse mattverse removed the Stale label Jan 29, 2024
@mattverse
Copy link
Member

Added unit tests, will be merging this PR by EOW

@ValarDragon
Copy link
Member Author

Thank you for adding the test 🙏

@ValarDragon ValarDragon merged commit 9193ee4 into main Feb 2, 2024
1 check passed
@ValarDragon ValarDragon deleted the dev/remove_twap_update_iterator branch February 2, 2024 00:24
mergify bot pushed a commit that referenced this pull request Feb 2, 2024
* Remove using an iterator from updating TWAP records

* Changelog

* Add test case

---------

Co-authored-by: mattverse <[email protected]>
Co-authored-by: Matt, Park <[email protected]>
(cherry picked from commit 9193ee4)

# Conflicts:
#	CHANGELOG.md
mergify bot pushed a commit that referenced this pull request Feb 2, 2024
* Remove using an iterator from updating TWAP records

* Changelog

* Add test case

---------

Co-authored-by: mattverse <[email protected]>
Co-authored-by: Matt, Park <[email protected]>
(cherry picked from commit 9193ee4)
ValarDragon added a commit that referenced this pull request Feb 2, 2024
* Remove using an iterator from updating TWAP records

* Changelog

* Add test case

---------

Co-authored-by: mattverse <[email protected]>
Co-authored-by: Matt, Park <[email protected]>
(cherry picked from commit 9193ee4)

Co-authored-by: Dev Ojha <[email protected]>
p0mvn pushed a commit that referenced this pull request Feb 3, 2024
…7403)

* Remove using an iterator from updating TWAP records (#7266)

* Remove using an iterator from updating TWAP records

* Changelog

* Add test case

---------

Co-authored-by: mattverse <[email protected]>
Co-authored-by: Matt, Park <[email protected]>
(cherry picked from commit 9193ee4)

# Conflicts:
#	CHANGELOG.md

* Update CHANGELOG.md

---------

Co-authored-by: Dev Ojha <[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/v21.x backport patches to v21.x branch A:backport/v22.x backport patches to v22.x branch C:x/twap Changes to the twap 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.

None yet

5 participants