Skip to content

Commit

Permalink
track-number-of-optimized-regexp-label-matchers - make isRegexOptimiz…
Browse files Browse the repository at this point in the history
…ed (#481)

public

Signed-off-by: dhanu <[email protected]>
  • Loading branch information
dhanusaputra authored Apr 18, 2023
1 parent c461e22 commit ad4dc38
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
8 changes: 8 additions & 0 deletions model/labels/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,11 @@ func (m *Matcher) Prefix() string {
}
return m.re.prefix
}

// IsRegexOptimized returns whether regex is optimized.
func (m *Matcher) IsRegexOptimized() bool {
if m.re == nil {
return false
}
return m.re.IsOptimized()
}
24 changes: 24 additions & 0 deletions model/labels/matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,30 @@ func TestPrefix(t *testing.T) {
}
}

func TestIsRegexOptimized(t *testing.T) {
for i, tc := range []struct {
matcher *Matcher
isRegexOptimized bool
}{
{
matcher: mustNewMatcher(t, MatchEqual, "abc"),
isRegexOptimized: false,
},
{
matcher: mustNewMatcher(t, MatchRegexp, "."),
isRegexOptimized: false,
},
{
matcher: mustNewMatcher(t, MatchRegexp, "abc.+"),
isRegexOptimized: true,
},
} {
t.Run(fmt.Sprintf("%d: %s", i, tc.matcher), func(t *testing.T) {
require.Equal(t, tc.isRegexOptimized, tc.matcher.IsRegexOptimized())
})
}
}

func BenchmarkMatchType_String(b *testing.B) {
for i := 0; i <= b.N; i++ {
_ = MatchType(i % int(MatchNotRegexp+1)).String()
Expand Down
6 changes: 2 additions & 4 deletions model/labels/regexp.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,9 @@ func (m *FastRegexMatcher) compileMatchStringFunction() func(string) bool {
}
}

// isOptimized returns true if any fast-path optimization is applied to the
// IsOptimized returns true if any fast-path optimization is applied to the
// regex matcher.
//
//nolint:unused
func (m *FastRegexMatcher) isOptimized() bool {
func (m *FastRegexMatcher) IsOptimized() bool {
return len(m.setMatches) > 0 || m.stringMatcher != nil || m.prefix != "" || m.suffix != "" || m.contains != ""
}

Expand Down
2 changes: 1 addition & 1 deletion model/labels/regexp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ func TestAnalyzeRealQueries(t *testing.T) {
numChecked++

// Check if each regexp matcher is supported by our optimization.
if m.isOptimized() {
if m.IsOptimized() {
numOptimized++
info.optimized = true
}
Expand Down

0 comments on commit ad4dc38

Please sign in to comment.