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

perf: remove unnecessary null byte #2110

Merged
merged 6 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 11 additions & 0 deletions util/keys/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,14 @@ func ExtractAddressAndString(startIndex int, key []byte) (addr sdk.AccAddress, s
s, nextIndex, err = ExtractString(nextIndex, key)
return addr, s, nextIndex, err
}

// ToStr takes the full key and converts it to a string
func ToStr(key []byte) string {
return string(key)
}

// NoLastByte returns sub-slice of the key without the last byte.
// Panics if length of key is zero.
func NoLastByte(key []byte) string {
return string(key[:len(key)-1])
}
6 changes: 0 additions & 6 deletions util/store/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,3 @@ func SumCoins(s storetypes.KVStore, f StrExtractor) sdk.Coins {
// StrExtractor is a function type which will take a bytes string value and extracts
// string out of it.
type StrExtractor func([]byte) string

// NoLastByte returns sub-slice of the key without the last byte.
// Panics if length of key is zero.
func NoLastByte(key []byte) string {
return string(key[:len(key)-1])
}
3 changes: 2 additions & 1 deletion util/store/iter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"gotest.tools/v3/assert"

"github.com/umee-network/umee/v5/tests/tsdk"
"github.com/umee-network/umee/v5/util/keys"
)

func TestIterate(t *testing.T) {
Expand Down Expand Up @@ -74,7 +75,7 @@ func TestSumCoins(t *testing.T) {
}

pdb := prefixstore.NewStore(db, []byte(prefix))
sum := SumCoins(pdb, NoLastByte)
sum := SumCoins(pdb, keys.NoLastByte)
sum.Sort()
assert.DeepEqual(t, expected, sum)
}
4 changes: 2 additions & 2 deletions x/incentive/keeper/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ func (k Keeper) getAllAccountUnbondings(ctx sdk.Context) ([]incentive.AccountUnb

// getAllTotalUnbonding gets total unbonding for all uTokens (used for a query)
func (k Keeper) getAllTotalUnbonding(ctx sdk.Context) sdk.Coins {
return store.SumCoins(k.prefixStore(ctx, keyPrefixTotalUnbonding), store.NoLastByte)
return store.SumCoins(k.prefixStore(ctx, keyPrefixTotalUnbonding), keys.ToStr)
}

// getAllTotalBonded gets total bonded for all uTokens (used for a query)
func (k Keeper) getAllTotalBonded(ctx sdk.Context) sdk.Coins {
return store.SumCoins(k.prefixStore(ctx, keyPrefixTotalBonded), store.NoLastByte)
return store.SumCoins(k.prefixStore(ctx, keyPrefixTotalBonded), keys.NoLastByte)
robert-zaremba marked this conversation as resolved.
Show resolved Hide resolved
}
8 changes: 4 additions & 4 deletions x/incentive/keeper/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ func keyIncentiveProgram(id uint32, status incentive.ProgramStatus) []byte {

// keyTotalBonded returns a KVStore key for total bonded uTokens of a given denom.
func keyTotalBonded(denom string) []byte {
// totalBondedPrefix | denom | 0x00
return util.ConcatBytes(1, keyPrefixTotalBonded, []byte(denom))
// totalBondedPrefix | denom
return util.ConcatBytes(0, keyPrefixTotalBonded, []byte(denom))
toteki marked this conversation as resolved.
Show resolved Hide resolved
}

// keyTotalUnbonding returns a KVStore key for total unbonding uTokens of a given denom.
func keyTotalUnbonding(denom string) []byte {
// totalUnbondingPrefix | denom | 0x00
return util.ConcatBytes(1, keyPrefixTotalUnbonding, []byte(denom))
// totalUnbondingPrefix | denom
return util.ConcatBytes(0, keyPrefixTotalUnbonding, []byte(denom))
}

// keyBondAmount returns a KVStore key for bonded amounts for a uToken denom and account.
Expand Down
5 changes: 3 additions & 2 deletions x/leverage/keeper/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/umee-network/umee/v5/util"
"github.com/umee-network/umee/v5/util/keys"
"github.com/umee-network/umee/v5/util/store"
"github.com/umee-network/umee/v5/x/leverage/types"
)
Expand Down Expand Up @@ -177,12 +178,12 @@ func (k Keeper) SweepBadDebts(ctx sdk.Context) error {

// GetAllUTokenSupply returns total supply of all uToken denoms.
func (k Keeper) GetAllUTokenSupply(ctx sdk.Context) sdk.Coins {
return store.SumCoins(k.prefixStore(ctx, types.KeyPrefixUtokenSupply), store.NoLastByte)
return store.SumCoins(k.prefixStore(ctx, types.KeyPrefixUtokenSupply), keys.NoLastByte)
}

// GetAllReserves returns all reserves.
func (k Keeper) GetAllReserves(ctx sdk.Context) sdk.Coins {
return store.SumCoins(k.prefixStore(ctx, types.KeyPrefixReserveAmount), store.NoLastByte)
return store.SumCoins(k.prefixStore(ctx, types.KeyPrefixReserveAmount), keys.NoLastByte)
toteki marked this conversation as resolved.
Show resolved Hide resolved
}

func (k Keeper) prefixStore(ctx sdk.Context, p []byte) storetypes.KVStore {
Expand Down