-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Remove static constants #18154
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
Remove static constants #18154
Conversation
3c8bb42 to
1c39590
Compare
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.
It is requested by analyzer.
|
I suggest splitting |
c5dc0f3 to
05022a1
Compare
|
We could avoid some string allocations with the following |
daxian-dbw
left a comment
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.
Left 2 comments. A lot of changes will be gone once addressing them, so I will wait after that to review the rest changes.
src/Microsoft.PowerShell.Security/security/SecureStringCommands.cs
Outdated
Show resolved
Hide resolved
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.
This is less readable than the existing code.
- You will need to reason that
"\r\n"will be cast toReadOnlySpanand then the chars are used instead of the string itself. Also, I don't think the saving is measurable. - It's helpful to keep all separators together in
Utils.Separators, for looking up and referencing.
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.
We can get better codegen using the char overloads:
| var index = stringToComplete.AsSpan().IndexOfAny("\r\n"); | |
| var index = stringToComplete.AsSpan().IndexOfAny('\r', '\n'); |
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.
Yes, we should use this overload.
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.
Done.
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.
| if (_commandOrigin == CommandOrigin.Runspace && _commandName.AsSpan().IndexOfAny("\\/:") >= 0) | |
| if (_commandOrigin == CommandOrigin.Runspace && _commandName.AsSpan().IndexOfAny('\\', '/', ':') >= 0) |
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.
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.
It is a question for .Net team why these options have so different codegen.
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.
One reason must be IndexOfAny("a-string") requires implicit casting from string to ReadOnlySpan<char>.
We should use the explicit overloads instead.
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.
Done.
I opened dotnet/runtime#76354
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.
| var secondBackslash = moduleCommandName.IndexOf('\\'); | |
| int secondBackslash = moduleCommandName.IndexOf('\\'); |
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.
This PR already touches this line so we can fix code style too?
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.
There is an enormous number of var. It is better to fix in another PR if we would want.
src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs
Outdated
Show resolved
Hide resolved
| if (useLiteralPath && LocationGlobber.StringContainsGlobCharacters(wordToComplete)) | ||
| if (useLiteralPath) | ||
| { | ||
| wordToComplete = WildcardPattern.Escape(wordToComplete, Utils.Separators.StarOrQuestion); |
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.
You point to removed line.
This reverts commit d0eccf5.
src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs
Outdated
Show resolved
Hide resolved
src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs
Show resolved
Hide resolved
|
I suggest changes to |
src/System.Management.Automation/engine/NativeCommandProcessor.cs
Outdated
Show resolved
Hide resolved
src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs
Outdated
Show resolved
Hide resolved
My intention was to remove static initializations (not just these) that slow down startup. If you don't find it useful, you can close this PR. Sorry for wasting your time. |
|
@iSazonov I didn't mean to frustrate you and I absolutely appreciate your intention for starting this work (this is not a simple change and you obviously put lots of effort). I do agree with the gist of your idea, and that's why I spent time going through the changes here. But we need some compromises, for example, for Also, it would be less obstacles to get the PR in if there are not semantic changes bundled together, such as the changes to |
I know :-) It's easier if you use the commits you find useful. You can delete the rest. Disagreement about personal preferences is not something worth wasting our time on. |
|
Escape and DirectoryOrDrive reverted. |
|
Escape was moved to #18230 |
|
This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days. |
|
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? 👍 :ok_hand: :thumbsdown: (Email) |
daxian-dbw
left a comment
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.
LGTM
|
@daxian-dbw Could you please merge? |
|
Thanks for the reminder! |
PR Summary
Please review commit by commit.
Historically PowerShell code contains a lot of static constants because of limitations in old .Net APIs. For example, now we have
string.Split(char)and can use it instead ofstring.Split(char[]).I had to refactor some methods.
Escape()method now is more short, simple, clear and fast - now we exactly point chars we want to escape.(for ex., now we don't remove trail spaces that are valid in file names)
PR Context
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.(which runs in a different PS Host).