Skip to content
Merged
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
38 changes: 25 additions & 13 deletions src/System.Management.Automation/DscSupport/CimDSCParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -525,9 +525,6 @@ public DscClassCacheEntry(DSCResourceRunAsCredential aDSCResourceRunAsCredential
public static class DscClassCache
{
private const string InboxDscResourceModulePath = "WindowsPowershell\\v1.0\\Modules\\PsDesiredStateConfiguration";
private const string reservedDynamicKeywords = "^(Synchronization|Certificate|IIS|SQL)$";

private const string reservedProperties = "^(Require|Trigger|Notify|Before|After|Subscribe)$";

private static readonly PSTraceSource s_tracer = PSTraceSource.GetTracer("DSC", "DSC Class Cache");

Expand Down Expand Up @@ -1264,13 +1261,6 @@ public static void ValidateInstanceText(string instanceText)
parser.ValidateInstanceText(instanceText);
}

private static bool IsMagicProperty(string propertyName)
{
return System.Text.RegularExpressions.Regex.Match(propertyName,
"^(ResourceId|SourceInfo|ModuleName|ModuleVersion|ConfigurationName)$",
System.Text.RegularExpressions.RegexOptions.IgnoreCase).Success;
}

private static string GetFriendlyName(CimClass cimClass)
{
try
Expand Down Expand Up @@ -1367,7 +1357,8 @@ private static DynamicKeyword CreateKeywordFromCimClass(string moduleName, Versi
//
// Skip all of the base, meta, registration and other classes that are not intended to be used directly by a script author
//
if (System.Text.RegularExpressions.Regex.Match(keywordString, "^OMI_Base|^OMI_.*Registration", System.Text.RegularExpressions.RegexOptions.IgnoreCase).Success)
if (keywordString.StartsWith("OMI_Base", StringComparison.OrdinalIgnoreCase) ||
(keywordString.StartsWith("OMI_", StringComparison.OrdinalIgnoreCase) && keywordString.IndexOf("Registration", 4, StringComparison.OrdinalIgnoreCase) >= 0))
Comment thread
xtqqczze marked this conversation as resolved.
{
return null;
}
Expand All @@ -1383,7 +1374,7 @@ private static DynamicKeyword CreateKeywordFromCimClass(string moduleName, Versi
};

// If it's one of reserved dynamic keyword, mark it
if (System.Text.RegularExpressions.Regex.Match(keywordString, reservedDynamicKeywords, System.Text.RegularExpressions.RegexOptions.IgnoreCase).Success)
if (IsReservedDynamicKeyword(keywordString))
{
keyword.IsReservedKeyword = true;
}
Expand Down Expand Up @@ -1441,7 +1432,7 @@ private static DynamicKeyword CreateKeywordFromCimClass(string moduleName, Versi
}
}
// If it's one of our reserved properties, save it for error reporting
if (System.Text.RegularExpressions.Regex.Match(prop.Name, reservedProperties, System.Text.RegularExpressions.RegexOptions.IgnoreCase).Success)
if (IsReservedProperty(prop.Name))
{
keyword.HasReservedProperties = true;
continue;
Expand Down Expand Up @@ -1540,6 +1531,27 @@ private static DynamicKeyword CreateKeywordFromCimClass(string moduleName, Versi
UpdateKnownRestriction(keyword);

return keyword;

static bool IsMagicProperty(string propertyName) =>
string.Equals(propertyName, "ResourceId", StringComparison.OrdinalIgnoreCase) ||
string.Equals(propertyName, "SourceInfo", StringComparison.OrdinalIgnoreCase) ||
string.Equals(propertyName, "ModuleName", StringComparison.OrdinalIgnoreCase) ||
string.Equals(propertyName, "ModuleVersion", StringComparison.OrdinalIgnoreCase) ||
string.Equals(propertyName, "ConfigurationName", StringComparison.OrdinalIgnoreCase);
Comment on lines +1535 to +1540
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

If this change is just to get rid of the Regex, then it doesn't make sense.
If it's about performance, then I suspect that Regex makes better optimizations than this change. And we need measurements to prove that there is a significant increase in parser performance.

Copy link
Copy Markdown
Contributor Author

@xtqqczze xtqqczze Oct 27, 2025

Choose a reason for hiding this comment

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

Avoiding Regex can make significant improvement:

Method propertyName Mean Error Median Ratio
IsMagicProperty_GeneratedRegex ConfigurationName 17.068 ns 0.0866 ns 17.056 ns 9.66
IsMagicProperty_StringEquals ConfigurationName 1.768 ns 0.0382 ns 1.772 ns 1.00
IsMagicProperty_CompiledRegex ConfigurationName 66.315 ns 1.3416 ns 65.370 ns 37.52
IsMagicProperty_RegexIsMatch ConfigurationName 127.821 ns 1.7693 ns 127.007 ns 72.32
IsMagicProperty_Regex ConfigurationName 157.995 ns 3.1062 ns 156.249 ns 89.40


static bool IsReservedDynamicKeyword(string keyword) =>
string.Equals(keyword, "Synchronization", StringComparison.OrdinalIgnoreCase) ||
string.Equals(keyword, "Certificate", StringComparison.OrdinalIgnoreCase) ||
string.Equals(keyword, "IIS", StringComparison.OrdinalIgnoreCase) ||
string.Equals(keyword, "SQL", StringComparison.OrdinalIgnoreCase);

static bool IsReservedProperty(string name) =>
string.Equals(name, "Require", StringComparison.OrdinalIgnoreCase) ||
string.Equals(name, "Trigger", StringComparison.OrdinalIgnoreCase) ||
string.Equals(name, "Notify", StringComparison.OrdinalIgnoreCase) ||
string.Equals(name, "Before", StringComparison.OrdinalIgnoreCase) ||
string.Equals(name, "After", StringComparison.OrdinalIgnoreCase) ||
string.Equals(name, "Subscribe", StringComparison.OrdinalIgnoreCase);
}

/// <summary>
Expand Down
Loading