From c5e0f527bcd6033e571dfced32d97594d1798ba5 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Mon, 9 Jan 2023 21:53:07 -0800 Subject: [PATCH] app/vmselect/netstorage: cache canonical MetricName for time series returned from the storage This reduces memory allocations for repeated queries, which return (almost) the same set of time series. --- app/vmselect/netstorage/netstorage.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/app/vmselect/netstorage/netstorage.go b/app/vmselect/netstorage/netstorage.go index e4026df7b..408766e90 100644 --- a/app/vmselect/netstorage/netstorage.go +++ b/app/vmselect/netstorage/netstorage.go @@ -1090,8 +1090,8 @@ func ProcessSearchQuery(qt *querytracer.Tracer, sq *storage.SearchQuery, deadlin putStorageSearch(sr) return nil, fmt.Errorf("cannot write %d bytes to temporary file: %w", len(buf), err) } - metricName := sr.MetricBlockRef.MetricName - brs := m[string(metricName)] + metricName := bytesutil.InternBytes(sr.MetricBlockRef.MetricName) + brs := m[metricName] if brs == nil { brs = &blockRefs{} } @@ -1100,10 +1100,8 @@ func ProcessSearchQuery(qt *querytracer.Tracer, sq *storage.SearchQuery, deadlin addr: addr, }) if len(brs.brs) == 1 { - // An optimization for big number of time series with long metricName values: - // use only a single copy of metricName for both orderedMetricNames and m. - orderedMetricNames = append(orderedMetricNames, string(metricName)) - m[orderedMetricNames[len(orderedMetricNames)-1]] = brs + orderedMetricNames = append(orderedMetricNames, metricName) + m[metricName] = brs } } if err := sr.Error(); err != nil {