Promql: reuse LabelBuilder in aggregations

We have a LabelBuilder in EvalNodeHelper; use it instead of creating a new one at every step.

Need to take some care that different uses of enh.lb do not overlap.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
This commit is contained in:
Bryan Boreham 2022-12-23 12:59:02 +00:00
parent 2c382f5e24
commit aafef011b7

View file

@ -2360,15 +2360,14 @@ func (ev *evaluator) aggregation(op parser.ItemType, grouping []string, without
} }
} }
lb := labels.NewBuilder(labels.EmptyLabels())
var buf []byte var buf []byte
for si, s := range vec { for si, s := range vec {
metric := s.Metric metric := s.Metric
if op == parser.COUNT_VALUES { if op == parser.COUNT_VALUES {
lb.Reset(metric) enh.resetBuilder(metric)
lb.Set(valueLabel, strconv.FormatFloat(s.V, 'f', -1, 64)) enh.lb.Set(valueLabel, strconv.FormatFloat(s.V, 'f', -1, 64))
metric = lb.Labels(labels.EmptyLabels()) metric = enh.lb.Labels(labels.EmptyLabels())
// We've changed the metric so we have to recompute the grouping key. // We've changed the metric so we have to recompute the grouping key.
recomputeGroupingKey = true recomputeGroupingKey = true
@ -2385,14 +2384,14 @@ func (ev *evaluator) aggregation(op parser.ItemType, grouping []string, without
group, ok := result[groupingKey] group, ok := result[groupingKey]
// Add a new group if it doesn't exist. // Add a new group if it doesn't exist.
if !ok { if !ok {
lb.Reset(metric) enh.resetBuilder(metric)
if without { if without {
lb.Del(grouping...) enh.lb.Del(grouping...)
lb.Del(labels.MetricName) enh.lb.Del(labels.MetricName)
} else { } else {
lb.Keep(grouping...) enh.lb.Keep(grouping...)
} }
m := lb.Labels(labels.EmptyLabels()) m := enh.lb.Labels(labels.EmptyLabels())
newAgg := &groupedAggregation{ newAgg := &groupedAggregation{
labels: m, labels: m,
value: s.V, value: s.V,