From 159a50fcf0f2a35653eb1ad94fb10a4ce56d7fc9 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Fri, 12 Jul 2024 10:33:34 +0300 Subject: [PATCH] [#236] netmap: Reduce allocations in getSelection() 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 │ new │ alloc │ │ sec/op │ sec/op vs base │ Netmap_ContainerNodes/REP_2-8 9.227µ ± 13% 8.677µ ± 6% ~ (p=0.165 n=10) Netmap_ContainerNodes/REP_2_IN_X_CBF_2_SELECT_2_FROM_*_AS_X-8 9.189µ ± 7% 7.946µ ± 14% -13.53% (p=0.001 n=10) geomean 9.208µ 8.303µ -9.82% │ new │ alloc │ │ B/op │ B/op vs base │ Netmap_ContainerNodes/REP_2-8 8.320Ki ± 0% 7.734Ki ± 0% -7.04% (p=0.000 n=10) Netmap_ContainerNodes/REP_2_IN_X_CBF_2_SELECT_2_FROM_*_AS_X-8 7.742Ki ± 0% 7.156Ki ± 0% -7.57% (p=0.000 n=10) geomean 8.026Ki 7.440Ki -7.31% │ new │ alloc │ │ allocs/op │ allocs/op vs base │ Netmap_ContainerNodes/REP_2-8 122.00 ± 0% 92.00 ± 0% -24.59% (p=0.000 n=10) Netmap_ContainerNodes/REP_2_IN_X_CBF_2_SELECT_2_FROM_*_AS_X-8 122.00 ± 0% 92.00 ± 0% -24.59% (p=0.000 n=10) geomean 122.0 92.00 -24.59% ``` Signed-off-by: Evgenii Stratonikov --- netmap/aggregator.go | 11 +++++------ netmap/selector.go | 4 +++- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/netmap/aggregator.go b/netmap/aggregator.go index 433a3ba..6926b84 100644 --- a/netmap/aggregator.go +++ b/netmap/aggregator.go @@ -72,12 +72,6 @@ func newMinAgg() aggregator { return new(minAgg) } -// newMeanIQRAgg returns an aggregator which -// computes mean value of values from IQR interval. -func newMeanIQRAgg() aggregator { - return new(meanIQRAgg) -} - // newReverseMinNorm returns a normalizer which // normalize values in range of 0.0 to 1.0 to a minimum value. func newReverseMinNorm(min float64) normalizer { @@ -116,6 +110,11 @@ func (a *minAgg) Compute() float64 { return a.min } +func (a *meanIQRAgg) clear() { + a.k = 0 + a.arr = a.arr[:0] +} + func (a *meanIQRAgg) Add(n float64) { a.arr = append(a.arr, n) } diff --git a/netmap/selector.go b/netmap/selector.go index d57a89d..b16e774 100644 --- a/netmap/selector.go +++ b/netmap/selector.go @@ -104,8 +104,10 @@ func (c *context) getSelection(s netmap.Selector) ([]nodes, error) { if len(c.hrwSeed) != 0 { weights := make([]float64, len(res)) + a := new(meanIQRAgg) for i := range res { - weights[i] = calcBucketWeight(res[i], newMeanIQRAgg(), c.weightFunc) + a.clear() + weights[i] = calcBucketWeight(res[i], a, c.weightFunc) } hrw.SortHasherSliceByWeightValue(res, weights, c.hrwSeedHash)