app/vmselect/promql: avoid memory allocations and copying from source timeseries to the returned result at timeseriesToResult()

This commit is contained in:
Aliaksandr Valialkin 2023-01-09 22:38:31 -08:00
parent 26cf680468
commit 2f3ddd4884
No known key found for this signature in database
GPG Key ID: A72BEC6CD3D0DED1
3 changed files with 16 additions and 10 deletions

View File

@ -129,9 +129,11 @@ func timeseriesToResult(tss []*timeseries, maySort bool) ([]netstorage.Result, e
m[k] = struct{}{}
rs := &result[i]
rs.MetricName.CopyFrom(&ts.MetricName)
rs.Values = append(rs.Values[:0], ts.Values...)
rs.Timestamps = append(rs.Timestamps[:0], ts.Timestamps...)
rs.MetricName.MoveFrom(&ts.MetricName)
rs.Values = ts.Values
ts.Values = nil
rs.Timestamps = ts.Timestamps
ts.Timestamps = nil
}
bbPool.Put(bb)

View File

@ -497,11 +497,9 @@ func mergeTimeseries(a, b []*timeseries, bStart int64, ec *EvalConfig) []*timese
tmp.denyReuse = true
tmp.Timestamps = sharedTimestamps
tmp.Values = make([]float64, 0, len(tmp.Timestamps))
// Do not use MetricName.CopyFrom for performance reasons.
// It is safe to make shallow copy, since tsB must no longer used.
tmp.MetricName = tsB.MetricName
tmp.MetricName.MoveFrom(&tsB.MetricName)
bb.B = marshalMetricNameSorted(bb.B[:0], &tsB.MetricName)
bb.B = marshalMetricNameSorted(bb.B[:0], &tmp.MetricName)
tsA := m[string(bb.B)]
if tsA == nil {
tStart := ec.Start
@ -525,9 +523,7 @@ func mergeTimeseries(a, b []*timeseries, bStart int64, ec *EvalConfig) []*timese
var tmp timeseries
tmp.denyReuse = true
tmp.Timestamps = sharedTimestamps
// Do not use MetricName.CopyFrom for performance reasons.
// It is safe to make shallow copy, since tsA must no longer used.
tmp.MetricName = tsA.MetricName
tmp.MetricName.MoveFrom(&tsA.MetricName)
tmp.Values = append(tmp.Values, tsA.Values...)
tStart := bStart

View File

@ -165,6 +165,14 @@ func (mn *MetricName) Reset() {
mn.Tags = mn.Tags[:0]
}
// MoveFrom moves src to mn.
//
// The src is reset after the call.
func (mn *MetricName) MoveFrom(src *MetricName) {
*mn = *src
*src = MetricName{}
}
// CopyFrom copies src to mn.
func (mn *MetricName) CopyFrom(src *MetricName) {
if cap(mn.MetricGroup) > 0 {