From cfc1193d15c2d7fd4466e26a9daf5de79aec452b Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Tue, 23 Jan 2024 14:04:45 +0200 Subject: [PATCH] app/vmselect/netstorage: limit the maximum brsPool size to 32Kb at ProcessSearchQuery() This avoids slow path in Go runtime for allocating objects bigger than 32Kb - see https://github.com/golang/go/blob/704401ffa06c60e059c9e6e4048045b4ff42530a/src/runtime/malloc.go#L11 This also reduces memory usage a bit for vmselect and single-node VictoriaMetrics after the commit 5dd37ad836ef1381ce7d31e9efdd68d7aae984b5 . Updates https://github.com/VictoriaMetrics/VictoriaMetrics/pull/5527 --- app/vmselect/netstorage/netstorage.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/app/vmselect/netstorage/netstorage.go b/app/vmselect/netstorage/netstorage.go index 2b32cb7c8..b532dee84 100644 --- a/app/vmselect/netstorage/netstorage.go +++ b/app/vmselect/netstorage/netstorage.go @@ -1425,6 +1425,12 @@ func (tbfw *tmpBlocksFileWrapper) RegisterAndWriteBlock(mb *storage.MetricBlock, addrs := &tbfwLocal.addrssPool[addrsIdx] addrsPool := tbfwLocal.addrsPool + if uintptr(cap(addrsPool)) >= maxFastAllocBlockSize/unsafe.Sizeof(tmpBlockAddr{}) && len(addrsPool) == cap(addrsPool) { + // Allocate a new addrsPool in order to avoid slow allocation of an object + // bigger than maxFastAllocBlockSize bytes at append() below. + addrsPool = make([]tmpBlockAddr, 0, maxFastAllocBlockSize/unsafe.Sizeof(tmpBlockAddr{})) + tbfwLocal.addrsPool = addrsPool + } if addrs.addrs == nil || haveSameBlockAddrTails(addrs.addrs, addrsPool) { // It is safe appending addr to addrsPool, since there are no other items added there yet. addrsPool = append(addrsPool, addr)