xxhash_unsafe.go 906 B

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