Skip to content

Commit 18d4ed4

Browse files
committed
MergeIterator: allocate less memory at first
We were allocating 24x the number of streams of batches, where each batch holds up to 12 samples. By allowing `c.batches` to reallocate when needed, we avoid the need to pre-allocate enough memory for all possible scenarios. Signed-off-by: Bryan Boreham <[email protected]>
1 parent aacbbd5 commit 18d4ed4

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

pkg/querier/batch/merge.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ func newMergeIterator(cs []GenericChunk) *mergeIterator {
3131
c := &mergeIterator{
3232
its: its,
3333
h: make(iteratorHeap, 0, len(its)),
34-
batches: make(batchStream, 0, len(its)*2*promchunk.BatchSize),
35-
batchesBuf: make(batchStream, len(its)*2*promchunk.BatchSize),
34+
batches: make(batchStream, 0, len(its)),
35+
batchesBuf: make(batchStream, len(its)),
3636
}
3737

3838
for _, iter := range c.its {
@@ -112,8 +112,7 @@ func (c *mergeIterator) buildNextBatch(size int) bool {
112112
for len(c.h) > 0 && (len(c.batches) == 0 || c.nextBatchEndTime() >= c.h[0].AtTime()) {
113113
c.nextBatchBuf[0] = c.h[0].Batch()
114114
c.batchesBuf = mergeStreams(c.batches, c.nextBatchBuf[:], c.batchesBuf, size)
115-
copy(c.batches[:len(c.batchesBuf)], c.batchesBuf)
116-
c.batches = c.batches[:len(c.batchesBuf)]
115+
c.batches = append(c.batches[:0], c.batchesBuf...)
117116

118117
if c.h[0].Next(size) {
119118
heap.Fix(&c.h, 0)

0 commit comments

Comments
 (0)