Repository navigation
[ENHANCEMENT] Optimize regexps with multiple prefixes - #13843
Conversation
pracucci
left a comment
There was a problem hiding this comment.
Good job Bryan! The logic looks solid to me. I left few minor comments, mostly to fix some comments and naming to match the new logic. I also suggest to improve a bit unit tests to add more cases with prefixes (e.g. alternation of prefixes with mixed case sensitivity).
Can you ping me once ready for a final review, please? 🙏
|
|
||
| func (m *equalMultiStringMapMatcher) setMatches() []string { | ||
| if len(m.values) >= maxSetMatches { | ||
| if len(m.values) >= maxSetMatches || len(m.prefixes) > 0 { |
There was a problem hiding this comment.
From a quick look to TestFindSetMatches I don't think we already have a test case covering the new case (when len(m.prefixes) > 0). Can you add it, please?
There was a problem hiding this comment.
Agreed. I checked the logic with threshold set to 0, so it did execute the new code, but the tests should check every time.
That reminds me: Do you think 16 is still the right choice of threshold?
There was a problem hiding this comment.
maxSetMatches is 256 and I don't remember how it was computed exactly (I remember we did few tests querying TSDB index at that time).
16 is the value of minEqualMultiStringMatcherMapThreshold. I computed the value using BenchmarkOptimizeEqualStringMatchers (see comment to minEqualMultiStringMatcherMapThreshold).
There was a problem hiding this comment.
Sorry I see you are commenting specifically on setMatches. I can add that case.
There was a problem hiding this comment.
I checked: setMatches is only called from optimizeAlternatingLiterals which currently does not cover prefixes.
So it is not possible to create a case where len(m.prefixes) > 0.
| // equalStringMatcher found. Returns true if and only if the input StringMatcher is *only* | ||
| // composed by an alternation of equalStringMatcher. | ||
| func findEqualStringMatchers(input StringMatcher, callback func(matcher *equalStringMatcher) bool) bool { | ||
| func findEqualStringMatchers(input StringMatcher, callback func(matcher *equalStringMatcher) bool, prefixCallback func(matcher *literalPrefixStringMatcher) bool) bool { |
There was a problem hiding this comment.
Now this function is doing more than its name and description. We should update the description and rename it.
Signed-off-by: Marco Pracucci <[email protected]>
|
@bboreham I would like to move forward with this PR. With the aim to help, I opened a PR against your branch to address my own comments: bboreham#3 |
Signed-off-by: Marco Pracucci <[email protected]>
Address review comments on prometheus#13843 Signed-off-by: Marco Pracucci <[email protected]>
f9224d1 to
5b7ab53
Compare
|
Update 2024-07-02: the slow-down has disappeared, possibly because of optimisations to case-insensitive search in other PRs. Meanwhile, I discovered another approach, to sort the alternates during regexp compilation grafana/regexp#11, which works well when the strings have some similarity. |
For example `foo.*|bar.*|baz.*`. Instead of checking each one in turn, we build a map of prefixes, then check the smaller set that could match the string supplied. Signed-off-by: Bryan Boreham <[email protected]>
Signed-off-by: Bryan Boreham <[email protected]>
Address review comments on prometheus#13843 Signed-off-by: Marco Pracucci <[email protected]>
Address review comments on prometheus#13843 Signed-off-by: Marco Pracucci <[email protected]>
Signed-off-by: Bryan Boreham <[email protected]>
5b7ab53 to
73edbc6
Compare
pracucci
left a comment
There was a problem hiding this comment.
Very nice work, LGTM! I've also locally run some fuzzy tests and they couldn't spot any issue. I left few minor comments I would like you to consider before merging, thanks!
Co-authored-by: Marco Pracucci <[email protected]> Signed-off-by: Bryan Boreham <[email protected]>
Storing MaxInt would break various validity checks. Signed-off-by: Bryan Boreham <[email protected]>
pracucci
left a comment
There was a problem hiding this comment.
LGTM, thanks for addressing my feedback!
…changing case folds The case-insensitive literal prefix/suffix fast paths of FastRegexMatcher assumed that case folding preserves the encoded length of a rune, slicing the input at len(prefix) or len(suffix) bytes before comparing with strings.EqualFold. Unicode simple case folding (which the regexp engine applies for case-insensitive matching) does not guarantee that: 'k' folds with the Kelvin sign 'K' (U+212A, 3 bytes) and 's' folds with the long s 'ſ' (U+017F, 2 bytes). As a result the fast paths cut such runes in the middle and wrongly rejected values that the regexp itself matches, so the optimized matcher disagreed with its own fallback regexp: - (?i)k.* did not match "Kelvin" (prefix pre-filter added in #18540), - (?i)(ka.+|kb.+) did not match "Ka" (literalPrefixInsensitiveStringMatcher, #13843), - .*(?i:k) did not match "abcK" (literalSuffixStringMatcher, #13843). In a query like foo{bar=~"(?i)k.*"} this silently drops matching series. Replace the byte-slicing checks with fold-aware prefix/suffix comparisons that walk runes under unicode.SimpleFold (with an ASCII fast path processing both strings in lockstep) and return the number of input bytes actually consumed, so the remainder handed to the right/left sub-matchers is sliced at the correct position. Co-Authored-By: Claude Fable 5 <[email protected]> Signed-off-by: TowyTowy <[email protected]>
For example
foo.*|bar.*|baz.*. Instead of checking each one in turn, we build a map of prefixes, then check the smaller set that could match the string supplied.Benchmarks (updated 2024-07-02):