From 827954ee7b13446bd67daeb8f4edf71a87f8e4df Mon Sep 17 00:00:00 2001 From: YenForYang Date: Mon, 13 Sep 2021 15:56:49 -0500 Subject: [PATCH] Cheaper byte to string conversion Revamped https://github.com/anacrolix/torrent/pull/587 --- bencode/misc.go | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/bencode/misc.go b/bencode/misc.go index 3b95afcecc..38b7fce84f 100644 --- a/bencode/misc.go +++ b/bencode/misc.go @@ -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)) }