Skip to content

Commit

Permalink
Cheaper byte to string conversion (#602)
Browse files Browse the repository at this point in the history
Revamped #587
  • Loading branch information
YenForYang authored Sep 14, 2021
1 parent 5332d3e commit 2203b3b
Showing 1 changed file with 1 addition and 9 deletions.
10 changes: 1 addition & 9 deletions bencode/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

0 comments on commit 2203b3b

Please sign in to comment.