[PERF] TSDB: Speed up intersectPostings.Next

Check if the next position is already a match, in which case we don't
have to call `Seek`.

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

View File

@ -641,15 +641,28 @@ func (it *intersectPostings) Seek(target storage.SeriesRef) bool {
}
func (it *intersectPostings) Next() bool {
target := it.current
for _, p := range it.postings {
// Move forward the first Postings and take its value as the target to match.
if !it.postings[0].Next() {
return false
}
target := it.postings[0].At()
allEqual := true
for _, p := range it.postings[1:] { // Now move forward all the other ones and check if they match.
if !p.Next() {
return false
}
if p.At() > target {
target = p.At()
at := p.At()
if at > target { // This one is past the target, so pick up a new target to Seek at the end.
target = at
allEqual = false
} else if at < target { // This one needs to Seek to the target, but carry on with other postings in case they have an even higher target.
allEqual = false
}
}
if allEqual {
it.current = target
return true
}
return it.Seek(target)
}