diff --git a/pkg/labels/matcher.go b/pkg/labels/matcher.go index 88d463233a..f299c40f64 100644 --- a/pkg/labels/matcher.go +++ b/pkg/labels/matcher.go @@ -28,17 +28,18 @@ const ( MatchNotRegexp ) +var matchTypeToStr = [...]string{ + MatchEqual: "=", + MatchNotEqual: "!=", + MatchRegexp: "=~", + MatchNotRegexp: "!~", +} + func (m MatchType) String() string { - typeToStr := map[MatchType]string{ - MatchEqual: "=", - MatchNotEqual: "!=", - MatchRegexp: "=~", - MatchNotRegexp: "!~", + if m < MatchEqual || m > MatchNotRegexp { + panic("unknown match type") } - if str, ok := typeToStr[m]; ok { - return str - } - panic("unknown match type") + return matchTypeToStr[m] } // Matcher models the matching of a label. diff --git a/pkg/labels/matcher_test.go b/pkg/labels/matcher_test.go index 4c85ac2abf..14615a50d1 100644 --- a/pkg/labels/matcher_test.go +++ b/pkg/labels/matcher_test.go @@ -117,3 +117,9 @@ func TestInverse(t *testing.T) { require.Equal(t, test.expected.Type, result.Type) } } + +func BenchmarkMatchType_String(b *testing.B) { + for i := 0; i <= b.N; i++ { + _ = MatchType(i % int(MatchNotRegexp+1)).String() + } +}