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
Avoid using regex when unnecessary in ScriptWriter
#18348
Conversation
src/System.Management.Automation/cimSupport/cmdletization/ScriptWriter.cs
Outdated
Show resolved
Hide resolved
src/System.Management.Automation/cimSupport/cmdletization/ScriptWriter.cs
Show resolved
Hide resolved
src/System.Management.Automation/cimSupport/cmdletization/ScriptWriter.cs
Outdated
Show resolved
Hide resolved
|
This PR has Quantification details
Why proper sizing of changes matters
Optimal pull request sizes drive a better predictable PR flow as they strike a
What can I do to optimize my changes
How to interpret the change counts in git diff output
Was this comment helpful? |
| // Now we have to fall back to the expensive regular expression matching, because 'PSType' | ||
| // could be a composite type like 'Nullable<enum_name>' or 'Dictionary<enum_name, object>', | ||
| // but we don't want the case where the enum name is part of another type's name. | ||
| matchFound = Regex.IsMatch(psType, $@"\b{Regex.Escape(e.EnumName)}\b"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: we could build single regex pattern as enumName1 | enumName2 | ... | enumName№ - I believe one Regex is more faster than many. I mean we build the pattern in the loop and move the Regex out the loop.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it could reduce the number of regex's created for the match, which would help a lot if regex matching were a must-have.
In this case, the common cases handled above will cover nearly all scenarios practically, so I think we just keep it simple when it comes to regex matching.
PR Summary
The
Regex.IsMatchinGetDotNetTypeis pretty expensive. Actually, in most cases, we can use simpler checks to get what we need. Handle those common cases so as to avoid regular expression checks when possible.PR Context
It's reported from MS internal team the
GetDotNetTypemethod call results in tons of regular expression creations, taking up a lot of CPU usage.PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:or[ WIP ]to the beginning of the title (theWIPbot will keep its status check atPendingwhile the prefix is present) and remove the prefix when the PR is ready.