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.
This commit is contained in:
Aliaksandr Valialkin 2023-01-09 22:19:53 -08:00
parent 562d6bca08
commit 4f0c11ee93
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1

View file

@ -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)