Skip to content

Commit

Permalink
MergeIterator: allocate less memory at first
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
bboreham committed Jul 6, 2021
1 parent aacbbd5 commit 18d4ed4
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions pkg/querier/batch/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func newMergeIterator(cs []GenericChunk) *mergeIterator {
c := &mergeIterator{
its: its,
h: make(iteratorHeap, 0, len(its)),
batches: make(batchStream, 0, len(its)*2*promchunk.BatchSize),
batchesBuf: make(batchStream, len(its)*2*promchunk.BatchSize),
batches: make(batchStream, 0, len(its)),
batchesBuf: make(batchStream, len(its)),
}

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

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

0 comments on commit 18d4ed4

Please sign in to comment.