Save on slice allocations in histogramIterator (#9494)

Signed-off-by: beorn7 <beorn@grafana.com>
This commit is contained in:
Björn Rabenstein 2021-10-13 11:13:49 +02:00 committed by GitHub
parent 468a89f1bf
commit 311673d62e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 30 additions and 18 deletions

View File

@ -652,17 +652,10 @@ func (it *histogramIterator) Reset(b []byte) {
it.t, it.cnt, it.zCnt = 0, 0, 0 it.t, it.cnt, it.zCnt = 0, 0, 0
it.tDelta, it.cntDelta, it.zCntDelta = 0, 0, 0 it.tDelta, it.cntDelta, it.zCntDelta = 0, 0, 0
// TODO(beorn7): Those will be recreated anyway. it.pBuckets = it.pBuckets[:0]
// Either delete them here entirely or recycle them it.pBucketsDelta = it.pBucketsDelta[:0]
// below if big enough. it.nBuckets = it.nBuckets[:0]
for i := range it.pBuckets { it.pBucketsDelta = it.pBucketsDelta[:0]
it.pBuckets[i] = 0
it.pBucketsDelta[i] = 0
}
for i := range it.nBuckets {
it.nBuckets[i] = 0
it.nBucketsDelta[i] = 0
}
it.sum = 0 it.sum = 0
it.leading = 0 it.leading = 0
@ -688,14 +681,33 @@ func (it *histogramIterator) Next() bool {
it.schema = schema it.schema = schema
it.zThreshold = zeroThreshold it.zThreshold = zeroThreshold
it.pSpans, it.nSpans = posSpans, negSpans it.pSpans, it.nSpans = posSpans, negSpans
numPosBuckets, numNegBuckets := countSpans(posSpans), countSpans(negSpans) numPBuckets, numNBuckets := countSpans(posSpans), countSpans(negSpans)
if numPosBuckets > 0 { // Allocate bucket slices as needed, recycling existing slices
it.pBuckets = make([]int64, numPosBuckets) // in case this iterator was reset and already has slices of a
it.pBucketsDelta = make([]int64, numPosBuckets) // 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 { if numNBuckets > 0 {
it.nBuckets = make([]int64, numNegBuckets) if cap(it.nBuckets) < numNBuckets {
it.nBucketsDelta = make([]int64, numNegBuckets) 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. // Now read the actual data.