Skip to content

Commit

Permalink
Update: faster hash
Browse files Browse the repository at this point in the history
  • Loading branch information
fufuok committed Oct 12, 2021
1 parent 3d22c08 commit d92a4bb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
20 changes: 10 additions & 10 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ func BenchmarkRandomR(b *testing.B) {
// goarch: amd64
// pkg: github.com/fufuok/balancer
// cpu: Intel(R) Xeon(R) CPU E5-2667 v2 @ 3.30GHz
// BenchmarkWeightedRoundRobin-4 30918976 38.35 ns/op 0 B/op 0 allocs/op
// BenchmarkWeightedRoundRobin-4 30627727 38.33 ns/op 0 B/op 0 allocs/op
// BenchmarkSmoothWeightedRoundRobin-4 20629270 59.60 ns/op 0 B/op 0 allocs/op
// BenchmarkSmoothWeightedRoundRobin-4 21024522 75.05 ns/op 0 B/op 0 allocs/op
// BenchmarkConsistentHash-4 26980348 43.72 ns/op 0 B/op 0 allocs/op
// BenchmarkConsistentHash-4 26978271 43.77 ns/op 0 B/op 0 allocs/op
// BenchmarkRoundRobin-4 51317532 23.18 ns/op 0 B/op 0 allocs/op
// BenchmarkRoundRobin-4 51717782 29.64 ns/op 0 B/op 0 allocs/op
// BenchmarkRandomR-4 47053610 32.63 ns/op 0 B/op 0 allocs/op
// BenchmarkRandomR-4 46210752 25.15 ns/op 0 B/op 0 allocs/op
// BenchmarkWeightedRoundRobin-4 30947391 37.95 ns/op 0 B/op 0 allocs/op
// BenchmarkWeightedRoundRobin-4 30508263 48.99 ns/op 0 B/op 0 allocs/op
// BenchmarkSmoothWeightedRoundRobin-4 20379349 60.12 ns/op 0 B/op 0 allocs/op
// BenchmarkSmoothWeightedRoundRobin-4 20407590 75.66 ns/op 0 B/op 0 allocs/op
// BenchmarkConsistentHash-4 33911062 44.45 ns/op 0 B/op 0 allocs/op
// BenchmarkConsistentHash-4 34818607 44.34 ns/op 0 B/op 0 allocs/op
// BenchmarkRoundRobin-4 51605906 23.35 ns/op 0 B/op 0 allocs/op
// BenchmarkRoundRobin-4 50344368 29.87 ns/op 0 B/op 0 allocs/op
// BenchmarkRandomR-4 45422324 26.06 ns/op 0 B/op 0 allocs/op
// BenchmarkRandomR-4 46060688 32.93 ns/op 0 B/op 0 allocs/op
16 changes: 12 additions & 4 deletions hash.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package balancer

import (
"hash/fnv"
"sync"

"github.com/fufuok/balancer/internal/utils"
Expand All @@ -18,6 +17,12 @@ type consistentHash struct {
sync.RWMutex
}

const (
// FNVa offset basis. See https://en.wikipedia.org/wiki/Fowler–Noll–Vo_hash_function#FNV-1a_hash
offset64 = 14695981039346656037
prime64 = 1099511628211
)

func NewConsistentHash(items ...[]string) (lb *consistentHash) {
if len(items) > 0 && len(items[0]) > 0 {
lb = &consistentHash{}
Expand Down Expand Up @@ -71,9 +76,12 @@ func (b *consistentHash) chooseNext(key []string) (choice string) {
}

func (b *consistentHash) hash(s string) uint64 {
h := fnv.New64a()
_, _ = h.Write([]byte(s))
return h.Sum64()
var h uint64 = offset64
for i := 0; i < len(s); i++ {
h ^= uint64(s[i])
h *= prime64
}
return h
}

func (b *consistentHash) Remove(item string, asClean ...bool) (ok bool) {
Expand Down

0 comments on commit d92a4bb

Please sign in to comment.