From aec9cd4316741645a7d439e9f67087af102fd6de Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Thu, 22 Feb 2024 17:37:43 +0200 Subject: [PATCH] lib/storage: do not pool rawRowsBlock when flushing rawRows to in-memory blocks The pooled rawRowsBlock objects occupies big amounts of memory between flushes, and the flushes are relatively rare. So it is better to don't use the pool and to allocate rawRow blocks on demand. This should reduce the average memory usage between flushes. --- lib/mergeset/table.go | 4 +--- lib/storage/partition.go | 32 ++++---------------------------- 2 files changed, 5 insertions(+), 31 deletions(-) diff --git a/lib/mergeset/table.go b/lib/mergeset/table.go index f55a4730e..9a19ffd28 100644 --- a/lib/mergeset/table.go +++ b/lib/mergeset/table.go @@ -255,9 +255,7 @@ func (ris *rawItemsShard) addItems(tb *Table, items [][]byte) [][]byte { ris.ibs = ibs ris.mu.Unlock() - if len(ibsToFlush) > 0 { - tb.flushBlocksToInmemoryParts(ibsToFlush, false) - } + tb.flushBlocksToInmemoryParts(ibsToFlush, false) return tailItems } diff --git a/lib/storage/partition.go b/lib/storage/partition.go index ef5937d24..e680885ca 100644 --- a/lib/storage/partition.go +++ b/lib/storage/partition.go @@ -489,7 +489,7 @@ func (rrs *rawRowsShard) Len() int { } func (rrs *rawRowsShard) addRows(pt *partition, rows []rawRow) []rawRow { - var rrb *rawRowsBlock + var rowsToFlush []rawRow rrs.mu.Lock() if cap(rrs.rows) == 0 { @@ -499,8 +499,8 @@ func (rrs *rawRowsShard) addRows(pt *partition, rows []rawRow) []rawRow { rrs.rows = rrs.rows[:len(rrs.rows)+n] rows = rows[n:] if len(rows) > 0 { - rrb = getRawRowsBlock() - rrb.rows, rrs.rows = rrs.rows, rrb.rows + rowsToFlush = rrs.rows + rrs.rows = newRawRows() n = copy(rrs.rows[:cap(rrs.rows)], rows) rrs.rows = rrs.rows[:n] rows = rows[n:] @@ -508,40 +508,16 @@ func (rrs *rawRowsShard) addRows(pt *partition, rows []rawRow) []rawRow { } rrs.mu.Unlock() - if rrb != nil { - pt.flushRowsToInmemoryParts(rrb.rows) - putRawRowsBlock(rrb) - } + pt.flushRowsToInmemoryParts(rowsToFlush) return rows } -type rawRowsBlock struct { - rows []rawRow -} - func newRawRows() []rawRow { n := getMaxRawRowsPerShard() return make([]rawRow, 0, n) } -func getRawRowsBlock() *rawRowsBlock { - v := rawRowsBlockPool.Get() - if v == nil { - return &rawRowsBlock{ - rows: newRawRows(), - } - } - return v.(*rawRowsBlock) -} - -func putRawRowsBlock(rrb *rawRowsBlock) { - rrb.rows = rrb.rows[:0] - rawRowsBlockPool.Put(rrb) -} - -var rawRowsBlockPool sync.Pool - func (pt *partition) flushRowsToInmemoryParts(rows []rawRow) { if len(rows) == 0 { return