]> Cypherpunks repositories - gostls13.git/commitdiff
hash/maphash: sync wyhash with runtime implementation
authorCuong Manh Le <cuong.manhle.vn@gmail.com>
Tue, 22 Oct 2024 07:56:17 +0000 (14:56 +0700)
committerCuong Manh Le <cuong.manhle.vn@gmail.com>
Wed, 23 Oct 2024 16:06:43 +0000 (16:06 +0000)
Fixes #69940

Change-Id: I40535d2647f9456d2196241bf7414b1e92b53c2c
Reviewed-on: https://go-review.googlesource.com/c/go/+/621756
Reviewed-by: Keith Randall <khr@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Michael Pratt <mpratt@google.com>
src/hash/maphash/maphash_purego.go

index be7ac52f2335c386b51f86f4eb2c7f2f26f76692..8c47ac5d5e24989489058b9a889a38024fcae02b 100644 (file)
@@ -13,6 +13,14 @@ import (
        "reflect"
 )
 
+var hashkey [4]uint64
+
+func init() {
+       for i := range hashkey {
+               hashkey[i] = randUint64()
+       }
+}
+
 func rthash(buf []byte, seed uint64) uint64 {
        if len(buf) == 0 {
                return seed
@@ -33,34 +41,28 @@ func randUint64() uint64 {
 // This is a port of wyhash implementation in runtime/hash64.go,
 // without using unsafe for purego.
 
-const (
-       m1 = 0xa0761d6478bd642f
-       m2 = 0xe7037ed1a0b428db
-       m3 = 0x8ebc6af09c88c6e3
-       m4 = 0x589965cc75374cc3
-       m5 = 0x1d8e4e27c47d124f
-)
+const m5 = 0x1d8e4e27c47d124f
 
 func wyhash(key []byte, seed, len uint64) uint64 {
        p := key
        i := len
        var a, b uint64
-       seed ^= m1
+       seed ^= hashkey[0]
 
        if i > 16 {
                if i > 48 {
                        seed1 := seed
                        seed2 := seed
                        for ; i > 48; i -= 48 {
-                               seed = mix(r8(p)^m2, r8(p[8:])^seed)
-                               seed1 = mix(r8(p[16:])^m3, r8(p[24:])^seed1)
-                               seed2 = mix(r8(p[32:])^m4, r8(p[40:])^seed2)
+                               seed = mix(r8(p)^hashkey[1], r8(p[8:])^seed)
+                               seed1 = mix(r8(p[16:])^hashkey[2], r8(p[24:])^seed1)
+                               seed2 = mix(r8(p[32:])^hashkey[3], r8(p[40:])^seed2)
                                p = p[48:]
                        }
                        seed ^= seed1 ^ seed2
                }
                for ; i > 16; i -= 16 {
-                       seed = mix(r8(p)^m2, r8(p[8:])^seed)
+                       seed = mix(r8(p)^hashkey[1], r8(p[8:])^seed)
                        p = p[16:]
                }
        }
@@ -74,7 +76,7 @@ func wyhash(key []byte, seed, len uint64) uint64 {
                a = r4(p)<<32 | r4(p[n:])
                b = r4(p[i-4:])<<32 | r4(p[i-4-n:])
        }
-       return mix(m5^len, mix(a^m2, b^seed))
+       return mix(m5^len, mix(a^hashkey[1], b^seed))
 }
 
 func r3(p []byte, k uint64) uint64 {