From 78e98a60c45935423e78d173074615193d717b3e Mon Sep 17 00:00:00 2001 From: Emmanuel T Odeke Date: Sat, 11 Dec 2021 15:04:38 -0800 Subject: [PATCH] perf: x/*: remove unnecessary byte<->string conversions in fmt and read-only bytes (#10739) --- x/bank/keeper/keeper.go | 11 +++++----- x/gov/client/utils/query.go | 44 ++++++++++++++++++------------------- x/upgrade/keeper/keeper.go | 3 ++- x/upgrade/plan/info.go | 4 +++- 4 files changed, 33 insertions(+), 29 deletions(-) diff --git a/x/bank/keeper/keeper.go b/x/bank/keeper/keeper.go index b5f80c5f6..31ed533c7 100644 --- a/x/bank/keeper/keeper.go +++ b/x/bank/keeper/keeper.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/internal/conv" "github.com/cosmos/cosmos-sdk/store/prefix" storetypes "github.com/cosmos/cosmos-sdk/store/types" sdk "github.com/cosmos/cosmos-sdk/types" @@ -196,7 +197,7 @@ func (k BaseKeeper) GetSupply(ctx sdk.Context, denom string) sdk.Coin { store := ctx.KVStore(k.storeKey) supplyStore := prefix.NewStore(store, types.SupplyKey) - bz := supplyStore.Get([]byte(denom)) + bz := supplyStore.Get(conv.UnsafeStrToBytes(denom)) if bz == nil { return sdk.Coin{ Denom: denom, @@ -220,7 +221,7 @@ func (k BaseKeeper) GetSupply(ctx sdk.Context, denom string) sdk.Coin { func (k BaseKeeper) HasSupply(ctx sdk.Context, denom string) bool { store := ctx.KVStore(k.storeKey) supplyStore := prefix.NewStore(store, types.SupplyKey) - return supplyStore.Has([]byte(denom)) + return supplyStore.Has(conv.UnsafeStrToBytes(denom)) } // GetDenomMetaData retrieves the denomination metadata. returns the metadata and true if the denom exists, @@ -229,7 +230,7 @@ func (k BaseKeeper) GetDenomMetaData(ctx sdk.Context, denom string) (types.Metad store := ctx.KVStore(k.storeKey) store = prefix.NewStore(store, types.DenomMetadataPrefix) - bz := store.Get([]byte(denom)) + bz := store.Get(conv.UnsafeStrToBytes(denom)) if bz == nil { return types.Metadata{}, false } @@ -244,7 +245,7 @@ func (k BaseKeeper) GetDenomMetaData(ctx sdk.Context, denom string) (types.Metad func (k BaseKeeper) HasDenomMetaData(ctx sdk.Context, denom string) bool { store := ctx.KVStore(k.storeKey) store = prefix.NewStore(store, types.DenomMetadataPrefix) - return store.Has([]byte(denom)) + return store.Has(conv.UnsafeStrToBytes(denom)) } // GetAllDenomMetaData retrieves all denominations metadata @@ -457,7 +458,7 @@ func (k BaseKeeper) setSupply(ctx sdk.Context, coin sdk.Coin) { // Bank invariants and IBC requires to remove zero coins. if coin.IsZero() { - supplyStore.Delete([]byte(coin.GetDenom())) + supplyStore.Delete(conv.UnsafeStrToBytes(coin.GetDenom())) } else { supplyStore.Set([]byte(coin.GetDenom()), intBytes) } diff --git a/x/gov/client/utils/query.go b/x/gov/client/utils/query.go index f4799a89d..f773c49c7 100644 --- a/x/gov/client/utils/query.go +++ b/x/gov/client/utils/query.go @@ -55,12 +55,12 @@ func QueryDepositsByTxQuery(clientCtx client.Context, params types.QueryProposal // Query legacy Msgs event action []string{ fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, types.TypeMsgDeposit), - fmt.Sprintf("%s.%s='%s'", types.EventTypeProposalDeposit, types.AttributeKeyProposalID, []byte(fmt.Sprintf("%d", params.ProposalID))), + fmt.Sprintf("%s.%s='%d'", types.EventTypeProposalDeposit, types.AttributeKeyProposalID, params.ProposalID), }, // Query proto Msgs event action []string{ fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, sdk.MsgTypeURL(&types.MsgDeposit{})), - fmt.Sprintf("%s.%s='%s'", types.EventTypeProposalDeposit, types.AttributeKeyProposalID, []byte(fmt.Sprintf("%d", params.ProposalID))), + fmt.Sprintf("%s.%s='%d'", types.EventTypeProposalDeposit, types.AttributeKeyProposalID, params.ProposalID), }, ) if err != nil { @@ -105,22 +105,22 @@ func QueryVotesByTxQuery(clientCtx client.Context, params types.QueryProposalVot // Query legacy Vote Msgs []string{ fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, types.TypeMsgVote), - fmt.Sprintf("%s.%s='%s'", types.EventTypeProposalVote, types.AttributeKeyProposalID, []byte(fmt.Sprintf("%d", params.ProposalID))), + fmt.Sprintf("%s.%s='%d'", types.EventTypeProposalVote, types.AttributeKeyProposalID, params.ProposalID), }, // Query Vote proto Msgs []string{ fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, sdk.MsgTypeURL(&types.MsgVote{})), - fmt.Sprintf("%s.%s='%s'", types.EventTypeProposalVote, types.AttributeKeyProposalID, []byte(fmt.Sprintf("%d", params.ProposalID))), + fmt.Sprintf("%s.%s='%d'", types.EventTypeProposalVote, types.AttributeKeyProposalID, params.ProposalID), }, // Query legacy VoteWeighted Msgs []string{ fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, types.TypeMsgVoteWeighted), - fmt.Sprintf("%s.%s='%s'", types.EventTypeProposalVote, types.AttributeKeyProposalID, []byte(fmt.Sprintf("%d", params.ProposalID))), + fmt.Sprintf("%s.%s='%d'", types.EventTypeProposalVote, types.AttributeKeyProposalID, params.ProposalID), }, // Query VoteWeighted proto Msgs []string{ fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, sdk.MsgTypeURL(&types.MsgVoteWeighted{})), - fmt.Sprintf("%s.%s='%s'", types.EventTypeProposalVote, types.AttributeKeyProposalID, []byte(fmt.Sprintf("%d", params.ProposalID))), + fmt.Sprintf("%s.%s='%d'", types.EventTypeProposalVote, types.AttributeKeyProposalID, params.ProposalID), }, ) if err != nil { @@ -174,26 +174,26 @@ func QueryVoteByTxQuery(clientCtx client.Context, params types.QueryVoteParams) // Query legacy Vote Msgs []string{ fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, types.TypeMsgVote), - fmt.Sprintf("%s.%s='%s'", types.EventTypeProposalVote, types.AttributeKeyProposalID, []byte(fmt.Sprintf("%d", params.ProposalID))), - fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeySender, []byte(params.Voter.String())), + fmt.Sprintf("%s.%s='%d'", types.EventTypeProposalVote, types.AttributeKeyProposalID, params.ProposalID), + fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeySender, params.Voter), }, // Query Vote proto Msgs []string{ fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, sdk.MsgTypeURL(&types.MsgVote{})), - fmt.Sprintf("%s.%s='%s'", types.EventTypeProposalVote, types.AttributeKeyProposalID, []byte(fmt.Sprintf("%d", params.ProposalID))), - fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeySender, []byte(params.Voter.String())), + fmt.Sprintf("%s.%s='%d'", types.EventTypeProposalVote, types.AttributeKeyProposalID, params.ProposalID), + fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeySender, params.Voter.String()), }, // Query legacy VoteWeighted Msgs []string{ fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, types.TypeMsgVoteWeighted), - fmt.Sprintf("%s.%s='%s'", types.EventTypeProposalVote, types.AttributeKeyProposalID, []byte(fmt.Sprintf("%d", params.ProposalID))), - fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeySender, []byte(params.Voter.String())), + fmt.Sprintf("%s.%s='%d'", types.EventTypeProposalVote, types.AttributeKeyProposalID, params.ProposalID), + fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeySender, params.Voter.String()), }, // Query VoteWeighted proto Msgs []string{ fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, sdk.MsgTypeURL(&types.MsgVoteWeighted{})), - fmt.Sprintf("%s.%s='%s'", types.EventTypeProposalVote, types.AttributeKeyProposalID, []byte(fmt.Sprintf("%d", params.ProposalID))), - fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeySender, []byte(params.Voter.String())), + fmt.Sprintf("%s.%s='%d'", types.EventTypeProposalVote, types.AttributeKeyProposalID, params.ProposalID), + fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeySender, params.Voter), }, ) if err != nil { @@ -258,14 +258,14 @@ func QueryDepositByTxQuery(clientCtx client.Context, params types.QueryDepositPa // Query legacy Msgs event action []string{ fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, types.TypeMsgDeposit), - fmt.Sprintf("%s.%s='%s'", types.EventTypeProposalDeposit, types.AttributeKeyProposalID, []byte(fmt.Sprintf("%d", params.ProposalID))), - fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeySender, []byte(params.Depositor.String())), + fmt.Sprintf("%s.%s='%d'", types.EventTypeProposalDeposit, types.AttributeKeyProposalID, params.ProposalID), + fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeySender, params.Depositor.String()), }, // Query proto Msgs event action []string{ fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, sdk.MsgTypeURL(&types.MsgDeposit{})), - fmt.Sprintf("%s.%s='%s'", types.EventTypeProposalDeposit, types.AttributeKeyProposalID, []byte(fmt.Sprintf("%d", params.ProposalID))), - fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeySender, []byte(params.Depositor.String())), + fmt.Sprintf("%s.%s='%d'", types.EventTypeProposalDeposit, types.AttributeKeyProposalID, params.ProposalID), + fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeySender, params.Depositor.String()), }, ) if err != nil { @@ -304,12 +304,12 @@ func QueryProposerByTxQuery(clientCtx client.Context, proposalID uint64) (Propos // Query legacy Msgs event action []string{ fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, types.TypeMsgSubmitProposal), - fmt.Sprintf("%s.%s='%s'", types.EventTypeSubmitProposal, types.AttributeKeyProposalID, []byte(fmt.Sprintf("%d", proposalID))), + fmt.Sprintf("%s.%s='%d'", types.EventTypeSubmitProposal, types.AttributeKeyProposalID, proposalID), }, // Query proto Msgs event action []string{ fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, sdk.MsgTypeURL(&types.MsgSubmitProposal{})), - fmt.Sprintf("%s.%s='%s'", types.EventTypeSubmitProposal, types.AttributeKeyProposalID, []byte(fmt.Sprintf("%d", proposalID))), + fmt.Sprintf("%s.%s='%d'", types.EventTypeSubmitProposal, types.AttributeKeyProposalID, proposalID), }, ) if err != nil { @@ -374,12 +374,12 @@ func queryInitialDepositByTxQuery(clientCtx client.Context, proposalID uint64) ( // Query legacy Msgs event action []string{ fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, types.TypeMsgSubmitProposal), - fmt.Sprintf("%s.%s='%s'", types.EventTypeSubmitProposal, types.AttributeKeyProposalID, []byte(fmt.Sprintf("%d", proposalID))), + fmt.Sprintf("%s.%s='%d'", types.EventTypeSubmitProposal, types.AttributeKeyProposalID, proposalID), }, // Query proto Msgs event action []string{ fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, sdk.MsgTypeURL(&types.MsgSubmitProposal{})), - fmt.Sprintf("%s.%s='%s'", types.EventTypeSubmitProposal, types.AttributeKeyProposalID, []byte(fmt.Sprintf("%d", proposalID))), + fmt.Sprintf("%s.%s='%d'", types.EventTypeSubmitProposal, types.AttributeKeyProposalID, proposalID), }, ) diff --git a/x/upgrade/keeper/keeper.go b/x/upgrade/keeper/keeper.go index 8b667ad31..d342ad9a4 100644 --- a/x/upgrade/keeper/keeper.go +++ b/x/upgrade/keeper/keeper.go @@ -13,6 +13,7 @@ import ( tmos "github.com/tendermint/tendermint/libs/os" "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/internal/conv" "github.com/cosmos/cosmos-sdk/store/prefix" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" @@ -230,7 +231,7 @@ func (k Keeper) GetUpgradedConsensusState(ctx sdk.Context, lastHeight int64) ([] // GetDoneHeight returns the height at which the given upgrade was executed func (k Keeper) GetDoneHeight(ctx sdk.Context, name string) int64 { store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte{types.DoneByte}) - bz := store.Get([]byte(name)) + bz := store.Get(conv.UnsafeStrToBytes(name)) if len(bz) == 0 { return 0 } diff --git a/x/upgrade/plan/info.go b/x/upgrade/plan/info.go index 5f146a9c9..9661844c2 100644 --- a/x/upgrade/plan/info.go +++ b/x/upgrade/plan/info.go @@ -9,6 +9,8 @@ import ( "path/filepath" "regexp" "strings" + + "github.com/cosmos/cosmos-sdk/internal/conv" ) // Info is the special structure that the Plan.Info string can be (as json). @@ -38,7 +40,7 @@ func ParseInfo(infoStr string) (*Info, error) { // Now, try to parse it into the expected structure. var planInfo Info - if err := json.Unmarshal([]byte(infoStr), &planInfo); err != nil { + if err := json.Unmarshal(conv.UnsafeStrToBytes(infoStr), &planInfo); err != nil { return nil, fmt.Errorf("could not parse plan info: %v", err) }