model/labels: improve performance of regex matchers like .*-.*-.*#17707
Merged
bboreham merged 4 commits intoprometheus:mainfrom Jan 8, 2026
Merged
Conversation
Signed-off-by: Charles Korn <charles.korn@grafana.com>
Signed-off-by: Charles Korn <charles.korn@grafana.com>
Signed-off-by: Charles Korn <charles.korn@grafana.com>
3ad2a3a to
cb5120e
Compare
bboreham
reviewed
Jan 7, 2026
Member
bboreham
left a comment
There was a problem hiding this comment.
Broadly fine; one question about the structure.
Also could you post your benchmark results for the cases added to BenchmarkFastRegexMatcher ?
Signed-off-by: Charles Korn <charles.korn@grafana.com>
a4dc256 to
db9cc23
Compare
Contributor
Author
That benchmark doesn't show much impact given most of the random test strings will not match the patterns, so they don't benefit from this optimisation: I added the |
bboreham
approved these changes
Jan 8, 2026
Member
bboreham
left a comment
There was a problem hiding this comment.
Thanks, that seems better to me.
56quarters
added a commit
to 56quarters/prometheus
that referenced
this pull request
Jan 9, 2026
This change fixes an issue introduced in prometheus#17707. When a regex with a wildcard, literal, and final wildcard surounded by a capture group was parsed - the capture group was not removed first preventing `optimizeConcatRegex` from running. Found via fuzz testing. Signed-off-by: Nick Pillitteri <nick.pillitteri@grafana.com>
56quarters
added a commit
to 56quarters/prometheus
that referenced
this pull request
Jan 9, 2026
This change fixes an issue introduced in prometheus#17707. When a regex with a wildcard, literal, and final wildcard surounded by a capture group was parsed - the capture group was not removed first preventing `optimizeConcatRegex` from running. Found via fuzz testing. Signed-off-by: Nick Pillitteri <nick.pillitteri@grafana.com>
bboreham
pushed a commit
that referenced
this pull request
Jan 12, 2026
This change fixes an issue introduced in #17707. When a regex with a wildcard, literal, and final wildcard surounded by a capture group was parsed - the capture group was not removed first preventing `optimizeConcatRegex` from running. Found via fuzz testing. Signed-off-by: Nick Pillitteri <nick.pillitteri@grafana.com>
charleskorn
added a commit
to grafana/mimir-prometheus
that referenced
this pull request
Jan 15, 2026
Signed-off-by: Charles Korn <charles.korn@grafana.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue(s) does the PR fix:
#14173 introduced an optimisation to better handle regex patterns like
.*-.*-.*. It identifies strings the pattern cannot possibly match (because they do not contain all of the literal values) and returns false fromMatchStringearly.However, if the string does contain all literal values, then the Go regex engine is used to confirm that the string does match the pattern. But this is not necessary in the case where the start and end of the pattern is
.*and everything in between is either a literal or.*: if the string contains all of the literals in order, then it matches the pattern, and invoking Go's regex engine to confirm this is unnecessary and quite slow.So this PR introduces a fast path for patterns like this.
I think we could apply something similar to patterns that don't start or end with
.*, but this would be a slightly more complex change, and I wanted to keep this PR as small as possible while I get my head aroundFastRegexMatcher.The existing
FastRegexMatcherbenchmark didn't cover the case where a pattern like.*-.*-.*matches the string, so I added a specific benchmark for this case:Does this PR introduce a user-facing change?