/
hash.go
73 lines (64 loc) · 1.66 KB
/
hash.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package mcache
import (
"unsafe"
"github.com/cespare/xxhash/v2"
)
//go:noescape
//go:linkname memhash runtime.memhash
func memhash(p unsafe.Pointer, h, s uintptr) uintptr
func KeyToHash(key interface{}) uint64 {
if key == nil {
return 0
}
switch k := key.(type) {
case uint64:
return k
case string:
return MemHashString(k)
case []byte:
return MemHash(k)
case byte:
return uint64(k)
case int:
return uint64(k)
case int32:
return uint64(k)
case uint32:
return uint64(k)
case int64:
return uint64(k)
default:
panic("Key type not supported")
}
}
type stringStruct struct {
str unsafe.Pointer
len int
}
// MemHash is the hash function used by go map, it utilizes available hardware instructions(behaves
// as aeshash if aes instruction is available).
// NOTE: The hash seed changes for every process. So, this cannot be used as a persistent hash.
func MemHash(data []byte) uint64 {
ss := (*stringStruct)(unsafe.Pointer(&data))
return uint64(memhash(ss.str, 0, uintptr(ss.len)))
}
// MemHashString is the hash function used by go map, it utilizes available hardware instructions
// (behaves as aeshash if aes instruction is available).
// NOTE: The hash seed changes for every process. So, this cannot be used as a persistent hash.
func MemHashString(str string) uint64 {
ss := (*stringStruct)(unsafe.Pointer(&str))
return uint64(memhash(ss.str, 0, uintptr(ss.len)))
}
func XXHashString(str string) uint64 {
return xxhash.Sum64String(str)
}
func Fnv32(key []byte) uint32 {
hash := uint32(2166136261)
const prime32 = uint32(16777619)
keyLength := len(key)
for i := 0; i < keyLength; i++ {
hash *= prime32
hash ^= uint32(key[i])
}
return hash
}