-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal: create package for unsafe bytes convertion (#8733)
Co-authored-by: Alessio Treglia <alessio@tendermint.com>
- Loading branch information
1 parent
0792db7
commit 5f2b90c
Showing
6 changed files
with
77 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Package conv provides internal functions for convertions and data manipulation | ||
package conv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package conv | ||
|
||
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)(unsafe.Pointer(&s)) | ||
(*reflect.SliceHeader)(unsafe.Pointer(&buf)).Cap = len(s) | ||
return buf | ||
} | ||
|
||
// 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 { | ||
hdr := (*reflect.StringHeader)(unsafe.Pointer(&b)) | ||
return *(*string)(unsafe.Pointer(hdr)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package conv | ||
|
||
import ( | ||
"runtime" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
func TestStringSuite(t *testing.T) { | ||
suite.Run(t, new(StringSuite)) | ||
} | ||
|
||
type StringSuite struct{ suite.Suite } | ||
|
||
func unsafeConvertStr() []byte { | ||
return UnsafeStrToBytes("abc") | ||
} | ||
|
||
func (s *StringSuite) TestUnsafeStrToBytes() { | ||
// 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++ { | ||
b := unsafeConvertStr() | ||
runtime.GC() | ||
<-time.NewTimer(2 * time.Millisecond).C | ||
b2 := append(b, 'd') | ||
s.Equal("abc", string(b)) | ||
s.Equal("abcd", string(b2)) | ||
} | ||
} | ||
|
||
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters