-
Notifications
You must be signed in to change notification settings - Fork 275
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: use fast unsafe bytes->string convertion (#525)
* perf: use fast unsafe bytes->string convertion * code style * update changelog * add fast convertions to cache * Update CHANGELOG.md Co-authored-by: Roman <ackhtariev@gmail.com> * one more tiny value cache Co-authored-by: Roman <ackhtariev@gmail.com>
- Loading branch information
1 parent
76cec48
commit 3e26f26
Showing
11 changed files
with
133 additions
and
47 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
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
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,26 @@ | ||
package common | ||
|
||
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 | ||
} | ||
|
||
// 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 *(*string)(unsafe.Pointer(&b)) | ||
} |
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,54 @@ | ||
package common | ||
|
||
import ( | ||
"runtime" | ||
"strconv" | ||
"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) | ||
} | ||
} | ||
|
||
func BenchmarkUnsafeStrToBytes(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
UnsafeStrToBytes(strconv.Itoa(i)) | ||
} | ||
} |
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
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,8 @@ | ||
package iavl | ||
|
||
import ibytes "github.com/cosmos/iavl/internal/bytes" | ||
|
||
var ( | ||
unsafeToStr = ibytes.UnsafeBytesToStr | ||
unsafeToBz = ibytes.UnsafeStrToBytes | ||
) |
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