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

Speedup SumLocksByDenom #7172

Merged
merged 3 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Misc Improvements
* [#7106](https://github.com/osmosis-labs/osmosis/pull/7106) Halve the time of log2 calculation (speeds up TWAP code)
* [#7093](https://github.com/osmosis-labs/osmosis/pull/7093),[#7100](https://github.com/osmosis-labs/osmosis/pull/7100) Lower CPU overheads of the Osmosis epoch.
* [#7093](https://github.com/osmosis-labs/osmosis/pull/7093),[#7100](https://github.com/osmosis-labs/osmosis/pull/7100),[#7172](https://github.com/osmosis-labs/osmosis/pull/7093) Lower CPU overheads of the Osmosis epoch.

## v21.0.0

Expand Down
16 changes: 12 additions & 4 deletions x/lockup/types/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"fmt"
"math/big"
"strings"
"time"

Expand Down Expand Up @@ -63,18 +64,25 @@ func (p PeriodLock) SingleCoin() (sdk.Coin, error) {
}

// TODO: Can we use sumtree instead here?
// Assumes that caller is passing in locks that contain denom
func SumLocksByDenom(locks []PeriodLock, denom string) osmomath.Int {
sum := osmomath.NewInt(0)
sumBi := big.NewInt(0)
// validate the denom once, so we can avoid the expensive validate check in the hot loop.
err := sdk.ValidateDenom(denom)
if err != nil {
panic(fmt.Errorf("invalid denom used internally: %s, %v", denom, err))
}
for _, lock := range locks {
// TODO: Replace with Mutative Int Operations
sum = sum.Add(lock.Coins.AmountOfNoDenomValidation(denom))
var amt osmomath.Int
// skip a 1second cumulative runtimeEq check
if len(lock.Coins) == 1 {
amt = lock.Coins[0].Amount
} else {
amt = lock.Coins.AmountOfNoDenomValidation(denom)
}
sumBi.Add(sumBi, amt.BigIntMut())
}
return sum
return osmomath.NewIntFromBigInt(sumBi)
}

// quick fix for getting native denom from synthetic denom.
Expand Down