-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Improve cache for match, split, replace operators and WildcardPattern #10657
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
|
/cc @mklement0 if you are interested to see and measure results. |
Add new cache for ignore case Regex-s Use StringComparer.Ordinal in the cache Use the cache in regex.cs
Co-Authored-By: Steve Lee <slee@microsoft.com>
8799f38 to
c3fee9c
Compare
| } | ||
| else | ||
| { | ||
| return subordinateRegexCache.GetOrAdd(patternString, 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.
Having this lambda expression in this method will result in an instance of the helper class to be created every time NewRegex gets called, even if the query can be served by the cache.
This should be changed to the following:
else
{
if (subordinateRegexCache.Count > MaxRegexCache)
{
subordinateRegexCache.Clear();
}
var regex = new Regex(patternString, options);
return subordinateRegexCache.GetOrAdd(patternString, regex);
}Note that, the ValueFactory delegate is called outside the locks and thus it's not atomic, meaning there is no difference clearing the dictionary this way from doing it in the ValueFactory delegate.
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.
Thanks for great comment!
Done.
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.
LGTM
|
🎉 Handy links: |
PR Summary
Fix #8941
PR Context
Before the change we cached only case-insensitive Regex-s (with RegexOptions.IgnoreCase).
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:or[ WIP ]to the beginning of the title (theWIPbot will keep its status check atPendingwhile the prefix is present) and remove the prefix when the PR is ready.