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
34 lines
1.1 KiB
Go
34 lines
1.1 KiB
Go
package simulation
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
"github.com/cosmos/cosmos-sdk/types/kv"
|
|
"github.com/cosmos/cosmos-sdk/x/capability/types"
|
|
)
|
|
|
|
// NewDecodeStore returns a decoder function closure that unmarshals the KVPair's
|
|
// Value to the corresponding capability type.
|
|
func NewDecodeStore(cdc codec.Codec) func(kvA, kvB kv.Pair) string {
|
|
return func(kvA, kvB kv.Pair) string {
|
|
switch {
|
|
case bytes.Equal(kvA.Key, types.KeyIndex):
|
|
idxA := sdk.BigEndianToUint64(kvA.Value)
|
|
idxB := sdk.BigEndianToUint64(kvB.Value)
|
|
return fmt.Sprintf("Index A: %d\nIndex B: %d\n", idxA, idxB)
|
|
|
|
case bytes.HasPrefix(kvA.Key, types.KeyPrefixIndexCapability):
|
|
var capOwnersA, capOwnersB types.CapabilityOwners
|
|
cdc.MustUnmarshal(kvA.Value, &capOwnersA)
|
|
cdc.MustUnmarshal(kvB.Value, &capOwnersB)
|
|
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, kvA.Key))
|
|
}
|
|
}
|
|
}
|