mirror of
https://github.com/prometheus/prometheus.git
synced 2025-01-12 14:27:27 -08:00
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:
parent
1b22242024
commit
0a8779f46d
|
@ -660,7 +660,7 @@ func (it *intersectPostings) Err() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Merge returns a new iterator over the union of the input iterators.
|
// 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 {
|
if len(its) == 0 {
|
||||||
return EmptyPostings()
|
return EmptyPostings()
|
||||||
}
|
}
|
||||||
|
@ -675,19 +675,19 @@ func Merge(_ context.Context, its ...Postings) Postings {
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
type mergedPostings struct {
|
type mergedPostings[T Postings] struct {
|
||||||
p []Postings
|
p []T
|
||||||
h *loser.Tree[storage.SeriesRef, Postings]
|
h *loser.Tree[storage.SeriesRef, T]
|
||||||
cur storage.SeriesRef
|
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.
|
const maxVal = storage.SeriesRef(math.MaxUint64) // This value must be higher than all real values used in the tree.
|
||||||
lt := loser.New(p, maxVal)
|
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 {
|
for {
|
||||||
if !it.h.Next() {
|
if !it.h.Next() {
|
||||||
return false
|
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 {
|
for !it.h.IsEmpty() && it.h.At() < id {
|
||||||
finished := !it.h.Winner().Seek(id)
|
finished := !it.h.Winner().Seek(id)
|
||||||
it.h.Fix(finished)
|
it.h.Fix(finished)
|
||||||
|
@ -713,11 +713,11 @@ func (it *mergedPostings) Seek(id storage.SeriesRef) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (it mergedPostings) At() storage.SeriesRef {
|
func (it mergedPostings[T]) At() storage.SeriesRef {
|
||||||
return it.cur
|
return it.cur
|
||||||
}
|
}
|
||||||
|
|
||||||
func (it mergedPostings) Err() error {
|
func (it mergedPostings[T]) Err() error {
|
||||||
for _, p := range it.p {
|
for _, p := range it.p {
|
||||||
if err := p.Err(); err != nil {
|
if err := p.Err(); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -392,7 +392,7 @@ func BenchmarkMerge(t *testing.B) {
|
||||||
refs = append(refs, temp)
|
refs = append(refs, temp)
|
||||||
}
|
}
|
||||||
|
|
||||||
its := make([]Postings, len(refs))
|
its := make([]*ListPostings, len(refs))
|
||||||
for _, nSeries := range []int{1, 10, 10000, 100000} {
|
for _, nSeries := range []int{1, 10, 10000, 100000} {
|
||||||
t.Run(strconv.Itoa(nSeries), func(bench *testing.B) {
|
t.Run(strconv.Itoa(nSeries), func(bench *testing.B) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
Loading…
Reference in a new issue