app/vmselect/netstorage: reduce the number of allocations for blockRefs objects in ProcessSearchQuery()

This should reduce pressure on Go GC at vmselect

The change has been extracted from https://github.com/VictoriaMetrics/VictoriaMetrics/pull/5527
This commit is contained in:
Aliaksandr Valialkin 2024-01-22 21:16:38 +02:00
parent 508c608062
commit 678234e9f0
No known key found for this signature in database
GPG key ID: 52C003EE2BCDB9EB

View file

@ -1180,10 +1180,14 @@ func ProcessSearchQuery(qt *querytracer.Tracer, sq *storage.SearchQuery, deadlin
var buf []byte
// metricNamesBuf is used for holding all the loaded unique metric names.
// It should reduce pressure on Go garbage collector by reducing
// the number of memory allocations when constructing metricName string from byte slice.
// It should reduce pressure on Go GC by reducing the number of string allocations
// when constructing metricName string from byte slice.
var metricNamesBuf []byte
// brssBuf is used for holding all the blockRefs objects across all the loaded time series.
// It should reduce pressure on Go GC by reducing the number of blockRefs allocations.
var brssBuf []blockRefs
for sr.NextMetricBlock() {
blocksRead++
if deadline.Exceeded() {
@ -1208,7 +1212,12 @@ func ProcessSearchQuery(qt *querytracer.Tracer, sq *storage.SearchQuery, deadlin
metricName := sr.MetricBlockRef.MetricName
brs := m[string(metricName)]
if brs == nil {
brs = &blockRefs{}
if cap(brssBuf) > len(brssBuf) {
brssBuf = brssBuf[:len(brssBuf)+1]
} else {
brssBuf = append(brssBuf, blockRefs{})
}
brs = &brssBuf[len(brssBuf)-1]
brs.brs = brs.brsPrealloc[:0]
}
brs.brs = append(brs.brs, blockRef{