Skip to content

Commit

Permalink
refactor: using unsafe.String and unsafe.SliceData (#21412)
Browse files Browse the repository at this point in the history
(cherry picked from commit d56bbb8)

# Conflicts:
#	store/internal/conv/string.go
#	store/v2/internal/conv/string.go
  • Loading branch information
cuiweixie authored and mergify[bot] committed Sep 4, 2024
1 parent fa8ea23 commit 157af92
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Every module contains its own CHANGELOG.md. Please refer to the module you are i
### Improvements

* (client) [#21436](https://github.com/cosmos/cosmos-sdk/pull/21436) Use `address.Codec` from client.Context in `tx.Sign`.
* (internal) [#21412](https://github.com/cosmos/cosmos-sdk/pull/21412) Using unsafe.String and unsafe.SliceData.

### API Breaking Changes

Expand Down
2 changes: 1 addition & 1 deletion internal/conv/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,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))
}
19 changes: 19 additions & 0 deletions store/internal/conv/string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package conv

import (
"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 {
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))
}
19 changes: 19 additions & 0 deletions store/v2/internal/conv/string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package conv

import (
"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 {
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))
}
2 changes: 1 addition & 1 deletion x/authz/internal/conv/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,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))
}
2 changes: 1 addition & 1 deletion x/nft/internal/conv/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,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))
}

0 comments on commit 157af92

Please sign in to comment.