Skip to content

Commit

Permalink
Update util.go
Browse files Browse the repository at this point in the history
Solve the problem of function parameters being recycled by GC
  • Loading branch information
siyuebin11 authored Sep 13, 2024
1 parent 452bfcb commit bd045ba
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@ func BytesToStringWithNoCopy(bytes []byte) string {
}

func StringToBytesWithNoCopy(s string) []byte {
x := (*[2]uintptr)(unsafe.Pointer(&s))
h := [3]uintptr{x[0], x[1], x[1]}
return *(*[]byte)(unsafe.Pointer(&h))
return *(*[]byte)(unsafe.Pointer(
&struct {
string
Cap int
}{s, len(s)},
))
}

func ListToMap(list []string) map[string]struct{} {
Expand Down

0 comments on commit bd045ba

Please sign in to comment.