Skip to content

Commit

Permalink
refactor: using unsafe.String and unsafe.SliceData
Browse files Browse the repository at this point in the history
  • Loading branch information
cuiweixie committed Aug 27, 2024
1 parent 74cedea commit 0a35f44
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 35 deletions.
4 changes: 2 additions & 2 deletions codec/address/bech32_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()

Expand Down
5 changes: 2 additions & 3 deletions codec/address/bech32_codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 0 additions & 8 deletions internal/conv/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
18 changes: 1 addition & 17 deletions internal/conv/string_test.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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))
Expand Down
8 changes: 4 additions & 4 deletions types/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion types/address/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/sha256"
"fmt"
"sort"
"unsafe"

"github.com/cometbft/cometbft/crypto"

Expand Down Expand Up @@ -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)
}

0 comments on commit 0a35f44

Please sign in to comment.