app/vmselect/netstorage: pre-allocate 4 block references per each time series during querying

Usually the number of blocks returned per each time series during queries is around 4.
So it is a good idea to pre-allocate 4 block references per time series
in order to reduce the number of memory allocations.
This commit is contained in:
Aliaksandr Valialkin 2023-01-09 22:03:21 -08:00
parent 2483c67579
commit abbac2c27c
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1

View file

@ -1233,7 +1233,14 @@ type tmpBlocksFileWrapper struct {
}
type blockAddrs struct {
addrs []tmpBlockAddr
addrsPrealloc [4]tmpBlockAddr
addrs []tmpBlockAddr
}
func newBlockAddrs() *blockAddrs {
ba := &blockAddrs{}
ba.addrs = ba.addrsPrealloc[:0]
return ba
}
func newTmpBlocksFileWrapper(sns []*storageNode) *tmpBlocksFileWrapper {
@ -1265,7 +1272,7 @@ func (tbfw *tmpBlocksFileWrapper) RegisterAndWriteBlock(mb *storage.MetricBlock,
m := tbfw.ms[workerID]
addrs := m[metricName]
if addrs == nil {
addrs = &blockAddrs{}
addrs = newBlockAddrs()
}
addrs.addrs = append(addrs.addrs, addr)
if len(addrs.addrs) == 1 {
@ -1295,7 +1302,7 @@ func (tbfw *tmpBlocksFileWrapper) Finalize() ([]string, map[string]*blockAddrs,
dstAddrs, ok := addrsByMetricName[metricName]
if !ok {
orderedMetricNames = append(orderedMetricNames, metricName)
dstAddrs = &blockAddrs{}
dstAddrs = newBlockAddrs()
addrsByMetricName[metricName] = dstAddrs
}
dstAddrs.addrs = append(dstAddrs.addrs, m[metricName].addrs...)