diff --git a/codec/address/bech32_codec.go b/codec/address/bech32_codec.go index 40cb29529d1dc..5d3200ff8541f 100644 --- a/codec/address/bech32_codec.go +++ b/codec/address/bech32_codec.go @@ -5,13 +5,13 @@ import ( "fmt" "strings" "sync" + "unsafe" "github.com/hashicorp/golang-lru/simplelru" "cosmossdk.io/core/address" errorsmod "cosmossdk.io/errors" - "github.com/cosmos/cosmos-sdk/internal/conv" sdkAddress "github.com/cosmos/cosmos-sdk/types/address" "github.com/cosmos/cosmos-sdk/types/bech32" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" @@ -115,7 +115,7 @@ func (cbc cachedBech32Codec) BytesToString(bz []byte) (string, error) { return "", nil } - key := conv.UnsafeBytesToStr(bz) + key := unsafe.String(unsafe.SliceData(bz), len(bz)) cbc.mu.Lock() defer cbc.mu.Unlock() diff --git a/codec/address/bech32_codec_test.go b/codec/address/bech32_codec_test.go index faa8432f07d0e..7ff55d4196b94 100644 --- a/codec/address/bech32_codec_test.go +++ b/codec/address/bech32_codec_test.go @@ -5,11 +5,10 @@ import ( "sync" "sync/atomic" "testing" + "unsafe" "github.com/hashicorp/golang-lru/simplelru" "github.com/stretchr/testify/require" - - "github.com/cosmos/cosmos-sdk/internal/conv" ) var ( @@ -141,7 +140,7 @@ func TestBech32Codec(t *testing.T) { accAddr, err := ac.BytesToString(addr) require.NoError(t, err) - cachedStrAddr, ok := tt.lru.Get(conv.UnsafeBytesToStr(addr)) + cachedStrAddr, ok := tt.lru.Get(unsafe.String(unsafe.SliceData(addr), len(addr))) require.True(t, ok) cachedStrAddrMap, ok := cachedStrAddr.(map[string]string) require.True(t, ok) diff --git a/internal/conv/string.go b/internal/conv/string.go index fa9e507be06da..f78334057be04 100644 --- a/internal/conv/string.go +++ b/internal/conv/string.go @@ -9,11 +9,3 @@ import ( func UnsafeStrToBytes(s string) []byte { return unsafe.Slice(unsafe.StringData(s), len(s)) // ref https://github.com/golang/go/issues/53003#issuecomment-1140276077 } - -// UnsafeBytesToStr is meant to make a zero allocation conversion -// from []byte -> string to speed up operations, it is not meant -// to be used generally, but for a specific pattern to delete keys -// from a map. -func UnsafeBytesToStr(b []byte) string { - return unsafe.String(unsafe.SliceData(b), len(b)) -} diff --git a/internal/conv/string_test.go b/internal/conv/string_test.go index 3a1451753188c..8e26c973866ba 100644 --- a/internal/conv/string_test.go +++ b/internal/conv/string_test.go @@ -1,12 +1,11 @@ package conv import ( + "github.com/stretchr/testify/suite" "runtime" "strconv" "testing" "time" - - "github.com/stretchr/testify/suite" ) func TestStringSuite(t *testing.T) { @@ -32,21 +31,6 @@ func (s *StringSuite) TestUnsafeStrToBytes() { } } -func unsafeConvertBytes() string { - return UnsafeBytesToStr([]byte("abc")) -} - -func (s *StringSuite) TestUnsafeBytesToStr() { - // we convert in other function to trigger GC. We want to check that - // the underlying array in []bytes is accessible after GC will finish swapping. - for i := 0; i < 5; i++ { - str := unsafeConvertBytes() - runtime.GC() - <-time.NewTimer(2 * time.Millisecond).C - s.Equal("abc", str) - } -} - func BenchmarkUnsafeStrToBytes(b *testing.B) { for i := 0; i < b.N; i++ { UnsafeStrToBytes(strconv.Itoa(i)) diff --git a/types/address.go b/types/address.go index 8b19141e65694..a890001c86742 100644 --- a/types/address.go +++ b/types/address.go @@ -8,13 +8,13 @@ import ( "fmt" "sync" "sync/atomic" + "unsafe" "github.com/hashicorp/golang-lru/simplelru" "sigs.k8s.io/yaml" addresscodec "github.com/cosmos/cosmos-sdk/codec/address" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" - "github.com/cosmos/cosmos-sdk/internal/conv" "github.com/cosmos/cosmos-sdk/types/bech32" ) @@ -286,7 +286,7 @@ func (aa AccAddress) String() string { return "" } - key := conv.UnsafeBytesToStr(aa) + key := unsafe.String(unsafe.SliceData(aa), len(aa)) if IsAddrCacheEnabled() { accAddrMu.Lock() @@ -437,7 +437,7 @@ func (va ValAddress) String() string { return "" } - key := conv.UnsafeBytesToStr(va) + key := unsafe.String(unsafe.SliceData(va), len(va)) if IsAddrCacheEnabled() { valAddrMu.Lock() @@ -584,7 +584,7 @@ func (ca ConsAddress) String() string { return "" } - key := conv.UnsafeBytesToStr(ca) + key := unsafe.String(unsafe.SliceData(ca), len(ca)) if IsAddrCacheEnabled() { consAddrMu.Lock() diff --git a/types/address/hash.go b/types/address/hash.go index ee7398518fb2f..1b827337db708 100644 --- a/types/address/hash.go +++ b/types/address/hash.go @@ -5,6 +5,7 @@ import ( "crypto/sha256" "fmt" "sort" + "unsafe" "github.com/cometbft/cometbft/crypto" @@ -89,5 +90,5 @@ func Module(moduleName string, derivationKeys ...[]byte) []byte { // This function is used to create a sub accounts. To create a module accounts use the // `Module` function. func Derive(address, key []byte) []byte { - return Hash(conv.UnsafeBytesToStr(address), key) + return Hash(unsafe.String(unsafe.SliceData(address), len(address)), key) }