You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
syntax: factor out common prefixes of alternations
It is generally quite subtle to reason clearly about how this
actually helps things in a finite automata based regex engine, but this
sort of factoring can lead to lots of improvements:
* We do use a bounded backtracker, so "pushing branches" down will help
things there, just like it would with a classical backtracker.
* It may lead to better literal extraction due to the simpler regex.
Whether prefix factoring is really to blame here is somewhat unclear,
but some downstream optimizations are more brittle than others. For
example, the "reverse inner" optimization requires examining a "top
level" concatenation to find literals to search for. By factoring out a
common prefix, we potentially expand the number of regexes that have a
top-level concat. For example, `\wfoo|\wbar` has no top-level concat but
`\w(?:foo|bar)` does.
* It should lead to faster matching even in finite automata oriented
engines like the PikeVM, and also faster construction of DFAs (lazy or
not). Namely, by pushing the branches down, we make it so they are
visited less frequently, and thus the constant state shuffling caused by
branches is reduced.
The prefix extraction could be better, as mentioned in the comments, but
this is a good start.
0 commit comments