Merge pull request #11766 from bboreham/agg-reuse-labelbuilder

Promql: reuse LabelBuilder in aggregations
This commit is contained in:
Ganesh Vernekar 2022-12-27 18:37:41 +05:30 committed by GitHub
commit ae72c752a1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1032,6 +1032,14 @@ type EvalNodeHelper struct {
resultMetric map[string]labels.Labels resultMetric map[string]labels.Labels
} }
func (enh *EvalNodeHelper) resetBuilder(lbls labels.Labels) {
if enh.lb == nil {
enh.lb = labels.NewBuilder(lbls)
} else {
enh.lb.Reset(lbls)
}
}
// DropMetricName is a cached version of DropMetricName. // DropMetricName is a cached version of DropMetricName.
func (enh *EvalNodeHelper) DropMetricName(l labels.Labels) labels.Labels { func (enh *EvalNodeHelper) DropMetricName(l labels.Labels) labels.Labels {
if enh.Dmn == nil { if enh.Dmn == nil {
@ -2152,12 +2160,7 @@ func resultMetric(lhs, rhs labels.Labels, op parser.ItemType, matching *parser.V
enh.resultMetric = make(map[string]labels.Labels, len(enh.Out)) enh.resultMetric = make(map[string]labels.Labels, len(enh.Out))
} }
if enh.lb == nil { enh.resetBuilder(lhs)
enh.lb = labels.NewBuilder(lhs)
} else {
enh.lb.Reset(lhs)
}
buf := bytes.NewBuffer(enh.lblResultBuf[:0]) buf := bytes.NewBuffer(enh.lblResultBuf[:0])
enh.lblBuf = lhs.Bytes(enh.lblBuf) enh.lblBuf = lhs.Bytes(enh.lblBuf)
buf.Write(enh.lblBuf) buf.Write(enh.lblBuf)
@ -2357,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
@ -2382,14 +2384,18 @@ 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) var m labels.Labels
enh.resetBuilder(metric)
if without { if without {
lb.Del(grouping...) enh.lb.Del(grouping...)
lb.Del(labels.MetricName) enh.lb.Del(labels.MetricName)
m = enh.lb.Labels(labels.EmptyLabels())
} else if len(grouping) > 0 {
enh.lb.Keep(grouping...)
m = enh.lb.Labels(labels.EmptyLabels())
} else { } else {
lb.Keep(grouping...) m = labels.EmptyLabels()
} }
m := lb.Labels(labels.EmptyLabels())
newAgg := &groupedAggregation{ newAgg := &groupedAggregation{
labels: m, labels: m,
value: s.V, value: s.V,