khenaidoo | b6238b3 | 2020-04-07 12:07:36 -0400 | [diff] [blame] | 1 | // +build !appengine |
| 2 | |
| 3 | // This file encapsulates usage of unsafe. |
| 4 | // xxhash_safe.go contains the safe implementations. |
| 5 | |
| 6 | package xxhash |
| 7 | |
| 8 | import ( |
| 9 | "reflect" |
| 10 | "unsafe" |
| 11 | ) |
| 12 | |
| 13 | // Sum64String computes the 64-bit xxHash digest of s. |
| 14 | // It may be faster than Sum64([]byte(s)) by avoiding a copy. |
| 15 | // |
| 16 | // TODO(caleb): Consider removing this if an optimization is ever added to make |
| 17 | // it unnecessary: https://golang.org/issue/2205. |
| 18 | // |
| 19 | // TODO(caleb): We still have a function call; we could instead write Go/asm |
| 20 | // copies of Sum64 for strings to squeeze out a bit more speed. |
| 21 | func Sum64String(s string) uint64 { |
| 22 | // See https://groups.google.com/d/msg/golang-nuts/dcjzJy-bSpw/tcZYBzQqAQAJ |
| 23 | // for some discussion about this unsafe conversion. |
| 24 | var b []byte |
| 25 | bh := (*reflect.SliceHeader)(unsafe.Pointer(&b)) |
| 26 | bh.Data = (*reflect.StringHeader)(unsafe.Pointer(&s)).Data |
| 27 | bh.Len = len(s) |
| 28 | bh.Cap = len(s) |
| 29 | return Sum64(b) |
| 30 | } |