Skip to content
This repository was archived by the owner on Feb 1, 2019. It is now read-only.

Commit cf9a7e5

Browse files
alexvaninim-kulikov
authored andcommitted
Removed redundant m64 variable and tweaked uniform distribution test. (#1)
1 parent 9b6321f commit cf9a7e5

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

hrw.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ type (
2222
}
2323
)
2424

25-
const m64 = 18446744073709551615 // modulus (2**64-1)
26-
2725
func weight(x uint64, y uint64) uint64 {
2826
acc := x ^ y
27+
// here used mmh3 64 bit finalizer
28+
// https://github.com/aappleby/smhasher/blob/61a0530f28277f2e850bfc39600ce61d02b518de/src/MurmurHash3.cpp#L81
2929
acc ^= acc >> 33
30-
acc = (acc * 0xff51afd7ed558ccd) % m64
30+
acc = acc * 0xff51afd7ed558ccd
3131
acc ^= acc >> 33
32-
acc = (acc * 0xc4ceb9fe1a85ec53) % m64
32+
acc = acc * 0xc4ceb9fe1a85ec53
3333
acc ^= acc >> 33
3434
return acc
3535
}

hrw_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/binary"
55
"fmt"
66
"hash/fnv"
7+
"math"
78
"reflect"
89
"strconv"
910
"testing"
@@ -54,14 +55,14 @@ func (h hashString) Hash() uint64 {
5455
hs := fnv.New64()
5556
// error always nil
5657
_, _ = hs.Write([]byte(h))
57-
return (hs.Sum64() >> 1) % m64
58+
return hs.Sum64() >> 1
5859
}
5960

6061
func hash(key []byte) uint64 {
6162
h := fnv.New64()
6263
// error always nil
6364
_, _ = h.Write(key)
64-
return (h.Sum64() >> 1) ^ m64
65+
return (h.Sum64() >> 1) ^ math.MaxUint64
6566
}
6667

6768
func mur3hash(key []byte) uint64 {
@@ -184,7 +185,7 @@ func TestSortByWeight(t *testing.T) {
184185
func TestUniformDistribution(t *testing.T) {
185186
var (
186187
i uint64
187-
size = uint64(4)
188+
size = uint64(10)
188189
nodes = make([]uint64, 0, size)
189190
counts = make(map[uint64]uint64)
190191
key = make([]byte, 16)
@@ -202,7 +203,7 @@ func TestUniformDistribution(t *testing.T) {
202203
}
203204

204205
mean := float64(keys) / float64(len(nodes))
205-
delta := mean * 0.002 // 0.2%
206+
delta := mean * 0.01 // 1 %
206207
for node, count := range counts {
207208
d := mean - float64(count)
208209
if d > delta || (0-d) > delta {

0 commit comments

Comments
 (0)