-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Improve Regex performance (in particular RegexOptions.Compiled) #271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
It has a fast path for ASCII.
e.g. avoiding allocating a range list if a class contains only categories, avoiding some intermediary strings, avoiding some delegate allocations for sorting, etc.
...libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/Regex.Timeout.cs
Show resolved
Hide resolved
|
Why is the ctor benchmark quite a bit slower now? |
Compiling a Regex gets slower because we're doing more work during compilation, specifically calling CharInClass 128 times to create the ASCII bitmap. But because we do so, we then get to avoid calling CharInClass for every single character we try to match against a character set every time the Regex is used. Compiling a Regex is already an order of magnitude more expensive than not, so you only do it when you're going to be using the Regex over and over and over and over, in which case this is by far worth the trade-off. |
|
Thank you for the explanation. |
| // to avoid lock: | ||
| CachedCodeEntry? first = s_cacheFirst; | ||
| if (first?.Key == key) | ||
| if (first != null && first.Key.Equals(key)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stephentoub - this will result in a NRE if first.Key is null
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@udlose, Key is a struct; it can't be null.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's what I get for not looking at the code in it's full context. Sorry for the wasted bits.
This is experimental feature that adds about 1.7kB in binary footprint per method returning ValueTask. Ifdef it out for native AOT until we figure out what to do with it. See dotnet#13633.
Recreating #109 after dotnet/runtime was made public.