-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: remove base app from store #14417
Changes from all commits
1d2b1a8
0bae28d
08b1d38
a81f0d3
24c5b44
4eee62f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package conv | ||
|
||
import ( | ||
"reflect" | ||
Check failure Code scanning / gosec Blocklisted import unsafe
Blocklisted import reflect
Check notice Code scanning / CodeQL Sensitive package import
Certain system packages contain functions which may be a possible source of non-determinism
|
||
"unsafe" | ||
Check failure Code scanning / gosec Blocklisted import unsafe
Blocklisted import unsafe
Check notice Code scanning / CodeQL Sensitive package import
Certain system packages contain functions which may be a possible source of non-determinism
|
||
) | ||
|
||
// 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)) | ||
Check warning Code scanning / gosec Use of unsafe calls should be audited
Use of unsafe calls should be audited
|
||
bufHdr := (*reflect.SliceHeader)(unsafe.Pointer(&buf)) | ||
Check warning Code scanning / gosec Use of unsafe calls should be audited
Use of unsafe calls should be audited
|
||
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)) | ||
Check warning Code scanning / gosec Use of unsafe calls should be audited
Use of unsafe calls should be audited
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package conv | ||
|
||
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') //nolint:gocritic // append is fine here | ||
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)) | ||
} | ||
} |
Check failure
Code scanning / gosec
the value in the range statement should be _ unless copying a map: want: for key := range m