From 16a7740ccd8221870c6cacfc71075c82f2201874 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 1 Jun 2023 21:00:32 +0300 Subject: [PATCH] [#8] hrw: Introduce StringHash() for hashing strings directly MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ``` goos: linux goarch: amd64 pkg: git.frostfs.info/TrueCloudLab/hrw cpu: 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz │ 6 │ 7 │ │ sec/op │ sec/op vs base │ SortHashersByValue_Typed_fnv_10-8 248.8n ± 1% 166.9n ± 9% -32.93% (p=0.000 n=10) SortHashersByValue_Typed_fnv_100-8 2.195µ ± 6% 1.297µ ± 6% -40.93% (p=0.000 n=10) SortHashersByValue_Typed_fnv_1000-8 22.47µ ± 4% 12.42µ ± 10% -44.73% (p=0.000 n=10) SortHashersByWeightValueTyped_fnv_10-8 301.7n ± 6% 180.8n ± 4% -40.09% (p=0.000 n=10) SortHashersByWeightValueTyped_fnv_100-8 2.526µ ± 1% 1.378µ ± 5% -45.47% (p=0.000 n=10) SortHashersByWeightValueTyped_fnv_1000-8 24.37µ ± 2% 12.80µ ± 4% -47.48% (p=0.000 n=10) geomean 2.472µ 1.430µ -42.13% │ 6 │ 7 │ │ B/op │ B/op vs base │ SortHashersByValue_Typed_fnv_10-8 144.0 ± 0% 144.0 ± 0% ~ (p=1.000 n=10) ¹ SortHashersByValue_Typed_fnv_100-8 960.0 ± 0% 960.0 ± 0% ~ (p=1.000 n=10) ¹ SortHashersByValue_Typed_fnv_1000-8 8.062Ki ± 0% 8.062Ki ± 0% ~ (p=1.000 n=10) ¹ SortHashersByWeightValueTyped_fnv_10-8 144.0 ± 0% 144.0 ± 0% ~ (p=1.000 n=10) ¹ SortHashersByWeightValueTyped_fnv_100-8 960.0 ± 0% 960.0 ± 0% ~ (p=1.000 n=10) ¹ SortHashersByWeightValueTyped_fnv_1000-8 8.062Ki ± 0% 8.062Ki ± 0% ~ (p=1.000 n=10) ¹ geomean 1.021Ki 1.021Ki +0.00% ¹ all samples are equal │ 6 │ 7 │ │ allocs/op │ allocs/op vs base │ SortHashersByValue_Typed_fnv_10-8 2.000 ± 0% 2.000 ± 0% ~ (p=1.000 n=10) ¹ SortHashersByValue_Typed_fnv_100-8 2.000 ± 0% 2.000 ± 0% ~ (p=1.000 n=10) ¹ SortHashersByValue_Typed_fnv_1000-8 2.000 ± 0% 2.000 ± 0% ~ (p=1.000 n=10) ¹ SortHashersByWeightValueTyped_fnv_10-8 2.000 ± 0% 2.000 ± 0% ~ (p=1.000 n=10) ¹ SortHashersByWeightValueTyped_fnv_100-8 2.000 ± 0% 2.000 ± 0% ~ (p=1.000 n=10) ¹ SortHashersByWeightValueTyped_fnv_1000-8 2.000 ± 0% 2.000 ± 0% ~ (p=1.000 n=10) ¹ geomean 2.000 2.000 +0.00% ¹ all samples are equal ``` Signed-off-by: Evgenii Stratonikov --- hrw.go | 5 +++++ hrw_test.go | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/hrw.go b/hrw.go index 9059636..1156037 100644 --- a/hrw.go +++ b/hrw.go @@ -68,6 +68,11 @@ func Hash(key []byte) uint64 { return murmur3.Sum64(key) } +// StringHash uses murmur3 hash to return uint64 +func StringHash(key string) uint64 { + return murmur3.StringSum64(key) +} + // Sort receive nodes and hash, and sort it by distance func Sort(nodes []uint64, hash uint64) []uint64 { l := len(nodes) diff --git a/hrw_test.go b/hrw_test.go index 934e335..bb22c81 100644 --- a/hrw_test.go +++ b/hrw_test.go @@ -61,7 +61,7 @@ func Example() { } func (h hashString) Hash() uint64 { - return Hash([]byte(h)) + return StringHash(string(h)) } func TestSortSliceByIndex(t *testing.T) {