track-number-of-optimized-regexp-label-matchers - make isRegexOptimized (#481)

public

Signed-off-by: dhanu <andreasdhanu@gmail.com>
This commit is contained in:
Dhanu Saputra 2023-04-18 14:24:41 +07:00 committed by GitHub
parent c461e22341
commit ad4dc380d5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 5 deletions

View file

@ -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()
}

View file

@ -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()

View file

@ -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 != ""
}

View file

@ -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
}