From faee0e43d1619fed7b51a4f9fa39ca0a73f7c274 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Fri, 17 Nov 2023 15:36:54 +0100 Subject: [PATCH] app/vmselect/promql: reduce the number of memory allocations inside copyTimeseriesShallow() Previously the number of memory allocations inside copyTimeseriesShallow() was equal to 1+len(tss) Reduce this number to 2 by pre-allocating a slice of timeseries structs with len(tss) length. --- app/vmselect/promql/transform.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/app/vmselect/promql/transform.go b/app/vmselect/promql/transform.go index 3ce79d016..0088e3044 100644 --- a/app/vmselect/promql/transform.go +++ b/app/vmselect/promql/transform.go @@ -2743,14 +2743,15 @@ func copyTimeseriesMetricNames(tss []*timeseries, makeCopy bool) []*timeseries { return rvs } -// copyTimeseriesShallow returns a copy of arg with shallow copies of MetricNames, -// Timestamps and Values. -func copyTimeseriesShallow(arg []*timeseries) []*timeseries { - rvs := make([]*timeseries, len(arg)) - for i, src := range arg { - var dst timeseries - dst.CopyShallow(src) - rvs[i] = &dst +// copyTimeseriesShallow returns a copy of src with shallow copies of MetricNames, Timestamps and Values. +func copyTimeseriesShallow(src []*timeseries) []*timeseries { + tss := make([]timeseries, len(src)) + for i, src := range src { + tss[i].CopyShallow(src) + } + rvs := make([]*timeseries, len(tss)) + for i := range tss { + rvs[i] = &tss[i] } return rvs }