From: YenForYang Date: Tue, 14 Sep 2021 00:41:04 +0000 (-0500) Subject: Cheaper byte to string conversion (#602) X-Git-Tag: v1.32.0~65 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=2203b3bcdf516347dfa66057feac7df5dde034db;p=btrtrc.git Cheaper byte to string conversion (#602) Revamped https://github.com/anacrolix/torrent/pull/587 --- diff --git a/bencode/misc.go b/bencode/misc.go index 3b95afce..38b7fce8 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)) }