Cheaper byte to string conversion (#602)
Revamped https://github.com/anacrolix/torrent/pull/587
This commit is contained in:
parent
5332d3e9d4
commit
2203b3bcdf
|
@ -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
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue