Skip to content

Commit 90fc78b

Browse files
committed
Higher performance UnsafeStringToSlice
Higher performance UnsafeStringToSlice
1 parent b0bf57d commit 90fc78b

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

nocopy.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -276,13 +276,13 @@ func unsafeSliceToString(b []byte) string {
276276
}
277277

278278
// zero-copy slice convert to string
279-
func unsafeStringToSlice(s string) (b []byte) {
280-
p := unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&s)).Data)
281-
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&b))
282-
hdr.Data = uintptr(p)
283-
hdr.Cap = len(s)
284-
hdr.Len = len(s)
285-
return b
279+
func unsafeStringToSlice(s string) []byte {
280+
return *(*[]byte)(unsafe.Pointer(
281+
&struct {
282+
string
283+
int
284+
}{s, len(s)},
285+
))
286286
}
287287

288288
// malloc limits the cap of the buffer from mcache.

0 commit comments

Comments
 (0)