From 1509fa2a15193de55be7e3e237e3e84cf20ac873 Mon Sep 17 00:00:00 2001 From: Jia-Jun Yeh Date: Fri, 4 Oct 2019 11:42:41 +0800 Subject: [PATCH 1/3] Fix generateMultiHash write data to source data slice If we pass slice instead of array and its capacity is safe to write, function append writes data to backing array. --- hash.go | 2 +- hash_test.go | 38 ++++++++++++++++++++++++++++++++++++++ ring_test.go | 6 ++++-- 3 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 hash_test.go diff --git a/hash.go b/hash.go index 0649c5a001..86a555fe6e 100644 --- a/hash.go +++ b/hash.go @@ -139,7 +139,7 @@ func bytesToUint64(b []byte) uint64 { // generateMultihash returns 4 64-bit (2 x 128-bit) MurmurHash3 hashes. func generateMultiHash(data []byte) [4]uint64 { h1, h2 := murmur128(data) - h3, h4 := murmur128(append(data, single)) + h3, h4 := murmur128(append([]byte{single}, data...)) return [4]uint64{h1, h2, h3, h4} } diff --git a/hash_test.go b/hash_test.go new file mode 100644 index 0000000000..59cc6267ee --- /dev/null +++ b/hash_test.go @@ -0,0 +1,38 @@ +package ring + +import "testing" + +func BenchmarkGenerateMultiHash(b *testing.B) { + b.ReportAllocs() + data := []byte{0x00, 0x12, 0x34, 0x56, 0x78, 0x00} + buff := make([]byte, len(data)) + for i := 0; i < b.N; i++ { + copy(buff, data) + generateMultiHash(buff[1:5]) + } +} + +func TestGenerateMultiHash(t *testing.T) { + data := []byte{ + 0x00, 0x12, 0x34, 0x56, 0x78, 0x00, + 0x00, 0x12, 0x34, 0x56, 0x78, 0x00, + 0x00, 0x12, 0x34, 0x56, 0x78, 0x00, + 0x00, 0x12, 0x34, 0x56, 0x78, 0x00, + 0x00, 0x12, 0x34, 0x56, 0x78, 0x00, + 0x00, 0x12, 0x34, 0x56, 0x78, 0x00, + 0x00, 0x12, 0x34, 0x56, 0x78, 0x00, + 0x00, 0x12, 0x34, 0x56, 0x78, 0x00, + 0x00, 0x12, 0x34, 0x56, 0x78, 0x00, + 0x00, 0x12, 0x34, 0x56, 0x78, 0x00, + 0x00, 0x12, 0x34, 0x56, 0x78, 0x00, + } + buff := make([]byte, len(data)) + copy(buff, data) + generateMultiHash(buff[1:20]) + + for i := range data { + if data[i] != buff[i] { + t.Fatalf("data not match at index: %v", i) + } + } +} diff --git a/ring_test.go b/ring_test.go index fcab06342d..54f30d5eec 100644 --- a/ring_test.go +++ b/ring_test.go @@ -64,19 +64,21 @@ func TestMain(m *testing.M) { // BenchmarkAdd tests adding elements to a Ring. func BenchmarkAdd(b *testing.B) { + b.ReportAllocs() buff := make([]byte, 4) for i := 0; i < b.N; i++ { intToByte(buff, i) - rBench.Add(buff) + rBench.Add(buff[:3]) } } // BenchmarkTest tests elements in a Ring. func BenchmarkTest(b *testing.B) { + b.ReportAllocs() buff := make([]byte, 4) for i := 0; i < b.N; i++ { intToByte(buff, i) - rBench.Test(buff) + rBench.Test(buff[:3]) } } From fe10d28a31fa09eba4000205c85ed78969194d22 Mon Sep 17 00:00:00 2001 From: Jia-Jun Yeh Date: Tue, 8 Oct 2019 11:42:57 +0800 Subject: [PATCH 2/3] Remove ReportAllocs() in benchmark --- hash_test.go | 1 - ring_test.go | 2 -- 2 files changed, 3 deletions(-) diff --git a/hash_test.go b/hash_test.go index 59cc6267ee..343a1ad3f0 100644 --- a/hash_test.go +++ b/hash_test.go @@ -3,7 +3,6 @@ package ring import "testing" func BenchmarkGenerateMultiHash(b *testing.B) { - b.ReportAllocs() data := []byte{0x00, 0x12, 0x34, 0x56, 0x78, 0x00} buff := make([]byte, len(data)) for i := 0; i < b.N; i++ { diff --git a/ring_test.go b/ring_test.go index 54f30d5eec..783e8c4278 100644 --- a/ring_test.go +++ b/ring_test.go @@ -64,7 +64,6 @@ func TestMain(m *testing.M) { // BenchmarkAdd tests adding elements to a Ring. func BenchmarkAdd(b *testing.B) { - b.ReportAllocs() buff := make([]byte, 4) for i := 0; i < b.N; i++ { intToByte(buff, i) @@ -74,7 +73,6 @@ func BenchmarkAdd(b *testing.B) { // BenchmarkTest tests elements in a Ring. func BenchmarkTest(b *testing.B) { - b.ReportAllocs() buff := make([]byte, 4) for i := 0; i < b.N; i++ { intToByte(buff, i) From a46c755aca04163664fec43c19298f6e21790716 Mon Sep 17 00:00:00 2001 From: Jia-Jun Yeh Date: Tue, 8 Oct 2019 12:02:37 +0800 Subject: [PATCH 3/3] Keep backward compatibility --- hash.go | 5 ++++- ring_test.go | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/hash.go b/hash.go index 86a555fe6e..4a60b5f6cd 100644 --- a/hash.go +++ b/hash.go @@ -139,7 +139,10 @@ func bytesToUint64(b []byte) uint64 { // generateMultihash returns 4 64-bit (2 x 128-bit) MurmurHash3 hashes. func generateMultiHash(data []byte) [4]uint64 { h1, h2 := murmur128(data) - h3, h4 := murmur128(append([]byte{single}, data...)) + buff := make([]byte, len(data)+1) + copy(buff, data) + buff[len(data)] = single + h3, h4 := murmur128(buff) return [4]uint64{h1, h2, h3, h4} } diff --git a/ring_test.go b/ring_test.go index 783e8c4278..fcab06342d 100644 --- a/ring_test.go +++ b/ring_test.go @@ -67,7 +67,7 @@ func BenchmarkAdd(b *testing.B) { buff := make([]byte, 4) for i := 0; i < b.N; i++ { intToByte(buff, i) - rBench.Add(buff[:3]) + rBench.Add(buff) } } @@ -76,7 +76,7 @@ func BenchmarkTest(b *testing.B) { buff := make([]byte, 4) for i := 0; i < b.N; i++ { intToByte(buff, i) - rBench.Test(buff[:3]) + rBench.Test(buff) } }