diff --git a/nocopy.go b/nocopy.go index ad74634a..1afd7e7f 100644 --- a/nocopy.go +++ b/nocopy.go @@ -16,7 +16,6 @@ package netpoll import ( "io" - "reflect" "unsafe" "github.com/bytedance/gopkg/lang/dirtmake" @@ -276,13 +275,13 @@ func unsafeSliceToString(b []byte) string { } // zero-copy slice convert to string -func unsafeStringToSlice(s string) (b []byte) { - p := unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&s)).Data) - hdr := (*reflect.SliceHeader)(unsafe.Pointer(&b)) - hdr.Data = uintptr(p) - hdr.Cap = len(s) - hdr.Len = len(s) - return b +func unsafeStringToSlice(s string) []byte { + return *(*[]byte)(unsafe.Pointer( + &struct { + string + int + }{s, len(s)}, + )) } // malloc limits the cap of the buffer from mcache.