From cf9a7e5b89147e345d4e66e5f0c60ab0e290eeb9 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Wed, 30 Jan 2019 12:32:34 +0300 Subject: [PATCH] Removed redundant m64 variable and tweaked uniform distribution test. (#1) --- hrw.go | 8 ++++---- hrw_test.go | 9 +++++---- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/hrw.go b/hrw.go index 550b8b5..786b0a7 100644 --- a/hrw.go +++ b/hrw.go @@ -22,14 +22,14 @@ type ( } ) -const m64 = 18446744073709551615 // modulus (2**64-1) - func weight(x uint64, y uint64) uint64 { acc := x ^ y + // here used mmh3 64 bit finalizer + // https://github.com/aappleby/smhasher/blob/61a0530f28277f2e850bfc39600ce61d02b518de/src/MurmurHash3.cpp#L81 acc ^= acc >> 33 - acc = (acc * 0xff51afd7ed558ccd) % m64 + acc = acc * 0xff51afd7ed558ccd acc ^= acc >> 33 - acc = (acc * 0xc4ceb9fe1a85ec53) % m64 + acc = acc * 0xc4ceb9fe1a85ec53 acc ^= acc >> 33 return acc } diff --git a/hrw_test.go b/hrw_test.go index 48c08e4..2da9cf1 100644 --- a/hrw_test.go +++ b/hrw_test.go @@ -4,6 +4,7 @@ import ( "encoding/binary" "fmt" "hash/fnv" + "math" "reflect" "strconv" "testing" @@ -54,14 +55,14 @@ func (h hashString) Hash() uint64 { hs := fnv.New64() // error always nil _, _ = hs.Write([]byte(h)) - return (hs.Sum64() >> 1) % m64 + return hs.Sum64() >> 1 } func hash(key []byte) uint64 { h := fnv.New64() // error always nil _, _ = h.Write(key) - return (h.Sum64() >> 1) ^ m64 + return (h.Sum64() >> 1) ^ math.MaxUint64 } func mur3hash(key []byte) uint64 { @@ -184,7 +185,7 @@ func TestSortByWeight(t *testing.T) { func TestUniformDistribution(t *testing.T) { var ( i uint64 - size = uint64(4) + size = uint64(10) nodes = make([]uint64, 0, size) counts = make(map[uint64]uint64) key = make([]byte, 16) @@ -202,7 +203,7 @@ func TestUniformDistribution(t *testing.T) { } mean := float64(keys) / float64(len(nodes)) - delta := mean * 0.002 // 0.2% + delta := mean * 0.01 // 1 % for node, count := range counts { d := mean - float64(count) if d > delta || (0-d) > delta {