From 0f2353997a7ab949b486e232b69e32a0eab12d68 Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Tue, 22 Oct 2024 14:56:17 +0700 Subject: [PATCH] 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 LUCI-TryBot-Result: Go LUCI Reviewed-by: Keith Randall Reviewed-by: Michael Pratt --- src/hash/maphash/maphash_purego.go | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/hash/maphash/maphash_purego.go b/src/hash/maphash/maphash_purego.go index be7ac52f23..8c47ac5d5e 100644 --- a/src/hash/maphash/maphash_purego.go +++ b/src/hash/maphash/maphash_purego.go @@ -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 {