From 207a62a3c22282ca02521b7ab9a09c9c96d15764 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Sun, 15 Jan 2023 12:58:44 -0800 Subject: [PATCH] app/vmselect/promql: reduce memory allocations when searching for time series pairs with identical labelsets in `q1 op q2` queries Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3641 --- app/vmselect/promql/binary_op.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/app/vmselect/promql/binary_op.go b/app/vmselect/promql/binary_op.go index 8e78664ff..4c304db25 100644 --- a/app/vmselect/promql/binary_op.go +++ b/app/vmselect/promql/binary_op.go @@ -5,6 +5,7 @@ import ( "math" "strings" + "github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil" "github.com/VictoriaMetrics/VictoriaMetrics/lib/logger" "github.com/VictoriaMetrics/VictoriaMetrics/lib/storage" "github.com/VictoriaMetrics/metricsql" @@ -248,7 +249,8 @@ func groupJoin(singleTimeseriesSide string, be *metricsql.BinaryOpExpr, rvsLeft, bb.B = marshalMetricTagsSorted(bb.B[:0], &tsCopy.MetricName) pair, ok := m[string(bb.B)] if !ok { - m[string(bb.B)] = &tsPair{ + k := bytesutil.InternBytes(bb.B) + m[k] = &tsPair{ left: &tsCopy, right: tsRight, } @@ -504,7 +506,8 @@ func createTimeseriesMapByTagSet(be *metricsql.BinaryOpExpr, left, right []*time logger.Panicf("BUG: unexpected binary op modifier %q", groupOp) } bb.B = marshalMetricTagsSorted(bb.B[:0], mn) - m[string(bb.B)] = append(m[string(bb.B)], ts) + k := bytesutil.InternBytes(bb.B) + m[k] = append(m[k], ts) } storage.PutMetricName(mn) bbPool.Put(bb)