X Tutup
The Wayback Machine - https://web.archive.org/web/20221003164249/https://github.com/PowerShell/PowerShell/pull/18154
Skip to content
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

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open

Conversation

iSazonov
Copy link
Collaborator

@iSazonov iSazonov commented Sep 24, 2022

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 of string.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.
  • GetModuleNameAndVersion() method is refactored a bit. Since all paths have been collected from file system it makes no sense to take into account alternative directory separator.
  • PathSearchTrimEnd is removed based on Why TrimEndChars was removed? dotnet/runtime#76122 (comment)
    (for ex., now we don't remove trail spaces that are valid in file names)

PR Context

PR Checklist

@iSazonov iSazonov added the CL-Performance Indicates that a PR should be marked as a performance improvement in the Change Log label Sep 24, 2022
@iSazonov iSazonov force-pushed the remove-static branch 5 times, most recently from 3c8bb42 to 1c39590 Compare Sep 24, 2022
@iSazonov iSazonov marked this pull request as ready for review Sep 25, 2022
@@ -1473,7 +1473,7 @@ private static CanDoPathLookupResult CanDoPathLookup(string possiblePath)
/// <summary>
/// The command name to search for.
/// </summary>
private string _commandName;
private readonly string _commandName;
Copy link
Collaborator Author

@iSazonov iSazonov Sep 25, 2022

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.

@xtqqczze
Copy link
Contributor

xtqqczze commented Sep 26, 2022

I suggest splitting PathSearchTrimEnd changes.

@iSazonov iSazonov force-pushed the remove-static branch 2 times, most recently from c5dc0f3 to 05022a1 Compare Sep 26, 2022
@xtqqczze
Copy link
Contributor

xtqqczze commented Sep 26, 2022

We could avoid some string allocations with the following IndexOfAny<T> overloads, available since .NET Core 2.1:

Copy link
Member

@daxian-dbw daxian-dbw left a comment

Left 2 comments. A lot of changes will be gone once addressing them, so I will wait after that to review the rest changes.

@@ -1263,7 +1263,7 @@ private static string GetFirstLineSubString(string stringToComplete, out bool ha
hasNewLine = false;
if (!string.IsNullOrEmpty(stringToComplete))
{
var index = stringToComplete.IndexOfAny(Utils.Separators.CrLf);
var index = stringToComplete.AsSpan().IndexOfAny("\r\n");
Copy link
Member

@daxian-dbw daxian-dbw Sep 26, 2022

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.

  1. You will need to reason that "\r\n" will be cast to ReadOnlySpan and then the chars are used instead of the string itself. Also, I don't think the saving is measurable.
  2. It's helpful to keep all separators together in Utils.Separators, for looking up and referencing.

Copy link
Contributor

@xtqqczze xtqqczze Sep 28, 2022

Choose a reason for hiding this comment

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

@daxian-dbw

We can get better codegen using the char overloads:

Suggested change
var index = stringToComplete.AsSpan().IndexOfAny("\r\n");
var index = stringToComplete.AsSpan().IndexOfAny('\r', '\n');

https://sharplab.io/#v2:C4LghgzgtgPgAgJgIwFgBQcDMACR2DC2A3utmbjgJYB2w2AsgAwAUcSj2EAlMaef3ADsnAHQBBCAGUADmGrMuIgJLUAJgFMAHgHkAZmOoBPZgCIAOgCcz1E1wDcfMgF9HFbDTr0krdpx4k0fgFhCHEpWXlFFQ0dfSNmAHJLBIAabCTqBPtXFzQnIA===

Copy link
Member

@daxian-dbw daxian-dbw Sep 28, 2022

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.

Copy link
Collaborator Author

@iSazonov iSazonov Sep 29, 2022

Choose a reason for hiding this comment

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

Done.

@msftbot msftbot bot added Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept and removed Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept labels Sep 26, 2022
@iSazonov iSazonov mentioned this pull request Sep 27, 2022
22 tasks
@@ -140,7 +140,7 @@ public bool MoveNext()

// For security reasons, if the command is coming from outside the runspace and it looks like a path,
// we want to pre-check that path before doing any probing of the network or drives
if (_commandOrigin == CommandOrigin.Runspace && _commandName.IndexOfAny(Utils.Separators.DirectoryOrDrive) >= 0)
if (_commandOrigin == CommandOrigin.Runspace && _commandName.AsSpan().IndexOfAny("\\/:") >= 0)
Copy link
Contributor

@xtqqczze xtqqczze Sep 28, 2022

Choose a reason for hiding this comment

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

Suggested change
if (_commandOrigin == CommandOrigin.Runspace && _commandName.AsSpan().IndexOfAny("\\/:") >= 0)
if (_commandOrigin == CommandOrigin.Runspace && _commandName.AsSpan().IndexOfAny('\\', '/', ':') >= 0)

Copy link
Contributor

@xtqqczze xtqqczze Sep 28, 2022

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

@iSazonov iSazonov Sep 28, 2022

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.

Copy link
Member

@daxian-dbw daxian-dbw Sep 28, 2022

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.

Copy link
Collaborator Author

@iSazonov iSazonov Sep 29, 2022

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

@@ -1150,7 +1150,7 @@ private static CommandInfo TryModuleAutoLoading(string commandName, ExecutionCon
string moduleName;

// Now we check if there exists the second '\'
var secondBackslash = moduleCommandName.IndexOfAny(Utils.Separators.Backslash);
var secondBackslash = moduleCommandName.IndexOf('\\');
Copy link
Contributor

@xtqqczze xtqqczze Sep 28, 2022

Choose a reason for hiding this comment

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

Suggested change
var secondBackslash = moduleCommandName.IndexOf('\\');
int secondBackslash = moduleCommandName.IndexOf('\\');

Copy link
Contributor

@xtqqczze xtqqczze Sep 29, 2022

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?

Copy link
Collaborator Author

@iSazonov iSazonov Sep 29, 2022

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.

{
wordToComplete = WildcardPattern.Escape(wordToComplete, Utils.Separators.StarOrQuestion);
Copy link
Collaborator Author

@iSazonov iSazonov Sep 29, 2022

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.

@@ -1536,7 +1536,6 @@ private void setupPathSearcher()
if (_canDoPathLookupResult == CanDoPathLookupResult.Yes)
{
_canDoPathLookup = true;
_commandName = _commandName.TrimEnd(Utils.Separators.PathSearchTrimEnd);
Copy link
Member

@daxian-dbw daxian-dbw Sep 29, 2022

Choose a reason for hiding this comment

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

Please revert changes related to TrimEnd since it was moved to #18166.

Copy link
Collaborator Author

@iSazonov iSazonov Sep 29, 2022

Choose a reason for hiding this comment

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

Done.

@msftbot msftbot bot added the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label Sep 29, 2022
@msftbot msftbot bot removed the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label Sep 29, 2022
@pull-request-quantifier
Copy link

pull-request-quantifier bot commented Sep 29, 2022

This PR has 175 quantified lines of changes. In general, a change size of upto 200 lines is ideal for the best PR experience!


Quantification details

Label      : Medium
Size       : +82 -93
Percentile : 55%

Total files changed: 30

Change summary by file extension:
.cs : +82 -93

Change counts above are quantified counts, based on the PullRequestQuantifier customizations.

Why proper sizing of changes matters

Optimal pull request sizes drive a better predictable PR flow as they strike a
balance between between PR complexity and PR review overhead. PRs within the
optimal size (typical small, or medium sized PRs) mean:

  • Fast and predictable releases to production:
    • Optimal size changes are more likely to be reviewed faster with fewer
      iterations.
    • Similarity in low PR complexity drives similar review times.
  • Review quality is likely higher as complexity is lower:
    • Bugs are more likely to be detected.
    • Code inconsistencies are more likely to be detected.
  • Knowledge sharing is improved within the participants:
    • Small portions can be assimilated better.
  • Better engineering practices are exercised:
    • Solving big problems by dividing them in well contained, smaller problems.
    • Exercising separation of concerns within the code changes.

What can I do to optimize my changes

  • Use the PullRequestQuantifier to quantify your PR accurately
    • Create a context profile for your repo using the context generator
    • Exclude files that are not necessary to be reviewed or do not increase the review complexity. Example: Autogenerated code, docs, project IDE setting files, binaries, etc. Check out the Excluded section from your prquantifier.yaml context profile.
    • Understand your typical change complexity, drive towards the desired complexity by adjusting the label mapping in your prquantifier.yaml context profile.
    • Only use the labels that matter to you, see context specification to customize your prquantifier.yaml context profile.
  • Change your engineering behaviors
    • For PRs that fall outside of the desired spectrum, review the details and check if:
      • Your PR could be split in smaller, self-contained PRs instead
      • Your PR only solves one particular issue. (For example, don't refactor and code new features in the same PR).

How to interpret the change counts in git diff output

  • One line was added: +1 -0
  • One line was deleted: +0 -1
  • One line was modified: +1 -1 (git diff doesn't know about modified, it will
    interpret that line like one addition plus one deletion)
  • Change percentiles: Change characteristics (addition, deletion, modification)
    of this PR in relation to all other PRs within the repository.


Was this comment helpful? 👍  👌  👎 (Email)
Customize PullRequestQuantifier for this repository.

{
wordToComplete = WildcardPattern.Escape(wordToComplete, Utils.Separators.StarOrQuestion);
wordToComplete = WildcardPattern.Escape(wordToComplete, "[]");
Copy link
Contributor

@xtqqczze xtqqczze Sep 29, 2022

Choose a reason for hiding this comment

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

@iSazonov Why is Utils.Separators.StarOrQuestion replaced with "[]", not "*?"

@@ -905,7 +905,7 @@ internal List<CompletionResult> GetResultHelper(CompletionContext completionCont
{
switch (completionContext.TokenBeforeCursor.Kind)
{

Copy link
Contributor

@xtqqczze xtqqczze Sep 29, 2022

Choose a reason for hiding this comment

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

Can we run dotnet-format on the repo to fix whitespace, so we don't see these changes in PRs?

Copy link
Collaborator Author

@iSazonov iSazonov Sep 30, 2022

Choose a reason for hiding this comment

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

We can 😄

@xtqqczze
Copy link
Contributor

xtqqczze commented Sep 29, 2022

I suggest changes to CompletionCompleters.NativeCompletionVariableCommands are split as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CL-Performance Indicates that a PR should be marked as a performance improvement in the Change Log Medium
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants
X Tutup