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
This commit is contained in:
Aliaksandr Valialkin 2023-01-09 22:45:35 -08:00
parent 2f3ddd4884
commit 645c24dc5f
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1
3 changed files with 15 additions and 9 deletions

View file

@ -116,10 +116,10 @@ func aggrFuncExt(afe func(tss []*timeseries, modifier *metricsql.ModifierExpr) [
for i, ts := range arg { for i, ts := range arg {
removeGroupTags(&ts.MetricName, modifier) removeGroupTags(&ts.MetricName, modifier)
bb.B = marshalMetricNameSorted(bb.B[:0], &ts.MetricName) bb.B = marshalMetricNameSorted(bb.B[:0], &ts.MetricName)
k := bytesutil.InternBytes(bb.B)
if keepOriginal { if keepOriginal {
ts = argOrig[i] ts = argOrig[i]
} }
k := bytesutil.InternBytes(bb.B)
tss := m[k] tss := m[k]
if tss == nil && maxSeries > 0 && len(m) >= maxSeries { if tss == nil && maxSeries > 0 && len(m) >= maxSeries {
// We already reached time series limit after grouping. Skip other time series. // We already reached time series limit after grouping. Skip other time series.

View file

@ -488,7 +488,8 @@ func mergeTimeseries(a, b []*timeseries, bStart int64, ec *EvalConfig) []*timese
defer bbPool.Put(bb) defer bbPool.Put(bb)
for _, ts := range a { for _, ts := range a {
bb.B = marshalMetricNameSorted(bb.B[:0], &ts.MetricName) 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)) 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) tmp.MetricName.MoveFrom(&tsB.MetricName)
bb.B = marshalMetricNameSorted(bb.B[:0], &tmp.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 { if tsA == nil {
tStart := ec.Start tStart := ec.Start
for tStart < bStart { for tStart < bStart {
@ -509,7 +511,7 @@ func mergeTimeseries(a, b []*timeseries, bStart int64, ec *EvalConfig) []*timese
} }
} else { } else {
tmp.Values = append(tmp.Values, tsA.Values...) tmp.Values = append(tmp.Values, tsA.Values...)
delete(m, string(bb.B)) delete(m, k)
} }
tmp.Values = append(tmp.Values, tsB.Values...) tmp.Values = append(tmp.Values, tsB.Values...)
if len(tmp.Values) != len(tmp.Timestamps) { if len(tmp.Values) != len(tmp.Timestamps) {

View file

@ -411,7 +411,8 @@ func transformBucketsLimit(tfa *transformFuncArg) ([]*timeseries, error) {
mn.CopyFrom(&ts.MetricName) mn.CopyFrom(&ts.MetricName)
mn.RemoveTag("le") mn.RemoveTag("le")
b = marshalMetricNameSorted(b[:0], &mn) 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, le: le,
ts: ts, ts: ts,
}) })
@ -513,7 +514,8 @@ func vmrangeBucketsToLE(tss []*timeseries) []*timeseries {
ts.MetricName.RemoveTag("le") ts.MetricName.RemoveTag("le")
ts.MetricName.RemoveTag("vmrange") ts.MetricName.RemoveTag("vmrange")
bb.B = marshalMetricNameSorted(bb.B[:0], &ts.MetricName) 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, startStr: startStr,
endStr: endStr, endStr: endStr,
start: start, start: start,
@ -1002,7 +1004,8 @@ func groupLeTimeseries(tss []*timeseries) map[string][]leTimeseries {
ts.MetricName.ResetMetricGroup() ts.MetricName.ResetMetricGroup()
ts.MetricName.RemoveTag("le") ts.MetricName.RemoveTag("le")
bb.B = marshalMetricTagsSorted(bb.B[:0], &ts.MetricName) 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, le: le,
ts: ts, ts: ts,
}) })
@ -1532,10 +1535,11 @@ func transformUnion(tfa *transformFuncArg) ([]*timeseries, error) {
for _, arg := range args { for _, arg := range args {
for _, ts := range arg { for _, ts := range arg {
bb.B = marshalMetricNameSorted(bb.B[:0], &ts.MetricName) bb.B = marshalMetricNameSorted(bb.B[:0], &ts.MetricName)
if m[string(bb.B)] { k := bytesutil.InternBytes(bb.B)
if m[k] {
continue continue
} }
m[string(bb.B)] = true m[k] = true
rvs = append(rvs, ts) rvs = append(rvs, ts)
} }
} }