From 74b060523256d0cb4696cf27a839c29d6edf707e Mon Sep 17 00:00:00 2001 From: helen Date: Thu, 29 Feb 2024 23:10:10 +0800 Subject: [PATCH] Optimize TouUnsafeBytes to make it leaner, more standards-compliant and (#5880) slightly faster. --- lib/bytesutil/bytesutil.go | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/lib/bytesutil/bytesutil.go b/lib/bytesutil/bytesutil.go index daa01232d5..67c67636b3 100644 --- a/lib/bytesutil/bytesutil.go +++ b/lib/bytesutil/bytesutil.go @@ -2,7 +2,6 @@ package bytesutil import ( "math/bits" - "reflect" "unsafe" ) @@ -72,10 +71,5 @@ func ToUnsafeString(b []byte) string { // // The returned byte slice is valid only until s is reachable and unmodified. func ToUnsafeBytes(s string) (b []byte) { - sh := (*reflect.StringHeader)(unsafe.Pointer(&s)) - slh := (*reflect.SliceHeader)(unsafe.Pointer(&b)) - slh.Data = sh.Data - slh.Len = sh.Len - slh.Cap = sh.Len - return b + return unsafe.Slice(unsafe.StringData(s), len(s)) }