Skip to content

Improve cache for match, split, replace operators and WildcardPattern - #10657

Merged
Dongbo Wang (daxian-dbw) merged 7 commits into
PowerShell:masterfrom
iSazonov:perf-cmatch-add-cache
Nov 1, 2019
Merged

Dongbo Wang (daxian-dbw) merged 7 commits into
PowerShell:masterfrom
iSazonov:perf-cmatch-add-cache

Conversation

@iSazonov

@iSazonov Ilya (iSazonov) commented Oct 1, 2019 •

Copy link
Copy Markdown
Collaborator

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 Ilya (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 Ilya (iSazonov) added this to the 7.0.0-preview.5 milestone Oct 1, 2019
@iSazonov Ilya (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
Copy Markdown
Collaborator Author

/cc Michael Klement (@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
}
else
{
return subordinateRegexCache.GetOrAdd(patternString, key =>

@daxian-dbw Dongbo Wang (daxian-dbw) Oct 31, 2019 •

Copy link
Copy Markdown
Member

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
Copy Markdown
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

@daxian-dbw Dongbo Wang (daxian-dbw) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@daxian-dbw
Dongbo Wang (daxian-dbw) merged commit 54edaee into PowerShell:master Nov 1, 2019
@iSazonov
Ilya (iSazonov) deleted the perf-cmatch-add-cache branch November 1, 2019 18:28
@ghost

Copy link
Copy Markdown

🎉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