[PERF] TSDB: ListPostings: check next item before binary search

It is fairly common that the next item is the one we want, and cheap
to check.

We could also start the binary search one position on, but strangely
that slows it down.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
This commit is contained in:
Bryan Boreham 2025-10-31 15:28:15 +00:00
parent be8307db58
commit 0e1e7441e4

View File

@ -851,15 +851,17 @@ func (it *ListPostings) Seek(x storage.SeriesRef) bool {
return false
}
// Do binary search between current position and end.
i, _ := slices.BinarySearch(it.list, x)
if i < len(it.list) {
it.cur = it.list[i]
it.list = it.list[i+1:]
return true
i := 0 // Check the next item in the list, otherwise binary search between current position and end.
if it.list[0] < x {
i, _ = slices.BinarySearch(it.list, x)
if i >= len(it.list) { // Off the end - terminate the iterator.
it.list = nil
return false
}
}
it.list = nil
return false
it.cur = it.list[i]
it.list = it.list[i+1:]
return true
}
func (*ListPostings) Err() error {