Improving "Sparse postings" intersection

Signed-off-by: alanprot <alanprot@gmail.com>
This commit is contained in:
alanprot 2024-04-22 22:54:22 -07:00
parent 76b0318ed5
commit bf15569e18
2 changed files with 14 additions and 2 deletions

View file

@ -487,15 +487,19 @@ func (it *intersectPostings) At() storage.SeriesRef {
func (it *intersectPostings) doNext() bool { func (it *intersectPostings) doNext() bool {
Loop: Loop:
for { for {
cont := false
for _, p := range it.arr { for _, p := range it.arr {
if !p.Seek(it.cur) { if !p.Seek(it.cur) {
return false return false
} }
if p.At() > it.cur { if p.At() > it.cur {
it.cur = p.At() it.cur = p.At()
continue Loop cont = true
} }
} }
if cont {
continue Loop
}
return true return true
} }
} }

View file

@ -46,6 +46,7 @@ func BenchmarkQuerier(b *testing.B) {
} }
for n := 0; n < 10; n++ { for n := 0; n < 10; n++ {
addSeries(labels.FromStrings("a", strconv.Itoa(n)+postingsBenchSuffix))
for i := 0; i < 100000; i++ { for i := 0; i < 100000; i++ {
addSeries(labels.FromStrings("i", strconv.Itoa(i)+postingsBenchSuffix, "n", strconv.Itoa(n)+postingsBenchSuffix, "j", "foo")) 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. // 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) { func benchmarkPostingsForMatchers(b *testing.B, ir IndexReader) {
ctx := context.Background() ctx := context.Background()
a1 := labels.MustNewMatcher(labels.MatchEqual, "a", "1"+postingsBenchSuffix)
n1 := labels.MustNewMatcher(labels.MatchEqual, "n", "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) nX := labels.MustNewMatcher(labels.MatchEqual, "n", "X"+postingsBenchSuffix)
jFoo := labels.MustNewMatcher(labels.MatchEqual, "j", "foo") 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}}, {`j="foo",n="1"`, []*labels.Matcher{jFoo, n1}},
{`n="1",j!="foo"`, []*labels.Matcher{n1, jNotFoo}}, {`n="1",j!="foo"`, []*labels.Matcher{n1, jNotFoo}},
{`n="1",i!="2"`, []*labels.Matcher{n1, iNot2}}, {`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}}, {`n="X",j!="foo"`, []*labels.Matcher{nX, jNotFoo}},
{`i=~"1[0-9]",j=~"foo|bar"`, []*labels.Matcher{iCharSet, jFooBar}}, {`i=~"1[0-9]",j=~"foo|bar"`, []*labels.Matcher{iCharSet, jFooBar}},
{`j=~"foo|bar"`, []*labels.Matcher{jFooBar}}, {`j=~"foo|bar"`, []*labels.Matcher{jFooBar}},
@ -173,8 +177,12 @@ func benchmarkPostingsForMatchers(b *testing.B, ir IndexReader) {
b.ReportAllocs() b.ReportAllocs()
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
_, err := PostingsForMatchers(ctx, ir, c.matchers...) p, err := PostingsForMatchers(ctx, ir, c.matchers...)
require.NoError(b, err) require.NoError(b, err)
// Iterate over the postings
for p.Next() {
// Do nothing
}
} }
}) })
} }