From bf15569e18f7a9f99a07ee8e12d9bcb51f6f4e64 Mon Sep 17 00:00:00 2001 From: alanprot Date: Mon, 22 Apr 2024 22:54:22 -0700 Subject: [PATCH] Improving "Sparse postings" intersection Signed-off-by: alanprot --- tsdb/index/postings.go | 6 +++++- tsdb/querier_bench_test.go | 10 +++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/tsdb/index/postings.go b/tsdb/index/postings.go index 61a5560ee..b48c086d5 100644 --- a/tsdb/index/postings.go +++ b/tsdb/index/postings.go @@ -487,15 +487,19 @@ func (it *intersectPostings) At() storage.SeriesRef { func (it *intersectPostings) doNext() bool { Loop: for { + cont := false for _, p := range it.arr { if !p.Seek(it.cur) { return false } if p.At() > it.cur { it.cur = p.At() - continue Loop + cont = true } } + if cont { + continue Loop + } return true } } diff --git a/tsdb/querier_bench_test.go b/tsdb/querier_bench_test.go index 9a8230242..9294d8fcd 100644 --- a/tsdb/querier_bench_test.go +++ b/tsdb/querier_bench_test.go @@ -46,6 +46,7 @@ func BenchmarkQuerier(b *testing.B) { } for n := 0; n < 10; n++ { + addSeries(labels.FromStrings("a", strconv.Itoa(n)+postingsBenchSuffix)) for i := 0; i < 100000; i++ { addSeries(labels.FromStrings("i", strconv.Itoa(i)+postingsBenchSuffix, "n", strconv.Itoa(n)+postingsBenchSuffix, "j", "foo")) // Have some series that won't be matched, to properly test inverted matches. @@ -98,7 +99,9 @@ func BenchmarkQuerier(b *testing.B) { func benchmarkPostingsForMatchers(b *testing.B, ir IndexReader) { ctx := context.Background() + a1 := labels.MustNewMatcher(labels.MatchEqual, "a", "1"+postingsBenchSuffix) n1 := labels.MustNewMatcher(labels.MatchEqual, "n", "1"+postingsBenchSuffix) + n0_1 := labels.MustNewMatcher(labels.MatchEqual, "n", "0_1"+postingsBenchSuffix) nX := labels.MustNewMatcher(labels.MatchEqual, "n", "X"+postingsBenchSuffix) jFoo := labels.MustNewMatcher(labels.MatchEqual, "j", "foo") @@ -134,6 +137,7 @@ func benchmarkPostingsForMatchers(b *testing.B, ir IndexReader) { {`j="foo",n="1"`, []*labels.Matcher{jFoo, n1}}, {`n="1",j!="foo"`, []*labels.Matcher{n1, jNotFoo}}, {`n="1",i!="2"`, []*labels.Matcher{n1, iNot2}}, + {`n="0_1",j="foo,a="1"`, []*labels.Matcher{n0_1, jFoo, a1}}, {`n="X",j!="foo"`, []*labels.Matcher{nX, jNotFoo}}, {`i=~"1[0-9]",j=~"foo|bar"`, []*labels.Matcher{iCharSet, jFooBar}}, {`j=~"foo|bar"`, []*labels.Matcher{jFooBar}}, @@ -173,8 +177,12 @@ func benchmarkPostingsForMatchers(b *testing.B, ir IndexReader) { b.ReportAllocs() b.ResetTimer() for i := 0; i < b.N; i++ { - _, err := PostingsForMatchers(ctx, ir, c.matchers...) + p, err := PostingsForMatchers(ctx, ir, c.matchers...) require.NoError(b, err) + // Iterate over the postings + for p.Next() { + // Do nothing + } } }) }