From a69f00903cb109ec569c9a90662627be90c9074b Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Fri, 12 Jul 2024 10:22:35 +0300 Subject: [PATCH] [#236] netmap: Replace sort.Slice() with slices.Sort() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ``` goos: linux goarch: amd64 pkg: git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap cpu: 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz │ old │ new │ │ sec/op │ sec/op vs base │ Netmap_ContainerNodes/REP_2-8 10.395µ ± 14% 9.227µ ± 13% -11.24% (p=0.015 n=10) Netmap_ContainerNodes/REP_2_IN_X_CBF_2_SELECT_2_FROM_*_AS_X-8 10.110µ ± 16% 9.189µ ± 7% ~ (p=0.105 n=10) geomean 10.25µ 9.208µ -10.18% │ old │ new │ │ B/op │ B/op vs base │ Netmap_ContainerNodes/REP_2-8 8.695Ki ± 0% 8.320Ki ± 0% -4.31% (p=0.000 n=10) Netmap_ContainerNodes/REP_2_IN_X_CBF_2_SELECT_2_FROM_*_AS_X-8 8.117Ki ± 0% 7.742Ki ± 0% -4.62% (p=0.000 n=10) geomean 8.401Ki 8.026Ki -4.47% │ old │ new │ │ allocs/op │ allocs/op vs base │ Netmap_ContainerNodes/REP_2-8 138.0 ± 0% 122.0 ± 0% -11.59% (p=0.000 n=10) Netmap_ContainerNodes/REP_2_IN_X_CBF_2_SELECT_2_FROM_*_AS_X-8 138.0 ± 0% 122.0 ± 0% -11.59% (p=0.000 n=10) geomean 138.0 122.0 -11.59% ``` Signed-off-by: Evgenii Stratonikov --- netmap/aggregator.go | 6 ++---- netmap/node_info.go | 22 +++++----------------- netmap/selector.go | 11 ++++++----- netmap/selector_test.go | 7 ++++--- 4 files changed, 17 insertions(+), 29 deletions(-) diff --git a/netmap/aggregator.go b/netmap/aggregator.go index 0f577a0..433a3ba 100644 --- a/netmap/aggregator.go +++ b/netmap/aggregator.go @@ -1,8 +1,6 @@ package netmap -import ( - "sort" -) +import "slices" type ( // aggregator can calculate some value across all netmap @@ -128,7 +126,7 @@ func (a *meanIQRAgg) Compute() float64 { return 0 } - sort.Slice(a.arr, func(i, j int) bool { return a.arr[i] < a.arr[j] }) + slices.Sort(a.arr) var min, max float64 diff --git a/netmap/node_info.go b/netmap/node_info.go index 8f323f7..bd37388 100644 --- a/netmap/node_info.go +++ b/netmap/node_info.go @@ -3,7 +3,7 @@ package netmap import ( "errors" "fmt" - "sort" + "slices" "strconv" "strings" @@ -227,14 +227,6 @@ func (x NodeInfo) Hash() uint64 { return hrw.Hash(x.m.GetPublicKey()) } -// less declares "less than" comparison between two NodeInfo instances: -// x1 is less than x2 if it has less Hash(). -// -// Method is needed for internal placement needs. -func less(x1, x2 NodeInfo) bool { - return x1.Hash() < x2.Hash() -} - func (x *NodeInfo) setNumericAttribute(key string, num uint64) { x.SetAttribute(key, strconv.FormatUint(num, 10)) } @@ -455,15 +447,11 @@ func (x *NodeInfo) SortAttributes() { return } - sort.Slice(as, func(i, j int) bool { - switch strings.Compare(as[i].GetKey(), as[j].GetKey()) { - case -1: - return true - case 1: - return false - default: - return as[i].GetValue() < as[j].GetValue() + slices.SortFunc(as, func(ai, aj netmap.Attribute) int { + if r := strings.Compare(ai.GetKey(), aj.GetKey()); r != 0 { + return r } + return strings.Compare(ai.GetValue(), aj.GetValue()) }) x.m.SetAttributes(as) diff --git a/netmap/selector.go b/netmap/selector.go index f9247ff..d57a89d 100644 --- a/netmap/selector.go +++ b/netmap/selector.go @@ -1,8 +1,9 @@ package netmap import ( + "cmp" "fmt" - "sort" + "slices" "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/netmap" "git.frostfs.info/TrueCloudLab/hrw" @@ -70,12 +71,12 @@ func (c *context) getSelection(s netmap.Selector) ([]nodes, error) { // we also need to have deterministic input to HRW sorting routine. if len(c.hrwSeed) == 0 { if s.GetAttribute() == "" { - sort.Slice(buckets, func(i, j int) bool { - return less(buckets[i].nodes[0], buckets[j].nodes[0]) + slices.SortFunc(buckets, func(b1, b2 nodeAttrPair) int { + return cmp.Compare(b1.nodes[0].Hash(), b2.nodes[0].Hash()) }) } else { - sort.Slice(buckets, func(i, j int) bool { - return buckets[i].attr < buckets[j].attr + slices.SortFunc(buckets, func(b1, b2 nodeAttrPair) int { + return cmp.Compare(b1.attr, b2.attr) }) } } diff --git a/netmap/selector_test.go b/netmap/selector_test.go index 934ff7e..0f0dd24 100644 --- a/netmap/selector_test.go +++ b/netmap/selector_test.go @@ -1,11 +1,12 @@ package netmap import ( + "cmp" "crypto/rand" "encoding/binary" "fmt" mrand "math/rand" - "sort" + "slices" "strconv" "testing" @@ -85,8 +86,8 @@ func BenchmarkHRWSort(b *testing.B) { copy(realNodes, vectors) b.StartTimer() - sort.Slice(vectors, func(i, j int) bool { - return less(vectors[i][0], vectors[j][0]) + slices.SortFunc(vectors, func(vi, vj nodes) int { + return cmp.Compare(vi[0].Hash(), vj[0].Hash()) }) hrw.SortSliceByWeightIndex(realNodes, weights, pivot) }