TSDB: Make mergedPostings generic

Now we can call it with more specific types which is more efficient than
making everything go through the `Postings` interface.

Benchmark the concrete type.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
This commit is contained in:
Bryan Boreham 2024-07-26 19:35:58 +01:00
parent 1b22242024
commit 0a8779f46d
2 changed files with 11 additions and 11 deletions

View file

@ -660,7 +660,7 @@ func (it *intersectPostings) Err() error {
}
// Merge returns a new iterator over the union of the input iterators.
func Merge(_ context.Context, its ...Postings) Postings {
func Merge[T Postings](_ context.Context, its ...T) Postings {
if len(its) == 0 {
return EmptyPostings()
}
@ -675,19 +675,19 @@ func Merge(_ context.Context, its ...Postings) Postings {
return p
}
type mergedPostings struct {
p []Postings
h *loser.Tree[storage.SeriesRef, Postings]
type mergedPostings[T Postings] struct {
p []T
h *loser.Tree[storage.SeriesRef, T]
cur storage.SeriesRef
}
func newMergedPostings(p []Postings) (m *mergedPostings, nonEmpty bool) {
func newMergedPostings[T Postings](p []T) (m *mergedPostings[T], nonEmpty bool) {
const maxVal = storage.SeriesRef(math.MaxUint64) // This value must be higher than all real values used in the tree.
lt := loser.New(p, maxVal)
return &mergedPostings{p: p, h: lt}, true
return &mergedPostings[T]{p: p, h: lt}, true
}
func (it *mergedPostings) Next() bool {
func (it *mergedPostings[T]) Next() bool {
for {
if !it.h.Next() {
return false
@ -701,7 +701,7 @@ func (it *mergedPostings) Next() bool {
}
}
func (it *mergedPostings) Seek(id storage.SeriesRef) bool {
func (it *mergedPostings[T]) Seek(id storage.SeriesRef) bool {
for !it.h.IsEmpty() && it.h.At() < id {
finished := !it.h.Winner().Seek(id)
it.h.Fix(finished)
@ -713,11 +713,11 @@ func (it *mergedPostings) Seek(id storage.SeriesRef) bool {
return true
}
func (it mergedPostings) At() storage.SeriesRef {
func (it mergedPostings[T]) At() storage.SeriesRef {
return it.cur
}
func (it mergedPostings) Err() error {
func (it mergedPostings[T]) Err() error {
for _, p := range it.p {
if err := p.Err(); err != nil {
return err

View file

@ -392,7 +392,7 @@ func BenchmarkMerge(t *testing.B) {
refs = append(refs, temp)
}
its := make([]Postings, len(refs))
its := make([]*ListPostings, len(refs))
for _, nSeries := range []int{1, 10, 10000, 100000} {
t.Run(strconv.Itoa(nSeries), func(bench *testing.B) {
ctx := context.Background()