From a000cec011b87fee6576b88cbcf12b734cf0d139 Mon Sep 17 00:00:00 2001 From: Thomas Jackson Date: Tue, 11 Jun 2019 01:24:50 -0700 Subject: [PATCH] Re-use label builder in promql aggregation (#5641) For my benchmarks on aggregation this reduces allocations by ~5% (~10% time improvement): ``` benchmark old ns/op new ns/op delta BenchmarkEvaluations/benchdata/aggregators.test/promxy-4 727692 649626 -10.73% benchmark old allocs new allocs delta BenchmarkEvaluations/benchdata/aggregators.test/promxy-4 2566 2434 -5.14% benchmark old bytes new bytes delta BenchmarkEvaluations/benchdata/aggregators.test/promxy-4 162760 148854 -8.54% ``` Signed-off-by: Thomas Jackson --- pkg/labels/labels.go | 7 +++++++ promql/engine.go | 6 ++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/pkg/labels/labels.go b/pkg/labels/labels.go index 435f871696..7defc6abc4 100644 --- a/pkg/labels/labels.go +++ b/pkg/labels/labels.go @@ -292,6 +292,13 @@ func NewBuilder(base Labels) *Builder { } } +// Reset clears all current state for the builder +func (b *Builder) Reset(base Labels) { + b.base = base + b.del = b.del[:0] + b.add = b.add[:0] +} + // Del deletes the label of the given name. func (b *Builder) Del(ns ...string) *Builder { for _, n := range ns { diff --git a/promql/engine.go b/promql/engine.go index 283538ccd5..4b178c6dbd 100644 --- a/promql/engine.go +++ b/promql/engine.go @@ -1718,11 +1718,13 @@ func (ev *evaluator) aggregation(op ItemType, grouping []string, without bool, p } } + lb := labels.NewBuilder(nil) + for _, s := range vec { metric := s.Metric if op == ItemCountValues { - lb := labels.NewBuilder(metric) + lb.Reset(metric) lb.Set(valueLabel, strconv.FormatFloat(s.V, 'f', -1, 64)) metric = lb.Labels() } @@ -1742,7 +1744,7 @@ func (ev *evaluator) aggregation(op ItemType, grouping []string, without bool, p var m labels.Labels if without { - lb := labels.NewBuilder(metric) + lb.Reset(metric) lb.Del(grouping...) lb.Del(labels.MetricName) m = lb.Labels()