Skip to content

Reduce allocations where new WildcardPattern is used #10053

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2114,10 +2114,10 @@ protected override bool PreNewActionEvent(CmdletActionEventArgs args)

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);
Expand All @@ -2138,10 +2138,10 @@ protected override bool PreNewActionEvent(CmdletActionEventArgs args)

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);
Expand All @@ -2162,10 +2162,10 @@ protected override bool PreNewActionEvent(CmdletActionEventArgs args)

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);
Expand Down
14 changes: 6 additions & 8 deletions src/Microsoft.PowerShell.Commands.Diagnostics/GetEventCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -608,11 +608,10 @@ private void ProcessListLog()
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))
||
Expand Down Expand Up @@ -679,11 +678,10 @@ private void ProcessListProvider()
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))
||
Expand Down Expand Up @@ -2059,10 +2057,10 @@ private void FindLogNamesMatchingWildcards(EventLogSession eventLogSession, IEnu
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)))
||
Expand Down Expand Up @@ -2128,10 +2126,10 @@ private void FindProvidersByLogForWildcardPatterns(EventLogSession eventLogSessi
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)))
||
Expand Down
20 changes: 6 additions & 14 deletions src/System.Management.Automation/engine/regex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ public sealed class WildcardPattern
// 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.
Expand All @@ -86,15 +87,8 @@ internal string PatternConvertedToRegex
/// </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>
Expand All @@ -105,13 +99,11 @@ public WildcardPattern(string pattern)
/// <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;
Expand Down