lib/mergeset: pre-allocate data and items for inmemoryBlock in order to reduce memory allocations under high churn rate

Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2007
This commit is contained in:
Aliaksandr Valialkin 2022-02-01 00:56:53 +02:00
parent 4bdd10ab90
commit 9c62b25ad6
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1

View file

@ -117,9 +117,10 @@ func (ib *inmemoryBlock) Add(x []byte) bool {
if len(x)+len(data) > maxInmemoryBlockSize {
return false
}
if cap(data) < maxInmemoryBlockSize {
dataLen := len(data)
data = bytesutil.ResizeWithCopyNoOverallocate(data, maxInmemoryBlockSize)[:dataLen]
if cap(data) == 0 {
// Pre-allocate data and items in order to reduce memory allocations
data = make([]byte, 0, maxInmemoryBlockSize)
ib.items = make([]Item, 0, 512)
}
dataLen := len(data)
data = append(data, x...)