From 24e12c5b212533693fb1e68644721b6ff8b5801c Mon Sep 17 00:00:00 2001 From: gediminasgu Date: Wed, 16 Oct 2019 18:33:24 +0300 Subject: [PATCH] Added overload for HLL++ to Add uint64 in place of Hash It helps when using fash hash libraries like https://github.com/segmentio/fasthash/ as using hash does two allocations for every operation. --- bench_test.go | 2 +- hyperloglogplus.go | 12 ++++++++---- hyperloglogplus_test.go | 2 +- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/bench_test.go b/bench_test.go index d3dd1ff..d4affcf 100644 --- a/bench_test.go +++ b/bench_test.go @@ -23,7 +23,7 @@ func hash64(s string) hash.Hash64 { func randStr(n int) string { i := rand.Uint32() - return fmt.Sprintf("a%s %s", i, n) + return fmt.Sprintf("a%d %d", i, n) } func benchmark(precision uint8, n int) { diff --git a/hyperloglogplus.go b/hyperloglogplus.go index d8d8337..3f0b198 100644 --- a/hyperloglogplus.go +++ b/hyperloglogplus.go @@ -145,13 +145,17 @@ func (h *HyperLogLogPlus) toNormal() { // Add adds a new item to HyperLogLogPlus h. func (h *HyperLogLogPlus) Add(item Hash64) { - x := item.Sum64() + h.AddUInt64(item.Sum64()) +} + +// Add adds a new item to HyperLogLogPlus h. +func (h *HyperLogLogPlus) AddUInt64(item uint64) { if h.sparse { - h.tmpSet.Add(h.encodeHash(x)) + h.tmpSet.Add(h.encodeHash(item)) h.maybeMerge() } else { - i := eb64(x, 64, 64-h.p) // {x63,...,x64-p} - w := x< h.reg[i] { diff --git a/hyperloglogplus_test.go b/hyperloglogplus_test.go index d350824..8355267 100644 --- a/hyperloglogplus_test.go +++ b/hyperloglogplus_test.go @@ -53,7 +53,7 @@ func TestHLLPPAddNoSparse(t *testing.T) { t.Error(n) } - h.Add(fakeHash64(0xff03080000000000)) + h.AddUInt64(fakeHash64(0xff03080000000000).Sum64()) n = h.reg[0xff03] if n != 5 { t.Error(n)