From 4f0c11ee93507e8ac2721ab3accb2aa548f13151 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Mon, 9 Jan 2023 22:19:53 -0800 Subject: [PATCH] app/vmselect/promql: intern output series names inside timeseriesToResult() This reduces the number of memory allocations for repeated queries, which return (almost) the same set of time series. --- app/vmselect/promql/exec.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/vmselect/promql/exec.go b/app/vmselect/promql/exec.go index ab2753fc8..7959cf169 100644 --- a/app/vmselect/promql/exec.go +++ b/app/vmselect/promql/exec.go @@ -12,6 +12,7 @@ import ( "github.com/VictoriaMetrics/VictoriaMetrics/app/vmselect/netstorage" "github.com/VictoriaMetrics/VictoriaMetrics/app/vmselect/querystats" + "github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil" "github.com/VictoriaMetrics/VictoriaMetrics/lib/decimal" "github.com/VictoriaMetrics/VictoriaMetrics/lib/querytracer" "github.com/VictoriaMetrics/VictoriaMetrics/lib/storage" @@ -121,10 +122,11 @@ func timeseriesToResult(tss []*timeseries, maySort bool) ([]netstorage.Result, e bb := bbPool.Get() for i, ts := range tss { bb.B = marshalMetricNameSorted(bb.B[:0], &ts.MetricName) - if _, ok := m[string(bb.B)]; ok { + k := bytesutil.InternBytes(bb.B) + if _, ok := m[k]; ok { return nil, fmt.Errorf(`duplicate output timeseries: %s`, stringMetricName(&ts.MetricName)) } - m[string(bb.B)] = struct{}{} + m[k] = struct{}{} rs := &result[i] rs.MetricName.CopyFrom(&ts.MetricName)