diff --git a/pkg/logql/range_vector.go b/pkg/logql/range_vector.go index 73d66aa2bc25d..94393b6b9fa44 100644 --- a/pkg/logql/range_vector.go +++ b/pkg/logql/range_vector.go @@ -26,6 +26,7 @@ type rangeVectorIterator struct { selRange, step, end, current int64 window map[string]*promql.Series metrics map[string]labels.Labels + at []promql.Sample } func newRangeVectorIterator( @@ -130,11 +131,14 @@ func (r *rangeVectorIterator) load(start, end int64) { } func (r *rangeVectorIterator) At(aggregator RangeVectorAggregator) (int64, promql.Vector) { - result := make([]promql.Sample, 0, len(r.window)) + if r.at == nil { + r.at = make([]promql.Sample, 0, len(r.window)) + } + r.at = r.at[:0] // convert ts from nano to milli seconds as the iterator work with nanoseconds ts := r.current / 1e+6 for _, series := range r.window { - result = append(result, promql.Sample{ + r.at = append(r.at, promql.Sample{ Point: promql.Point{ V: aggregator(series.Points), T: ts, @@ -142,7 +146,7 @@ func (r *rangeVectorIterator) At(aggregator RangeVectorAggregator) (int64, promq Metric: series.Metric, }) } - return ts, result + return ts, r.at } var seriesPool sync.Pool