From 6865f3b4974d51feb2b9f86d8677496e0beb545d Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Fri, 28 May 2021 01:09:32 +0300 Subject: [PATCH] Revert "lib/mergeset: remove a pool for inmemoryBlock structs" This reverts commit 793fe39921c71aece123d763e5aee35670353f5f. Reason to revert: production testing revealed possible slowdown when registering big number of new time series --- lib/mergeset/encoding.go | 15 +++++++++++++-- lib/mergeset/part_search.go | 2 +- lib/mergeset/table.go | 9 +++++---- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/lib/mergeset/encoding.go b/lib/mergeset/encoding.go index f57f4f4e3..8cb57082c 100644 --- a/lib/mergeset/encoding.go +++ b/lib/mergeset/encoding.go @@ -540,6 +540,17 @@ func putLensBuffer(lb *lensBuffer) { lensBufferPool.Put(lb) } -func newInmemoryBlock() *inmemoryBlock { - return &inmemoryBlock{} +func getInmemoryBlock() *inmemoryBlock { + v := ibPool.Get() + if v == nil { + return &inmemoryBlock{} + } + return v.(*inmemoryBlock) } + +func putInmemoryBlock(ib *inmemoryBlock) { + ib.Reset() + ibPool.Put(ib) +} + +var ibPool sync.Pool diff --git a/lib/mergeset/part_search.go b/lib/mergeset/part_search.go index d2a8a1116..8671561b6 100644 --- a/lib/mergeset/part_search.go +++ b/lib/mergeset/part_search.go @@ -316,7 +316,7 @@ func (ps *partSearch) readInmemoryBlock(bh *blockHeader) (*inmemoryBlock, error) ps.sb.lensData = bytesutil.Resize(ps.sb.lensData, int(bh.lensBlockSize)) ps.p.lensFile.MustReadAt(ps.sb.lensData, int64(bh.lensBlockOffset)) - ib := newInmemoryBlock() + ib := getInmemoryBlock() if err := ib.UnmarshalData(&ps.sb, bh.firstItem, bh.commonPrefix, bh.itemsCount, bh.marshalType); err != nil { return nil, fmt.Errorf("cannot unmarshal storage block with %d items: %w", bh.itemsCount, err) } diff --git a/lib/mergeset/table.go b/lib/mergeset/table.go index 6a9eb7bed..ba12ada94 100644 --- a/lib/mergeset/table.go +++ b/lib/mergeset/table.go @@ -182,15 +182,16 @@ func (ris *rawItemsShard) addItems(tb *Table, items [][]byte) error { ris.mu.Lock() ibs := ris.ibs if len(ibs) == 0 { - ib := newInmemoryBlock() + ib := getInmemoryBlock() ibs = append(ibs, ib) ris.ibs = ibs } ib := ibs[len(ibs)-1] for _, item := range items { if !ib.Add(item) { - ib = newInmemoryBlock() + ib = getInmemoryBlock() if !ib.Add(item) { + putInmemoryBlock(ib) err = fmt.Errorf("cannot insert an item %q into an empty inmemoryBlock; it looks like the item is too large? len(item)=%d", item, len(item)) break } @@ -674,13 +675,13 @@ func (tb *Table) mergeRawItemsBlocks(blocksToMerge []*inmemoryBlock) { func (tb *Table) mergeInmemoryBlocks(blocksToMerge []*inmemoryBlock) *partWrapper { // Convert blocksToMerge into inmemoryPart's mps := make([]*inmemoryPart, 0, len(blocksToMerge)) - for i, ib := range blocksToMerge { + for _, ib := range blocksToMerge { if len(ib.items) == 0 { continue } mp := getInmemoryPart() mp.Init(ib) - blocksToMerge[i] = nil + putInmemoryBlock(ib) mps = append(mps, mp) } if len(mps) == 0 {