diff --git a/netmap/netmap.go b/netmap/netmap.go index f0ece7d..85e2a84 100644 --- a/netmap/netmap.go +++ b/netmap/netmap.go @@ -113,9 +113,10 @@ func (n nodes) Hash() uint64 { return 0 } -// weights returns slice of nodes weights W. -func (n nodes) weights(wf weightFunc) []float64 { - w := make([]float64, 0, len(n)) +func (n nodes) appendWeightsTo(wf weightFunc, w []float64) []float64 { + if cap(w) < len(n) { + w = make([]float64, 0, len(n)) + } for i := range n { w = append(w, wf(n[i])) } @@ -149,10 +150,13 @@ func (m NetMap) PlacementVectors(vectors [][]NodeInfo, pivot []byte) ([][]NodeIn wf := defaultWeightFunc(m.nodes) result := make([][]NodeInfo, len(vectors)) + var ws []float64 + for i := range vectors { result[i] = make([]NodeInfo, len(vectors[i])) copy(result[i], vectors[i]) - hrw.SortHasherSliceByWeightValue(result[i], nodes(result[i]).weights(wf), h) + ws = nodes(result[i]).appendWeightsTo(wf, ws[:0]) + hrw.SortHasherSliceByWeightValue(result[i], ws, h) } return result, nil diff --git a/netmap/selector.go b/netmap/selector.go index b16e774..ab73cec 100644 --- a/netmap/selector.go +++ b/netmap/selector.go @@ -171,8 +171,10 @@ func (c *context) getSelectionBase(s netmap.Selector) []nodeAttrPair { } if len(c.hrwSeed) != 0 { + var ws []float64 for i := range result { - hrw.SortHasherSliceByWeightValue(result[i].nodes, result[i].nodes.weights(c.weightFunc), c.hrwSeedHash) + ws = result[i].nodes.appendWeightsTo(c.weightFunc, ws[:0]) + hrw.SortHasherSliceByWeightValue(result[i].nodes, ws, c.hrwSeedHash) } }