From 3879488476f778ce7a23be7acd20c184f0c4529e Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Mon, 21 Aug 2023 19:52:14 +0100 Subject: [PATCH] promql: simplify inner loop of rangeEval Took out the loops with break after one iteration, and extract some common code to a function. Signed-off-by: Bryan Boreham --- promql/engine.go | 53 +++++++++++++++++------------------------------- 1 file changed, 19 insertions(+), 34 deletions(-) diff --git a/promql/engine.go b/promql/engine.go index 816f20721..466e4b1d8 100644 --- a/promql/engine.go +++ b/promql/engine.go @@ -1187,42 +1187,27 @@ func (ev *evaluator) rangeEval(prepSeries func(labels.Labels, *EvalSeriesHelper) bufHelpers[i] = bufHelpers[i][:0] } - for si, series := range matrixes[i] { - for _, point := range series.Floats { - if point.T == ts { - if ev.currentSamples < ev.maxSamples { - vectors[i] = append(vectors[i], Sample{Metric: series.Metric, F: point.F, T: ts}) - if prepSeries != nil { - bufHelpers[i] = append(bufHelpers[i], seriesHelpers[i][si]) - } - - // Move input vectors forward so we don't have to re-scan the same - // past points at the next step. - matrixes[i][si].Floats = series.Floats[1:] - ev.currentSamples++ - } else { - ev.error(ErrTooManySamples(env)) - } - } - break + add := func(si int, s Sample) { + vectors[i] = append(vectors[i], s) + if prepSeries != nil { + bufHelpers[i] = append(bufHelpers[i], seriesHelpers[i][si]) } - for _, point := range series.Histograms { - if point.T == ts { - if ev.currentSamples < ev.maxSamples { - vectors[i] = append(vectors[i], Sample{Metric: series.Metric, H: point.H, T: ts}) - if prepSeries != nil { - bufHelpers[i] = append(bufHelpers[i], seriesHelpers[i][si]) - } + ev.currentSamples++ + } - // Move input vectors forward so we don't have to re-scan the same - // past points at the next step. - matrixes[i][si].Histograms = series.Histograms[1:] - ev.currentSamples++ - } else { - ev.error(ErrTooManySamples(env)) - } - } - break + for si, series := range matrixes[i] { + if len(series.Floats) > 0 && series.Floats[0].T == ts { + add(si, Sample{Metric: series.Metric, F: series.Floats[0].F, T: ts}) + // Move input vectors forward so we don't have to re-scan the same + // past points at the next step. + matrixes[i][si].Floats = series.Floats[1:] + } + if len(series.Histograms) > 0 && series.Histograms[0].T == ts { + add(si, Sample{Metric: series.Metric, H: series.Histograms[0].H, T: ts}) + matrixes[i][si].Histograms = series.Histograms[1:] + } + if ev.currentSamples > ev.maxSamples { + ev.error(ErrTooManySamples(env)) } } args[i] = vectors[i]