Debian Codegraph @

debian/rust-vendor/fancy-regex/PERFORMANCE.md

raw
1 The README has a quick introduction to the performance of this crate.
2 This will look at some examples and compare them to the Oniguruma engine.
3
4 ## Catastrophic backtracking
5
6 Backtracking engines can have worst-case performance when the regular
7 expression forces the engine to consider an exponentially increasing
8 number of sub-cases.
9
10 For a good explanation of that, read
11 [Runaway Regular Expressions: Catastrophic Backtracking][].
12
13 Let's look at the regex from the README again:
14
15 ```regex
16 (a|b|ab)*bc
17 ```
18
19 And the input text:
20
21 ababababababababababababababababababababababababababababac
22
23 Python's engine has exponential runtime. The regex crate and fancy-regex
24 however have no problem with it.
25
26 ## Oniguruma
27
28 [Oniguruma][] implements a backtracking
29 engine. So we'd expect it to have a problem with the above regex too.
30
31 However, in the above case, it quickly finds that there's no match. How
32 is that possible? The answer is that it has optimizations which
33 sometimes help it avoid having to do any matching at all:
34
35 In the pattern `(a|b|ab)*bc`, you might notice that if the input doesn't
36 contain `bc`, the pattern will never match. Oniguruma detects that and,
37 before it tries to do any matching, tries to find `bc` in the input.
38
39 But what happens if we add `bc` at the end of the input, like this:
40
41 ababababababababababababababababababababababababababababacbc
42
43 Now the optimization doesn't help anymore, and Oniguruma is slow too.
44
45 ## fancy-regex
46
47 For `(a|b|ab)*bc` fancy-regex is fast in all cases because it can
48 delegate to the regex crate which matches it in linear runtime.
49
50 Let's look at another regex, one that makes use of a "fancy" look-ahead:
51
52 ```regex
53 (a|b|ab)*(?=c)
54 ```
55 When fancy-regex matches it against this input:
56
57 abababababababababababababababababababababababababababab
58
59 It's still fast! The reason is that although `(?=c)` is not supported
60 by the regex crate, fancy-regex detects the trailing positive lookahead
61 and is able to essentially rewrite the pattern into
62
63 ```regex
64 ((a|b|ab)*)c
65 ```
66
67 and thus delegate the whole thing to the regex crate, and fixup the
68 captures/match boundaries after a match is found.
69
70 If, however, the lookahead didn't come at the end of the pattern,
71 it would be slow! The reason is that fancy-regex would need to handle
72 the lookahead with backtracking. And because `(a|b|ab)*` is before it,
73 that also needs to be done with backtracking as well.
74
75 Oniguruma doesn't have a problem with this particular case because its
76 optimization saves it again: It checks if there's a `c` in the input
77 before doing any matching.
78
79 fancy-regex now has a **Seek** pre-filter optimization (disabled by
80 default) for hard (backtracking) patterns, which provides similar
81 benefits for many cases. It derives a regular approximation of the
82 pattern and uses it to skip directly to the earliest plausible match
83 position in the haystack, so the backtracking VM only needs to run from
84 that position onward. For patterns where the hard parts leave useful
85 literals or anchors in the approximation — for example, backreferences
86 like `(\w+) \1` or anchored patterns like `(.{5})\1$` — the Seek
87 pre-filter can dramatically reduce the number of positions the VM tries.
88 Lookarounds are currently dropped when building the approximation pattern,
89 so the Seek pre-filter does not directly replicate Oniguruma's "find `c`
90 first" step in that specific case. The Seek optimization can be toggled
91 on/off via the `seek` method on `RegexBuilder`/`RegexOptionsBuilder`.
92
93 Note that how much fancy-regex can do without backtracking depends on
94 the structure of the regex. For example, with `(?=(a|b|ab)*bc)`, the
95 inner part of the look-ahead can be delegated to regex-automata entirely.
96
97 ### Summary
98
99 * If the regex doesn't use fancy features, or the features used in the
100 pattern can be identified as being syntactic sugar and slightly
101 rewritten, fancy-regex should have linear runtime compared to
102 Oniguruma's exponential worst-case.
103 * Even if the regex doesn't use any fancy features, Oniguruma can be
104 faster because it is a mature and highly optimized engine.
105 * With fancy features, Oniguruma can still be faster in some cases. Its
106 mature literal pre-search covers more patterns than the Seek pre-filter
107 (for example, lookaround patterns where the Seek approximation drops
108 the lookahead). The Seek pre-filter can be turned on or off via
109 `RegexBuilder::seek(seek_enabled)` if needed.
110
111 ## Compilation performance
112
113 The sections above are about *match* time. Compiling a pattern
114 (`Regex::new`) has its own cost, which matters if you build many regexes
115 or build them on a hot path.
116
117 For a pattern with no fancy features, fancy-regex is a thin wrapper: it
118 parses the pattern, confirms it needs no backtracking, and hands it to
119 regex-automata. The hand-off goes through a directly-built `Hir` (see
120 `src/to_hir.rs`) rather than a re-serialized pattern string, so the
121 pattern is only parsed once; compile time is then essentially
122 regex-automata's engine build plus a small fixed overhead.
123
124 For a pattern that *does* use fancy features, the compiler splits it into
125 the parts that need the backtracking VM and the parts it can delegate to
126 regex-automata. Each delegated sub-expression that is more than a single
127 character class becomes its own compiled regex-automata engine (also built
128 from an `Hir`, without re-parsing), and building those engines (their NFAs
129 and lazy DFAs) is the dominant cost. Compile time and memory therefore
130 scale roughly with the *number* of such sub-expressions, not the length of
131 the pattern.
132
133 Single character classes (`\d`, `\w`, `[a-z]`, …) — by far the most common
134 delegated piece, including the body of a repeat like `\w+` — are matched
135 directly by the VM instead of building an engine, which keeps compilation
136 of class-heavy fancy patterns cheap.
137
138 A `RegexSet` compiles each member pattern individually and additionally
139 builds two set-wide engines: a multi-pattern meta regex that finds the
140 earliest candidate position, and an overlapping lazy DFA that identifies
141 which patterns can match there. Each member's regular approximation is
142 parsed once and the `Hir` is shared between both set-wide engines, and
143 members built by the set skip constructing prefilters (the set only ever
144 searches them anchored at candidate positions). Set compile cost is
145 still roughly `N` member compiles plus the two combined engine builds.
146
147 [Runaway Regular Expressions: Catastrophic Backtracking]: https://www.regular-expressions.info/catastrophic.html
148 [Oniguruma]: https://github.com/kkos/oniguruma