From 311673d62ebdeb424cce73c9e695231e95cbc796 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Rabenstein?= Date: Wed, 13 Oct 2021 11:13:49 +0200 Subject: [PATCH] Save on slice allocations in histogramIterator (#9494) Signed-off-by: beorn7 --- tsdb/chunkenc/histogram.go | 48 ++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/tsdb/chunkenc/histogram.go b/tsdb/chunkenc/histogram.go index ad0dbe9f3..c92aa65f3 100644 --- a/tsdb/chunkenc/histogram.go +++ b/tsdb/chunkenc/histogram.go @@ -652,17 +652,10 @@ func (it *histogramIterator) Reset(b []byte) { it.t, it.cnt, it.zCnt = 0, 0, 0 it.tDelta, it.cntDelta, it.zCntDelta = 0, 0, 0 - // TODO(beorn7): Those will be recreated anyway. - // Either delete them here entirely or recycle them - // below if big enough. - for i := range it.pBuckets { - it.pBuckets[i] = 0 - it.pBucketsDelta[i] = 0 - } - for i := range it.nBuckets { - it.nBuckets[i] = 0 - it.nBucketsDelta[i] = 0 - } + it.pBuckets = it.pBuckets[:0] + it.pBucketsDelta = it.pBucketsDelta[:0] + it.nBuckets = it.nBuckets[:0] + it.pBucketsDelta = it.pBucketsDelta[:0] it.sum = 0 it.leading = 0 @@ -688,14 +681,33 @@ func (it *histogramIterator) Next() bool { it.schema = schema it.zThreshold = zeroThreshold it.pSpans, it.nSpans = posSpans, negSpans - numPosBuckets, numNegBuckets := countSpans(posSpans), countSpans(negSpans) - if numPosBuckets > 0 { - it.pBuckets = make([]int64, numPosBuckets) - it.pBucketsDelta = make([]int64, numPosBuckets) + numPBuckets, numNBuckets := countSpans(posSpans), countSpans(negSpans) + // Allocate bucket slices as needed, recycling existing slices + // in case this iterator was reset and already has slices of a + // sufficient capacity.. + if numPBuckets > 0 { + if cap(it.pBuckets) < numPBuckets { + it.pBuckets = make([]int64, numPBuckets) + // If cap(it.pBuckets) isn't sufficient, neither is cap(it.pBucketsDelta). + it.pBucketsDelta = make([]int64, numPBuckets) + } else { + for i := 0; i < numPBuckets; i++ { + it.pBuckets = append(it.pBuckets, 0) + it.pBucketsDelta = append(it.pBucketsDelta, 0) + } + } } - if numNegBuckets > 0 { - it.nBuckets = make([]int64, numNegBuckets) - it.nBucketsDelta = make([]int64, numNegBuckets) + if numNBuckets > 0 { + if cap(it.nBuckets) < numNBuckets { + it.nBuckets = make([]int64, numNBuckets) + // If cap(it.nBuckets) isn't sufficient, neither is cap(it.nBucketsDelta). + it.nBucketsDelta = make([]int64, numNBuckets) + } else { + for i := 0; i < numNBuckets; i++ { + it.nBuckets = append(it.nBuckets, 0) + it.nBucketsDelta = append(it.nBucketsDelta, 0) + } + } } // Now read the actual data.