Move some of the creations of WildcardPattern in outer loop to avoid unnecessary allocation (#10053)

This commit is contained in:
Ilya
2019-07-10 09:06:34 -07:00
committed by Dongbo Wang
parent 7485b01b2f
commit 2ff65c100b
3 changed files with 15 additions and 25 deletions
@@ -2114,10 +2114,10 @@ namespace Microsoft.Management.Infrastructure.CimCmdlets
if (context.PropertyName != null)
{
pattern = new WildcardPattern(context.PropertyName, WildcardOptions.IgnoreCase);
bool match = false;
if (cimClass.CimClassProperties != null)
{
pattern = new WildcardPattern(context.PropertyName, WildcardOptions.IgnoreCase);
foreach (CimPropertyDeclaration decl in cimClass.CimClassProperties)
{
DebugHelper.WriteLog("--- property name : {0}", 1, decl.Name);
@@ -2138,10 +2138,10 @@ namespace Microsoft.Management.Infrastructure.CimCmdlets
if (context.MethodName != null)
{
pattern = new WildcardPattern(context.MethodName, WildcardOptions.IgnoreCase);
bool match = false;
if (cimClass.CimClassMethods != null)
{
pattern = new WildcardPattern(context.MethodName, WildcardOptions.IgnoreCase);
foreach (CimMethodDeclaration decl in cimClass.CimClassMethods)
{
DebugHelper.WriteLog("--- method name : {0}", 1, decl.Name);
@@ -2162,10 +2162,10 @@ namespace Microsoft.Management.Infrastructure.CimCmdlets
if (context.QualifierName != null)
{
pattern = new WildcardPattern(context.QualifierName, WildcardOptions.IgnoreCase);
bool match = false;
if (cimClass.CimClassQualifiers != null)
{
pattern = new WildcardPattern(context.QualifierName, WildcardOptions.IgnoreCase);
foreach (CimQualifier qualifier in cimClass.CimClassQualifiers)
{
DebugHelper.WriteLog("--- qualifier name : {0}", 1, qualifier.Name);
@@ -608,11 +608,10 @@ namespace Microsoft.PowerShell.Commands
foreach (string logPattern in _listLog)
{
bool bMatchFound = false;
WildcardPattern wildLogPattern = new WildcardPattern(logPattern, WildcardOptions.IgnoreCase);
foreach (string logName in eventLogSession.GetLogNames())
{
WildcardPattern wildLogPattern = new WildcardPattern(logPattern, WildcardOptions.IgnoreCase);
if (((!WildcardPattern.ContainsWildcardCharacters(logPattern))
&& string.Equals(logPattern, logName, StringComparison.CurrentCultureIgnoreCase))
||
@@ -679,11 +678,10 @@ namespace Microsoft.PowerShell.Commands
foreach (string provPattern in _listProvider)
{
bool bMatchFound = false;
WildcardPattern wildProvPattern = new WildcardPattern(provPattern, WildcardOptions.IgnoreCase);
foreach (string provName in eventLogSession.GetProviderNames())
{
WildcardPattern wildProvPattern = new WildcardPattern(provPattern, WildcardOptions.IgnoreCase);
if (((!WildcardPattern.ContainsWildcardCharacters(provPattern))
&& string.Equals(provPattern, provName, StringComparison.CurrentCultureIgnoreCase))
||
@@ -2059,10 +2057,10 @@ namespace Microsoft.PowerShell.Commands
foreach (string logPattern in logPatterns)
{
bool bMatched = false;
WildcardPattern wildLogPattern = new WildcardPattern(logPattern, WildcardOptions.IgnoreCase);
foreach (string actualLogName in eventLogSession.GetLogNames())
{
WildcardPattern wildLogPattern = new WildcardPattern(logPattern, WildcardOptions.IgnoreCase);
if (((!WildcardPattern.ContainsWildcardCharacters(logPattern))
&& (logPattern.Equals(actualLogName, StringComparison.CurrentCultureIgnoreCase)))
||
@@ -2128,10 +2126,10 @@ namespace Microsoft.PowerShell.Commands
foreach (string provPattern in providerPatterns)
{
bool bMatched = false;
WildcardPattern wildProvPattern = new WildcardPattern(provPattern, WildcardOptions.IgnoreCase);
foreach (string provName in eventLogSession.GetProviderNames())
{
WildcardPattern wildProvPattern = new WildcardPattern(provPattern, WildcardOptions.IgnoreCase);
if (((!WildcardPattern.ContainsWildcardCharacters(provPattern))
&& (provPattern.Equals(provName, StringComparison.CurrentCultureIgnoreCase)))
||
@@ -65,8 +65,9 @@ namespace System.Management.Automation
// wildcard pattern
internal string Pattern { get; }
// options that control match behavior
internal WildcardOptions Options { get; } = WildcardOptions.None;
// Options that control match behavior.
// Default is WildcardOptions.None.
internal WildcardOptions Options { get; }
/// <summary>
/// Wildcard pattern converted to regex pattern.
@@ -86,15 +87,8 @@ namespace System.Management.Automation
/// </summary>
/// <param name="pattern">The wildcard pattern to match.</param>
/// <returns>The constructed WildcardPattern object.</returns>
/// <remarks> if wildCardType == None, the pattern does not have wild cards</remarks>
public WildcardPattern(string pattern)
public WildcardPattern(string pattern) : this(pattern, WildcardOptions.None)
{
if (pattern == null)
{
throw PSTraceSource.NewArgumentNullException("pattern");
}
Pattern = pattern;
}
/// <summary>
@@ -105,13 +99,11 @@ namespace System.Management.Automation
/// <param name="pattern">The wildcard pattern to match.</param>
/// <param name="options">Wildcard options.</param>
/// <returns>The constructed WildcardPattern object.</returns>
/// <remarks> if wildCardType == None, the pattern does not have wild cards </remarks>
public WildcardPattern(string pattern,
WildcardOptions options)
public WildcardPattern(string pattern, WildcardOptions options)
{
if (pattern == null)
{
throw PSTraceSource.NewArgumentNullException("pattern");
throw PSTraceSource.NewArgumentNullException(nameof(pattern));
}
Pattern = pattern;