From a5a4eab679ccf2d432ab34b5143ef9e1c6482839 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Fri, 6 Oct 2023 12:28:07 +0100 Subject: [PATCH] Storage: reduce memory allocations when merging series sets (#12938) Instead of setting to nil and allocating a new slice every time the merge is advanced, re-use the previous slice. This is safe because the `currentSets` member is only used inside member functions, and explicitly copied in `At()`, the only place it leaves the struct. Signed-off-by: Bryan Boreham --- storage/merge.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/storage/merge.go b/storage/merge.go index f7a88738c..1e29374e5 100644 --- a/storage/merge.go +++ b/storage/merge.go @@ -347,7 +347,7 @@ func (c *genericMergeSeriesSet) Next() bool { } // Now, pop items of the heap that have equal label sets. - c.currentSets = nil + c.currentSets = c.currentSets[:0] c.currentLabels = c.heap[0].At().Labels() for len(c.heap) > 0 && labels.Equal(c.currentLabels, c.heap[0].At().Labels()) { set := heap.Pop(&c.heap).(genericSeriesSet)