From d0f64dff2cc370b876c82244bd46b61872d6ffa5 Mon Sep 17 00:00:00 2001 From: Emmanuel T Odeke Date: Thu, 14 Oct 2021 01:53:38 -0700 Subject: [PATCH] perf: avoid unnecessary byteslice->string before fmt %s verb (#10364) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fmt.Printf or fmt.Sprintf already know how to convert a byteslice into a string when building the output; we shouldn't incur the unnecessary string(byteslice) conversion. Using Bencher, we can see improvements such as https://dashboard.github.orijtech.com/benchmark/3245b8e4bbbd44a597480319aaa4b9fe which in independent experiments show: * time/op (ns/op) FormatIt-8 1.2µs ± 2% 1.1µs ± 10% -11.77% (p=0.000 n=10+9) * speed (MB/s) FormatIt-8 0.71GB/s ± 2% 0.80GB/s ± 9% +13.59% (p=0.000 n=10+9) * allocs/op (B/op) FormatIt-8 2.0kB ± 0% 1.1kB ± 0% -45.62% (p=0.000 n=10+10) * allocs/op (count/op) FormatIt-8 11 ± 0% 9.0 ± 0% -18.18% (p=0.000 n=10+10) Fixes #10363 --- client/keys/add_ledger_test.go | 4 ++-- client/keys/add_test.go | 10 +++++----- crypto/keyring/signing_algorithms_test.go | 2 +- x/capability/simulation/decoder.go | 2 +- x/genutil/client/cli/init.go | 2 +- x/params/types/subspace.go | 6 +++--- x/upgrade/client/cli/query.go | 2 +- 7 files changed, 14 insertions(+), 14 deletions(-) diff --git a/client/keys/add_ledger_test.go b/client/keys/add_ledger_test.go index 1901a3e3261f..90ffc16b78f5 100644 --- a/client/keys/add_ledger_test.go +++ b/client/keys/add_ledger_test.go @@ -54,7 +54,7 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) { fmt.Sprintf("--%s=0", flagIndex), fmt.Sprintf("--%s=330", flagCoinType), fmt.Sprintf("--%s=%s", cli.OutputFlag, OutputFormatText), - fmt.Sprintf("--%s=%s", flags.FlagKeyAlgorithm, string(hd.Secp256k1Type)), + fmt.Sprintf("--%s=%s", flags.FlagKeyAlgorithm, hd.Secp256k1Type), fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest), }) @@ -104,7 +104,7 @@ func Test_runAddCmdLedger(t *testing.T) { "keyname1", fmt.Sprintf("--%s=true", flags.FlagUseLedger), fmt.Sprintf("--%s=%s", cli.OutputFlag, OutputFormatText), - fmt.Sprintf("--%s=%s", flags.FlagKeyAlgorithm, string(hd.Secp256k1Type)), + fmt.Sprintf("--%s=%s", flags.FlagKeyAlgorithm, hd.Secp256k1Type), fmt.Sprintf("--%s=%d", flagCoinType, sdk.CoinType), fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest), }) diff --git a/client/keys/add_test.go b/client/keys/add_test.go index b47210194cfc..6368fec6f1b9 100644 --- a/client/keys/add_test.go +++ b/client/keys/add_test.go @@ -45,7 +45,7 @@ func Test_runAddCmdBasic(t *testing.T) { "keyname1", fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome), fmt.Sprintf("--%s=%s", cli.OutputFlag, OutputFormatText), - fmt.Sprintf("--%s=%s", flags.FlagKeyAlgorithm, string(hd.Secp256k1Type)), + fmt.Sprintf("--%s=%s", flags.FlagKeyAlgorithm, hd.Secp256k1Type), fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest), }) mockIn.Reset("y\n") @@ -58,7 +58,7 @@ func Test_runAddCmdBasic(t *testing.T) { "keyname2", fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome), fmt.Sprintf("--%s=%s", cli.OutputFlag, OutputFormatText), - fmt.Sprintf("--%s=%s", flags.FlagKeyAlgorithm, string(hd.Secp256k1Type)), + fmt.Sprintf("--%s=%s", flags.FlagKeyAlgorithm, hd.Secp256k1Type), fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest), }) @@ -72,7 +72,7 @@ func Test_runAddCmdBasic(t *testing.T) { "keyname4", fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome), fmt.Sprintf("--%s=%s", cli.OutputFlag, OutputFormatText), - fmt.Sprintf("--%s=%s", flags.FlagKeyAlgorithm, string(hd.Secp256k1Type)), + fmt.Sprintf("--%s=%s", flags.FlagKeyAlgorithm, hd.Secp256k1Type), fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest), }) @@ -84,7 +84,7 @@ func Test_runAddCmdBasic(t *testing.T) { fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome), fmt.Sprintf("--%s=true", flags.FlagDryRun), fmt.Sprintf("--%s=%s", cli.OutputFlag, OutputFormatText), - fmt.Sprintf("--%s=%s", flags.FlagKeyAlgorithm, string(hd.Secp256k1Type)), + fmt.Sprintf("--%s=%s", flags.FlagKeyAlgorithm, hd.Secp256k1Type), }) require.NoError(t, cmd.ExecuteContext(ctx)) @@ -247,7 +247,7 @@ func TestAddRecoverFileBackend(t *testing.T) { "keyname1", fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome), fmt.Sprintf("--%s=%s", cli.OutputFlag, OutputFormatText), - fmt.Sprintf("--%s=%s", flags.FlagKeyAlgorithm, string(hd.Secp256k1Type)), + fmt.Sprintf("--%s=%s", flags.FlagKeyAlgorithm, hd.Secp256k1Type), fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendFile), fmt.Sprintf("--%s", flagRecover), }) diff --git a/crypto/keyring/signing_algorithms_test.go b/crypto/keyring/signing_algorithms_test.go index 14283b910846..e99492f12a17 100644 --- a/crypto/keyring/signing_algorithms_test.go +++ b/crypto/keyring/signing_algorithms_test.go @@ -56,7 +56,7 @@ func TestAltSigningAlgoList_Contains(t *testing.T) { func TestAltSigningAlgoList_String(t *testing.T) { list := SigningAlgoList{hd.Secp256k1, notSupportedAlgo{}} - require.Equal(t, fmt.Sprintf("%s,notSupported", string(hd.Secp256k1Type)), list.String()) + require.Equal(t, fmt.Sprintf("%s,notSupported", hd.Secp256k1Type), list.String()) } type notSupportedAlgo struct { diff --git a/x/capability/simulation/decoder.go b/x/capability/simulation/decoder.go index 96e2c41c0a2e..3cea7bb8893b 100644 --- a/x/capability/simulation/decoder.go +++ b/x/capability/simulation/decoder.go @@ -27,7 +27,7 @@ func NewDecodeStore(cdc codec.Codec) func(kvA, kvB kv.Pair) string { return fmt.Sprintf("CapabilityOwners A: %v\nCapabilityOwners B: %v\n", capOwnersA, capOwnersB) default: - panic(fmt.Sprintf("invalid %s key prefix %X (%s)", types.ModuleName, kvA.Key, string(kvA.Key))) + panic(fmt.Sprintf("invalid %s key prefix %X (%s)", types.ModuleName, kvA.Key, kvA.Key)) } } } diff --git a/x/genutil/client/cli/init.go b/x/genutil/client/cli/init.go index 7931e2634c59..b36f1bbeefd6 100644 --- a/x/genutil/client/cli/init.go +++ b/x/genutil/client/cli/init.go @@ -61,7 +61,7 @@ func displayInfo(info printInfo) error { return err } - _, err = fmt.Fprintf(os.Stderr, "%s\n", string(sdk.MustSortJSON(out))) + _, err = fmt.Fprintf(os.Stderr, "%s\n", sdk.MustSortJSON(out)) return err } diff --git a/x/params/types/subspace.go b/x/params/types/subspace.go index dd316cbd42ef..e528a185eb9e 100644 --- a/x/params/types/subspace.go +++ b/x/params/types/subspace.go @@ -88,7 +88,7 @@ func (s Subspace) transientStore(ctx sdk.Context) sdk.KVStore { func (s Subspace) Validate(ctx sdk.Context, key []byte, value interface{}) error { attr, ok := s.table.m[string(key)] if !ok { - return fmt.Errorf("parameter %s not registered", string(key)) + return fmt.Errorf("parameter %s not registered", key) } if err := attr.vfn(value); err != nil { @@ -167,7 +167,7 @@ func (s Subspace) Modified(ctx sdk.Context, key []byte) bool { func (s Subspace) checkType(key []byte, value interface{}) { attr, ok := s.table.m[string(key)] if !ok { - panic(fmt.Sprintf("parameter %s not registered", string(key))) + panic(fmt.Sprintf("parameter %s not registered", key)) } ty := attr.ty @@ -209,7 +209,7 @@ func (s Subspace) Set(ctx sdk.Context, key []byte, value interface{}) { func (s Subspace) Update(ctx sdk.Context, key, value []byte) error { attr, ok := s.table.m[string(key)] if !ok { - panic(fmt.Sprintf("parameter %s not registered", string(key))) + panic(fmt.Sprintf("parameter %s not registered", key)) } ty := attr.ty diff --git a/x/upgrade/client/cli/query.go b/x/upgrade/client/cli/query.go index 2460d29addd5..aebb5795b2a6 100644 --- a/x/upgrade/client/cli/query.go +++ b/x/upgrade/client/cli/query.go @@ -104,7 +104,7 @@ func GetAppliedPlanCmd() *cobra.Command { if err != nil { return err } - return clientCtx.PrintString(fmt.Sprintf("%s\n", string(bz))) + return clientCtx.PrintString(fmt.Sprintf("%s\n", bz)) }, }