From 90fc78b7ec30391ea8398f9f3d89f88c670a9474 Mon Sep 17 00:00:00 2001 From: baycore <45189032+dxasu@users.noreply.github.com> Date: Fri, 22 Nov 2024 18:45:55 +0800 Subject: [PATCH 1/2] Higher performance UnsafeStringToSlice Higher performance UnsafeStringToSlice --- nocopy.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/nocopy.go b/nocopy.go index ad74634a..ef8e530d 100644 --- a/nocopy.go +++ b/nocopy.go @@ -276,13 +276,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. From c2eef10c68e5f6c3d407285eb0e4dc3672ca4852 Mon Sep 17 00:00:00 2001 From: baycore <45189032+dxasu@users.noreply.github.com> Date: Mon, 25 Nov 2024 11:10:08 +0800 Subject: [PATCH 2/2] remove unused import reflect remove unused import reflect --- nocopy.go | 1 - 1 file changed, 1 deletion(-) diff --git a/nocopy.go b/nocopy.go index ef8e530d..1afd7e7f 100644 --- a/nocopy.go +++ b/nocopy.go @@ -16,7 +16,6 @@ package netpoll import ( "io" - "reflect" "unsafe" "github.com/bytedance/gopkg/lang/dirtmake"