8000 Improve cache for match, split, replace operators and WildcardPattern by iSazonov · Pull Request #10657 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content

Conversation

@iSazonov
Copy link
Collaborator
@iSazonov iSazonov commented Oct 1, 2019

PR Summary

Fix #8941

  • Add new cache for case-sensitive Regex-s
  • Use StringComparer.Ordinal in the cache (that will slightly speed up and reduce allocations)
  • Use the cache in regex.cs (that will slightly speed up cmdlets)
  • Make the cache two-level to take in account all regex option combinations.

PR Context

Before the change we cached only case-insensitive Regex-s (with RegexOptions.IgnoreCase).

PR Checklist

@iSazonov iSazonov added the CL-Performance Indicates that a PR should be marked as a performance improvement in the Change Log label Oct 1, 2019
@iSazonov iSazonov added this to the 7.0.0-preview.5 milestone Oct 1, 2019
@iSazonov iSazonov changed the title Improve cache for match, split and replace operators and WildcardPattern Improve cache for match, split, replace operators and WildcardPattern Oct 1, 2019
@iSazonov
Copy link
Collaborator Author
iSazonov commented Oct 1, 2019

/cc @mklement0 if you are interested to see and measure results.

@ghost ghost added Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept and removed Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept labels Oct 1, 2019
@iSazonov iSazonov force-pushed the perf-cmatch-add-cache branch from 8799f38 to c3fee9c Compare October 21, 2019 12:11
}
else
{
return subordinateRegexCache.GetOrAdd(patternString, key =>
Copy link
Member
@daxian-dbw daxian-dbw Oct 31, 2019

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.

Copy link
Collaborator Author

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.

@ghost ghost added the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label Oct 31, 2019
@ghost ghost removed the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label Nov 1, 2019
Copy link
Member
@daxian-dbw daxian-dbw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@daxian-dbw daxian-dbw merged commit 54edaee into PowerShell:master Nov 1, 2019
@iSazonov iSazonov deleted the perf-cmatch-add-cache branch November 1, 2019 18:28
@ghost
Copy link
ghost commented Nov 21, 2019

🎉v7.0.0-preview.6 has been released which incorporates this pull request.:tada:

Handy links:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-Performance Indicates that a PR should be marked as a performance improvement in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Performance problem: -cmatch prevents automatic caching of on-demand-compiled regexes

4 participants

0