Skip to content

Commit

Permalink
feat: take advantage of unsafe.Slice() since go v1.17+
Browse files Browse the repository at this point in the history
  • Loading branch information
Gusted committed Aug 26, 2021
1 parent 2937dd4 commit 5f44ea0
Showing 1 changed file with 1 addition and 6 deletions.
7 changes: 1 addition & 6 deletions utils/fast.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@ func UnsafeString(b []byte) string {

// UnsafeBytes returns a byte pointer without allocation.
func UnsafeBytes(s string) (bs []byte) {
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
bh := (*reflect.SliceHeader)(unsafe.Pointer(&bs))
bh.Data = sh.Data
bh.Len = sh.Len
bh.Cap = sh.Len
return bs
return unsafe.Slice((*byte)(unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&s)).Data)), len(s))
}

func EncodeToString(src []byte) string {
Expand Down

0 comments on commit 5f44ea0

Please sign in to comment.