| Hi again. :-) In practice, production grade general purpose regex engines are never implemented with formal DFAs in all cases. They take too long to build and use way too much memory, especially when Unicode is involved. For example: $ regex-cli debug dfa dense '(?-u)\w{10}' -qm
parse time: 45.418µs
translate time: 14.626µs
compile nfa time: 373.919µs
compile dense dfa time: 564.7µs
dense dfa memory: 5436
dense alphabet length: 23
dense stride: 32
$ regex-cli debug dfa dense '\w{10}' -qm
parse time: 41.344µs
translate time: 27.072µs
compile nfa time: 6.360692ms
compile dense dfa time: 1.038092145s
dense dfa memory: 2998332
dense alphabet length: 115
dense stride: 128
And this is including oodles of tricks for shrink the DFA's size. (Note though that the above examples minimize the DFA, which adds significant time. Without minimization, the latter is about an order of magnitude faster.)So even this simple regex is 3MB in size. Finally, DFAs can grow exponentially in the size of the regex, which means you can't really use them with untrusted regexes. In practice, regex engines like RE2 use a hybrid NFA/DFA, or "lazy DFA." The DFA is computed at search time and cached. If too much cache is used, then the regex engine falls back to an NFA simulation. Beating the performance of a DFA without going into more exotic things like vectorized algorithms or more specialized regex engines (bit-parallel NFAs) is pretty tricky. And also, do not repeat the same mistake that the OP seems to make: conflating what DFA and NFA mean. A lot of people like to call things like PCRE an "NFA" implementation, but it's not, because meaningfully more powerful than what an NFA can do. General purpose regex engines tend to be split between ones that are based on finite automata and ones that are based on backtracking. |
Also, by "do not repeat the same mistake that the OP seems to make: conflating what DFA and NFA mean." do you mean in the article? I wrote that mostly with the intent to say _"this ain't how things are normally done, DFA/NFA exist and are used"_ but admittedly don't have more than a surface-level understanding of them. If you have tips on how to clarify that wording so I don't confuse others, at the very least, I'd much appreciate it! :)