The Regex Monster Revisited: a Lazy DFA for Go's regexp
Last February I wrote about taming a regex monster : an alternation of a hundred 8-letter literals from a Gophers Slack discussion. The DFA of modernc.org/regexp matched it about 28x faster than the standard library. The catches were a compile tax of about 200x, and a hand-written proof-of-concept VM around NewMatchDFA , because the DFA choked on the .* s around the literals and had to be fed the bare alternation. Rob Pike pointed out that a list of literals is a job for Aho-Corasick, not for regular expressions, and he was right. Since then the DFA has been rewritten, and the tax is gone. Since v1.10.0 the package runs a lazy DFA, in the style of RE2’s and of Rust’s regex-automata, behind the ordinary Regexp API. v1.11.2 is the current release. This post revisits the monster, sketches how the DFA works, and lists what I learned on the way. Some of the lessons were embarrassing. The monster, revisited Same pattern, same inputs, plain regexp.MustCompile and MatchString . One CPU...