feat: dont compile regex matcher if we know its a literal (#12434)

labels: dont compile regex matcher if we know its a literal

Signed-off-by: Michael Hoffmann <mhoffm@posteo.de>

Co-authored-by: Sharad <sharadgaur@gmail.com>
This commit is contained in:
Michael Hoffmann 2023-06-07 22:54:30 +02:00 committed by Bryan Boreham
parent 90f6c1faba
commit 7f9fe4cb5f

View file

@ -118,12 +118,12 @@ func TestInverse(t *testing.T) {
expected: &Matcher{Type: MatchEqual, Name: "name2", Value: "value2"},
},
{
matcher: &Matcher{Type: MatchRegexp, Name: "name3", Value: "value3"},
expected: &Matcher{Type: MatchNotRegexp, Name: "name3", Value: "value3"},
matcher: &Matcher{Type: MatchRegexp, Name: "name3", Value: "value3.*"},
expected: &Matcher{Type: MatchNotRegexp, Name: "name3", Value: "value3.*"},
},
{
matcher: &Matcher{Type: MatchNotRegexp, Name: "name4", Value: "value4"},
expected: &Matcher{Type: MatchRegexp, Name: "name4", Value: "value4"},
matcher: &Matcher{Type: MatchNotRegexp, Name: "name4", Value: "value4.*"},
expected: &Matcher{Type: MatchRegexp, Name: "name4", Value: "value4.*"},
},
}
@ -215,3 +215,13 @@ func BenchmarkMatchType_String(b *testing.B) {
_ = MatchType(i % int(MatchNotRegexp+1)).String()
}
}
func BenchmarkNewMatcher(b *testing.B) {
b.Run("regex matcher with literal", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i <= b.N; i++ {
NewMatcher(MatchRegexp, "foo", "bar")
}
})
}