mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2025-03-21 15:45:01 +00:00
lib/logstorage: pre-allocated buffers for fields and rows inside rows.appendRows()
This should reduce the number of memory re-allocations inside the loop, which copies the rows.
This commit is contained in:
parent
0e413a7efb
commit
174a6db19f
1 changed files with 14 additions and 2 deletions
|
@ -7,6 +7,7 @@ import (
|
|||
|
||||
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
|
||||
"github.com/VictoriaMetrics/VictoriaMetrics/lib/encoding"
|
||||
"github.com/VictoriaMetrics/VictoriaMetrics/lib/slicesutil"
|
||||
)
|
||||
|
||||
// Field is a single field for the log entry.
|
||||
|
@ -219,11 +220,22 @@ func (rs *rows) reset() {
|
|||
func (rs *rows) appendRows(timestamps []int64, rows [][]Field) {
|
||||
rs.timestamps = append(rs.timestamps, timestamps...)
|
||||
|
||||
fieldsBuf := rs.fieldsBuf
|
||||
// Pre-allocate rs.fieldsBuf
|
||||
fieldsCount := 0
|
||||
for _, fields := range rows {
|
||||
fieldsCount += len(fields)
|
||||
}
|
||||
fieldsBuf := slicesutil.SetLength(rs.fieldsBuf, len(rs.fieldsBuf)+fieldsCount)
|
||||
fieldsBuf = fieldsBuf[:len(fieldsBuf)-fieldsCount]
|
||||
|
||||
// Pre-allocate rs.rows
|
||||
rs.rows = slicesutil.SetLength(rs.rows, len(rs.rows)+len(rows))
|
||||
dstRows := rs.rows[len(rs.rows)-len(rows):]
|
||||
|
||||
for i, fields := range rows {
|
||||
fieldsLen := len(fieldsBuf)
|
||||
fieldsBuf = append(fieldsBuf, fields...)
|
||||
rs.rows = append(rs.rows, fieldsBuf[fieldsLen:])
|
||||
dstRows[i] = fieldsBuf[fieldsLen:]
|
||||
}
|
||||
rs.fieldsBuf = fieldsBuf
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue