]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Cheaper byte to string conversion (#602)
authorYenForYang <YenForYang@users.noreply.github.com>
Tue, 14 Sep 2021 00:41:04 +0000 (19:41 -0500)
committerGitHub <noreply@github.com>
Tue, 14 Sep 2021 00:41:04 +0000 (10:41 +1000)
Revamped https://github.com/anacrolix/torrent/pull/587

bencode/misc.go

index 3b95afcecce7dab27ad793f418ea196408204290..38b7fce84fe55fe74009184b6e5b2c815e0cc663 100644 (file)
@@ -18,13 +18,5 @@ var unmarshalerType = reflect.TypeOf(func() *Unmarshaler {
 }()).Elem()
 
 func bytesAsString(b []byte) string {
-       if len(b) == 0 {
-               return ""
-       }
-       // See https://github.com/golang/go/issues/40701.
-       var s string
-       hdr := (*reflect.StringHeader)(unsafe.Pointer(&s))
-       hdr.Data = uintptr(unsafe.Pointer(&b[0]))
-       hdr.Len = len(b)
-       return s
+       return *(*string)(unsafe.Pointer(&b))
 }