Cheaper byte to string conversion (#602)

Revamped https://github.com/anacrolix/torrent/pull/587
This commit is contained in:
YenForYang 2021-09-13 19:41:04 -05:00 committed by GitHub
parent 5332d3e9d4
commit 2203b3bcdf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 1 additions and 9 deletions

View File

@ -18,13 +18,5 @@ var unmarshalerType = reflect.TypeOf(func() *Unmarshaler {
}()).Elem() }()).Elem()
func bytesAsString(b []byte) string { func bytesAsString(b []byte) string {
if len(b) == 0 { return *(*string)(unsafe.Pointer(&b))
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
} }