From 645c24dc5f4924f078ada448f572384219069198 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Mon, 9 Jan 2023 22:45:35 -0800 Subject: [PATCH] app/vmselect/promql: consistently intern series names obtained from marshalMetricNameSorted This reduces memory allocations when the returned series names are used as map keys later --- app/vmselect/promql/aggr.go | 2 +- app/vmselect/promql/rollup_result_cache.go | 8 +++++--- app/vmselect/promql/transform.go | 14 +++++++++----- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/app/vmselect/promql/aggr.go b/app/vmselect/promql/aggr.go index 70128935e..6bd094ae3 100644 --- a/app/vmselect/promql/aggr.go +++ b/app/vmselect/promql/aggr.go @@ -116,10 +116,10 @@ func aggrFuncExt(afe func(tss []*timeseries, modifier *metricsql.ModifierExpr) [ for i, ts := range arg { removeGroupTags(&ts.MetricName, modifier) bb.B = marshalMetricNameSorted(bb.B[:0], &ts.MetricName) + k := bytesutil.InternBytes(bb.B) if keepOriginal { ts = argOrig[i] } - k := bytesutil.InternBytes(bb.B) tss := m[k] if tss == nil && maxSeries > 0 && len(m) >= maxSeries { // We already reached time series limit after grouping. Skip other time series. diff --git a/app/vmselect/promql/rollup_result_cache.go b/app/vmselect/promql/rollup_result_cache.go index 11757ea15..6aabd4aa6 100644 --- a/app/vmselect/promql/rollup_result_cache.go +++ b/app/vmselect/promql/rollup_result_cache.go @@ -488,7 +488,8 @@ func mergeTimeseries(a, b []*timeseries, bStart int64, ec *EvalConfig) []*timese defer bbPool.Put(bb) for _, ts := range a { bb.B = marshalMetricNameSorted(bb.B[:0], &ts.MetricName) - m[string(bb.B)] = ts + k := bytesutil.InternBytes(bb.B) + m[k] = ts } rvs := make([]*timeseries, 0, len(a)) @@ -500,7 +501,8 @@ func mergeTimeseries(a, b []*timeseries, bStart int64, ec *EvalConfig) []*timese tmp.MetricName.MoveFrom(&tsB.MetricName) bb.B = marshalMetricNameSorted(bb.B[:0], &tmp.MetricName) - tsA := m[string(bb.B)] + k := bytesutil.InternBytes(bb.B) + tsA := m[k] if tsA == nil { tStart := ec.Start for tStart < bStart { @@ -509,7 +511,7 @@ func mergeTimeseries(a, b []*timeseries, bStart int64, ec *EvalConfig) []*timese } } else { tmp.Values = append(tmp.Values, tsA.Values...) - delete(m, string(bb.B)) + delete(m, k) } tmp.Values = append(tmp.Values, tsB.Values...) if len(tmp.Values) != len(tmp.Timestamps) { diff --git a/app/vmselect/promql/transform.go b/app/vmselect/promql/transform.go index c10e34808..62e5cd92e 100644 --- a/app/vmselect/promql/transform.go +++ b/app/vmselect/promql/transform.go @@ -411,7 +411,8 @@ func transformBucketsLimit(tfa *transformFuncArg) ([]*timeseries, error) { mn.CopyFrom(&ts.MetricName) mn.RemoveTag("le") b = marshalMetricNameSorted(b[:0], &mn) - m[string(b)] = append(m[string(b)], x{ + k := bytesutil.InternBytes(b) + m[k] = append(m[k], x{ le: le, ts: ts, }) @@ -513,7 +514,8 @@ func vmrangeBucketsToLE(tss []*timeseries) []*timeseries { ts.MetricName.RemoveTag("le") ts.MetricName.RemoveTag("vmrange") bb.B = marshalMetricNameSorted(bb.B[:0], &ts.MetricName) - m[string(bb.B)] = append(m[string(bb.B)], x{ + k := bytesutil.InternBytes(bb.B) + m[k] = append(m[k], x{ startStr: startStr, endStr: endStr, start: start, @@ -1002,7 +1004,8 @@ func groupLeTimeseries(tss []*timeseries) map[string][]leTimeseries { ts.MetricName.ResetMetricGroup() ts.MetricName.RemoveTag("le") bb.B = marshalMetricTagsSorted(bb.B[:0], &ts.MetricName) - m[string(bb.B)] = append(m[string(bb.B)], leTimeseries{ + k := bytesutil.InternBytes(bb.B) + m[k] = append(m[k], leTimeseries{ le: le, ts: ts, }) @@ -1532,10 +1535,11 @@ func transformUnion(tfa *transformFuncArg) ([]*timeseries, error) { for _, arg := range args { for _, ts := range arg { bb.B = marshalMetricNameSorted(bb.B[:0], &ts.MetricName) - if m[string(bb.B)] { + k := bytesutil.InternBytes(bb.B) + if m[k] { continue } - m[string(bb.B)] = true + m[k] = true rvs = append(rvs, ts) } }