From 2eb967231b3f313c8ee1f69040ab7563a73d33b7 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Mon, 12 Feb 2024 18:03:19 +0200 Subject: [PATCH] lib/storage: do not append headerData to bsw.indexData if its size exceeds maxBlockSize This is a follow-up optimization after 3c246cdf00ea453dd5d42ef8d679ca96df54b61c --- lib/storage/block_stream_writer.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/storage/block_stream_writer.go b/lib/storage/block_stream_writer.go index 9d90c77412..f674b13c38 100644 --- a/lib/storage/block_stream_writer.go +++ b/lib/storage/block_stream_writer.go @@ -135,6 +135,7 @@ func (bsw *blockStreamWriter) WriteExternalBlock(b *Block, ph *partHeader, rowsM atomic.AddUint64(rowsMerged, uint64(b.rowsCount())) b.deduplicateSamplesDuringMerge() headerData, timestampsData, valuesData := b.MarshalData(bsw.timestampsBlockOffset, bsw.valuesBlockOffset) + usePrevTimestamps := len(bsw.prevTimestampsData) > 0 && bytes.Equal(timestampsData, bsw.prevTimestampsData) if usePrevTimestamps { // The current timestamps block equals to the previous timestamps block. @@ -143,14 +144,13 @@ func (bsw *blockStreamWriter) WriteExternalBlock(b *Block, ph *partHeader, rowsM atomic.AddUint64(×tampsBlocksMerged, 1) atomic.AddUint64(×tampsBytesSaved, uint64(len(timestampsData))) } - indexDataLen := len(bsw.indexData) - bsw.indexData = append(bsw.indexData, headerData...) - if len(bsw.indexData) > maxBlockSize { - bsw.indexData = bsw.indexData[:indexDataLen] + + if len(bsw.indexData)+len(headerData) > maxBlockSize { bsw.flushIndexData() - bsw.indexData = append(bsw.indexData, headerData...) } + bsw.indexData = append(bsw.indexData, headerData...) bsw.mr.RegisterBlockHeader(&b.bh) + if !usePrevTimestamps { bsw.prevTimestampsData = append(bsw.prevTimestampsData[:0], timestampsData...) bsw.prevTimestampsBlockOffset = bsw.timestampsBlockOffset