From c743b2f3cdf9603c07229ca00962a3dc3dc14edd Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Thu, 18 Sep 2025 10:58:25 +0100 Subject: [PATCH] [PERF] Regex: stop calling Simplify It slows down compilation and doesn't make any of our benchmarks go faster. Assumed to be something that helped at an earlier point, but doesn't help now. Add a benchmark with a more complicated regex to demonstrate the slowdown. Signed-off-by: Bryan Boreham --- model/labels/matcher_test.go | 7 +++++++ model/labels/regexp.go | 2 -- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/model/labels/matcher_test.go b/model/labels/matcher_test.go index ff39d40d0f..214bb37eff 100644 --- a/model/labels/matcher_test.go +++ b/model/labels/matcher_test.go @@ -225,6 +225,13 @@ func BenchmarkNewMatcher(b *testing.B) { NewMatcher(MatchRegexp, "foo", "bar") } }) + b.Run("complex regex", func(b *testing.B) { + b.ReportAllocs() + b.ResetTimer() + for i := 0; i <= b.N; i++ { + NewMatcher(MatchRegexp, "foo", "((.*)(bar|b|buzz)(.+)|foo){10}") + } + }) } func BenchmarkMatcher_String(b *testing.B) { diff --git a/model/labels/regexp.go b/model/labels/regexp.go index 6838e094f1..dfab677031 100644 --- a/model/labels/regexp.go +++ b/model/labels/regexp.go @@ -67,8 +67,6 @@ func NewFastRegexMatcher(v string) (*FastRegexMatcher, error) { if err != nil { return nil, err } - // Simplify the syntax tree to run faster. - parsed = parsed.Simplify() m.re, err = regexp.Compile("^(?s:" + parsed.String() + ")$") if err != nil { return nil, err