diff --git a/libs/cosmos-sdk/store/cachekv/store.go b/libs/cosmos-sdk/store/cachekv/store.go index b569d0b147..dd4d2dc42e 100644 --- a/libs/cosmos-sdk/store/cachekv/store.go +++ b/libs/cosmos-sdk/store/cachekv/store.go @@ -3,7 +3,6 @@ package cachekv import ( "bytes" "io" - "reflect" "sort" "sync" "unsafe" @@ -376,12 +375,7 @@ func (store *Store) iterator(start, end []byte, ascending bool) types.Iterator { // to be used generally, but for a specific pattern to check for available // keys within a domain. func strToByte(s string) []byte { - var b []byte - hdr := (*reflect.SliceHeader)(unsafe.Pointer(&b)) - hdr.Cap = len(s) - hdr.Len = len(s) - hdr.Data = (*reflect.StringHeader)(unsafe.Pointer(&s)).Data - return b + return unsafe.Slice((*byte)(unsafe.Pointer(unsafe.StringData(s))), len(s)) } // byteSliceToStr is meant to make a zero allocation conversion @@ -389,8 +383,7 @@ func strToByte(s string) []byte { // to be used generally, but for a specific pattern to delete keys // from a map. func byteSliceToStr(b []byte) string { - hdr := (*reflect.StringHeader)(unsafe.Pointer(&b)) - return *(*string)(unsafe.Pointer(hdr)) + return unsafe.String(unsafe.SliceData(b), len(b)) } // Constructs a slice of dirty items, to use w/ memIterator. diff --git a/libs/cosmos-sdk/types/string.go b/libs/cosmos-sdk/types/string.go index 587764198f..abed6668ea 100644 --- a/libs/cosmos-sdk/types/string.go +++ b/libs/cosmos-sdk/types/string.go @@ -1,20 +1,13 @@ package types import ( - "reflect" "unsafe" ) // UnsafeStrToBytes uses unsafe to convert string into byte array. Returned bytes // must not be altered after this function is called as it will cause a segmentation fault. func UnsafeStrToBytes(s string) []byte { - var buf []byte - sHdr := (*reflect.StringHeader)(unsafe.Pointer(&s)) - bufHdr := (*reflect.SliceHeader)(unsafe.Pointer(&buf)) - bufHdr.Data = sHdr.Data - bufHdr.Cap = sHdr.Len - bufHdr.Len = sHdr.Len - return buf + return unsafe.Slice((*byte)(unsafe.Pointer(unsafe.StringData(s))), len(s)) } // UnsafeBytesToStr is meant to make a zero allocation conversion @@ -22,5 +15,5 @@ func UnsafeStrToBytes(s string) []byte { // to be used generally, but for a specific pattern to delete keys // from a map. func UnsafeBytesToStr(b []byte) string { - return *(*string)(unsafe.Pointer(&b)) + return unsafe.String(unsafe.SliceData(b), len(b)) }