From dc0b08efb0b9a9649e4cfa50ded91eb4dd1006b7 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Mon, 19 Dec 2022 10:27:51 -0800 Subject: [PATCH] lib/storage: optimize partSearch.searchBHS() for common case when the TSID for the current block header is bigger or equal to the current tsid This should help improving performance at https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3425 --- lib/storage/part_search.go | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/storage/part_search.go b/lib/storage/part_search.go index 572b45553..627bb01ba 100644 --- a/lib/storage/part_search.go +++ b/lib/storage/part_search.go @@ -256,18 +256,20 @@ func (ps *partSearch) searchBHS() bool { for len(bhs) > 0 { // Skip block headers with tsids smaller than the given tsid. tsid := &ps.BlockRef.bh.TSID - n := sort.Search(len(bhs), func(i int) bool { - return !bhs[i].TSID.Less(tsid) - }) - if n == len(bhs) { - // Nothing found. - break + if bhs[0].TSID.Less(tsid) { + n := sort.Search(len(bhs), func(i int) bool { + return !bhs[i].TSID.Less(tsid) + }) + if n == len(bhs) { + // Nothing found. + break + } + bhs = bhs[n:] } - bhs = bhs[n:] + bh := &bhs[0] // Invariant: tsid <= bh.TSID - bh := &bhs[0] if bh.TSID.MetricID != tsid.MetricID { // tsid < bh.TSID: no more blocks with the given tsid. // Proceed to the next (bigger) tsid.