5 ms·
C# has compile-time precompiled regular expressions now, too. The enabling C# feature is source generators.
by electroly 3y ago
C# has compile-time precompiled regular expressions now, too. The enabling C# feature is source generators.
- neonsunset 3y agoThey are extremely fast too (way faster than re2): https://github.com/BurntSushi/rebar?tab=readme-ov-file#summary-of-search-time-benchmarks https://github.com/BurntSushi/rebar?tab=readme-ov-file#summa...
- burntsushi 3y agoNote that rebar doesn't include C#'s compile time regexes in the benchmark. The only reason it doesn't is because nobody has implemented it yet. In rebar's model, it will require writing a C# program that compiles another C# program. It's pretty annoying. It's the same reason why CTRE isn't in rebar either. There's nothing preventing it other than the work to make it happen.
- neonsunset 3y agoThis is correct but both source-generated and runtime-compiled regexes should be within the same ballpark performance-wise (if somewhat favoring the former) therefore it's worth a mention :)
- burntsushi 3y agoOh yeah absolutely. Sorry, I only meant to give a very narrow clarification. :-) I just didn't want folks thinking that rebar was providing measurements for C# compile time regexes. You're absolutely right that that might mean compile time regexes are even faster than what rebar reports.
- whizzter 3y agoNOTE re2 is not the same as re2c here re2 is Google's safe regexp engine that works as a state-machine and isn't suspectible to exponential runtime attacks that can affect backtracking engines, it trades some performance for guaranteed non-fatal worst case scenarios. the C# compiled regexps is backtracking (fast by default) JIT compiled searchers so it's extremely fast (on the level of source-compiled matchers).
- burntsushi 3y agoNote that .NET has a non-backtracking engine now too. It's included in the rebar measurements.
- whizzter 3y agoYeah noticed, just pointing out that it's not 2x faster (still faster though and that's impressive!). I implemented an basic DFA based engine like re2 once as an exercise and there was a performance cost (and building it cost a bit) so I wonder if they JIT non backtracking variant also?
- burntsushi 3y agoNot sure. But rust/regex (of which I am the author) is above pcre2/jit in rebar's measurements. Hyperscan is too. All of rust/regex, Hyperscan and RE2 use finite automata, no JIT and no backtracking. There is more to the perf of a regex engine than whether it uses a JIT or not. :) Many of the curated rebar benchmarks (in the README) do not just list numbers. They also try to contextualize those numbers. (I am only one person though with limited expertise, so they tend to bias toward what I know. But still, it's miles better than what you'll see in most benchmarks IMO.) And of course, make sure to read the BIAS document.