From 1b0a29701b02202f192d2cb0c8b1ebaafc791f6a Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Fri, 23 Dec 2022 13:33:14 +0000 Subject: [PATCH] promql: optimise aggregation with no labels For a query like 'sum (foo)', we can quickly skip to the empty labels that its result needs. Signed-off-by: Bryan Boreham --- promql/engine.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/promql/engine.go b/promql/engine.go index fd2022207..ddfb26b13 100644 --- a/promql/engine.go +++ b/promql/engine.go @@ -2384,14 +2384,18 @@ func (ev *evaluator) aggregation(op parser.ItemType, grouping []string, without group, ok := result[groupingKey] // Add a new group if it doesn't exist. if !ok { + var m labels.Labels enh.resetBuilder(metric) if without { enh.lb.Del(grouping...) enh.lb.Del(labels.MetricName) - } else { + m = enh.lb.Labels(labels.EmptyLabels()) + } else if len(grouping) > 0 { enh.lb.Keep(grouping...) + m = enh.lb.Labels(labels.EmptyLabels()) + } else { + m = labels.EmptyLabels() } - m := enh.lb.Labels(labels.EmptyLabels()) newAgg := &groupedAggregation{ labels: m, value: s.V,