blob: d2b64e8bb00fed29bfdc3df7e48f5b30282e1b06 [file] [log] [blame]
khenaidoob6238b32020-04-07 12:07:36 -04001// +build !appengine
2
3// This file encapsulates usage of unsafe.
4// xxhash_safe.go contains the safe implementations.
5
6package xxhash
7
8import (
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.
21func 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}