Skip to content

Commit 0f23539

Browse files
committed
hash/maphash: sync wyhash with runtime implementation
Fixes #69940 Change-Id: I40535d2647f9456d2196241bf7414b1e92b53c2c Reviewed-on: https://go-review.googlesource.com/c/go/+/621756 Reviewed-by: Keith Randall <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Michael Pratt <[email protected]>
1 parent cb69354 commit 0f23539

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

src/hash/maphash/maphash_purego.go

+15-13
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ import (
1313
"reflect"
1414
)
1515

16+
var hashkey [4]uint64
17+
18+
func init() {
19+
for i := range hashkey {
20+
hashkey[i] = randUint64()
21+
}
22+
}
23+
1624
func rthash(buf []byte, seed uint64) uint64 {
1725
if len(buf) == 0 {
1826
return seed
@@ -33,34 +41,28 @@ func randUint64() uint64 {
3341
// This is a port of wyhash implementation in runtime/hash64.go,
3442
// without using unsafe for purego.
3543

36-
const (
37-
m1 = 0xa0761d6478bd642f
38-
m2 = 0xe7037ed1a0b428db
39-
m3 = 0x8ebc6af09c88c6e3
40-
m4 = 0x589965cc75374cc3
41-
m5 = 0x1d8e4e27c47d124f
42-
)
44+
const m5 = 0x1d8e4e27c47d124f
4345

4446
func wyhash(key []byte, seed, len uint64) uint64 {
4547
p := key
4648
i := len
4749
var a, b uint64
48-
seed ^= m1
50+
seed ^= hashkey[0]
4951

5052
if i > 16 {
5153
if i > 48 {
5254
seed1 := seed
5355
seed2 := seed
5456
for ; i > 48; i -= 48 {
55-
seed = mix(r8(p)^m2, r8(p[8:])^seed)
56-
seed1 = mix(r8(p[16:])^m3, r8(p[24:])^seed1)
57-
seed2 = mix(r8(p[32:])^m4, r8(p[40:])^seed2)
57+
seed = mix(r8(p)^hashkey[1], r8(p[8:])^seed)
58+
seed1 = mix(r8(p[16:])^hashkey[2], r8(p[24:])^seed1)
59+
seed2 = mix(r8(p[32:])^hashkey[3], r8(p[40:])^seed2)
5860
p = p[48:]
5961
}
6062
seed ^= seed1 ^ seed2
6163
}
6264
for ; i > 16; i -= 16 {
63-
seed = mix(r8(p)^m2, r8(p[8:])^seed)
65+
seed = mix(r8(p)^hashkey[1], r8(p[8:])^seed)
6466
p = p[16:]
6567
}
6668
}
@@ -74,7 +76,7 @@ func wyhash(key []byte, seed, len uint64) uint64 {
7476
a = r4(p)<<32 | r4(p[n:])
7577
b = r4(p[i-4:])<<32 | r4(p[i-4-n:])
7678
}
77-
return mix(m5^len, mix(a^m2, b^seed))
79+
return mix(m5^len, mix(a^hashkey[1], b^seed))
7880
}
7981

8082
func r3(p []byte, k uint64) uint64 {

0 commit comments

Comments
 (0)