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

x/tariff/keeper: hoist out sdk.ZeroInt() into a global variable to avoid needless allocations #315

Open
odeke-em opened this issue Mar 9, 2024 · 0 comments

Comments

@odeke-em
Copy link

odeke-em commented Mar 9, 2024

If we examine a bunch of loops like

for _, coin := range feesCollectedInt {
if coin.Amount.GT(sdk.ZeroInt()) {
foundAmountGreaterThanZero = true
break
}

for _, coin := range feesToDistribute {
truncated, _ := coin.TruncateDecimal()
if truncated.Amount.GT(sdk.ZeroInt()) {
foundAmountGreaterThanZero = true
break
}

for _, s := range entityShare {
truncated, _ := s.TruncateDecimal()
if truncated.Amount.GT(sdk.ZeroInt()) {
coins = append(coins, truncated)
}

if feeInt.Equal(sdk.ZeroInt()) {

we see that for every coin we invoke sdk.ZeroInt() and that has a real cost added trying to find the first non-zero coin

Suggestion

We can remove these unnecessary costs entirely by creating a global variable then using it in the comparisons to remove those unnecessary high costs and allocations

var zeroInt = sdk.ZeroInt()

func (k Keeper) AllocateTokens(ctx sdk.Context) {
	...
	foundAmountGreaterThanZero := false
	for _, coin := range feesCollectedInt {
		if coin.Amount.GT(zeroInt) {
			foundAmountGreaterThanZero = true
			break
		}
	}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant