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
This commit is contained in:
Aliaksandr Valialkin 2022-12-19 10:27:51 -08:00
parent 057fb2120b
commit dc0b08efb0
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1

View file

@ -256,18 +256,20 @@ func (ps *partSearch) searchBHS() bool {
for len(bhs) > 0 { for len(bhs) > 0 {
// Skip block headers with tsids smaller than the given tsid. // Skip block headers with tsids smaller than the given tsid.
tsid := &ps.BlockRef.bh.TSID tsid := &ps.BlockRef.bh.TSID
n := sort.Search(len(bhs), func(i int) bool { if bhs[0].TSID.Less(tsid) {
return !bhs[i].TSID.Less(tsid) n := sort.Search(len(bhs), func(i int) bool {
}) return !bhs[i].TSID.Less(tsid)
if n == len(bhs) { })
// Nothing found. if n == len(bhs) {
break // Nothing found.
break
}
bhs = bhs[n:]
} }
bhs = bhs[n:] bh := &bhs[0]
// Invariant: tsid <= bh.TSID // Invariant: tsid <= bh.TSID
bh := &bhs[0]
if bh.TSID.MetricID != tsid.MetricID { if bh.TSID.MetricID != tsid.MetricID {
// tsid < bh.TSID: no more blocks with the given tsid. // tsid < bh.TSID: no more blocks with the given tsid.
// Proceed to the next (bigger) tsid. // Proceed to the next (bigger) tsid.