Skip to content

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 September 24, 2022 19:10
@iSazonov iSazonov marked this pull request as ready for review September 25, 2022 08:18
Copy link
Collaborator Author

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

I suggest splitting PathSearchTrimEnd changes.

@iSazonov iSazonov force-pushed the remove-static branch 2 times, most recently from c5dc0f3 to 05022a1 Compare September 26, 2022 04:54
@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

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.

Copy link
Member

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

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

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

Choose a reason for hiding this comment

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

Done.

@ghost ghost 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
Copy link
Contributor

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

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

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

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

Copy link
Contributor

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

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

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.

if (useLiteralPath && LocationGlobber.StringContainsGlobCharacters(wordToComplete))
if (useLiteralPath)
{
wordToComplete = WildcardPattern.Escape(wordToComplete, Utils.Separators.StarOrQuestion);
Copy link
Collaborator Author

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.

@ghost ghost added the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label Sep 29, 2022
@ghost ghost removed the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label Sep 29, 2022
@xtqqczze
Copy link
Contributor

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

@iSazonov
Copy link
Collaborator Author

iSazonov commented Oct 4, 2022

I don't think it's really worth the changes.

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.

@daxian-dbw
Copy link
Member

daxian-dbw commented Oct 4, 2022

@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 IndexOfAny('\\', '/', ':'), how about we have Separators.DirectoryOrDrive to be const string of "\\/:", and then use it as IndexOfAny(Separator.DirectoryOrDrive)? With this, there would be less changes, which is easier for you to change as well as for others to review. I'm not asking for doing this for all existing members in Separatos, for those with single character values, I think you are right that we don't need to create a const member for them.

Also, it would be less obstacles to get the PR in if there are not semantic changes bundled together, such as the changes to regex.Escape. If you do think that's faster, then get the measurement to prove it and open a separate PR for it. Mixing them in one PR makes it harder to push it through.

@iSazonov
Copy link
Collaborator Author

iSazonov commented Oct 4, 2022

I didn't mean to frustrate you

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.

@iSazonov
Copy link
Collaborator Author

iSazonov commented Oct 5, 2022

Escape and DirectoryOrDrive reverted.

@iSazonov iSazonov mentioned this pull request Oct 6, 2022
22 tasks
@iSazonov
Copy link
Collaborator Author

iSazonov commented Oct 6, 2022

Escape was moved to #18230

@ghost ghost added the Review - Needed The PR is being reviewed label Oct 13, 2022
@ghost
Copy link

ghost commented Oct 13, 2022

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

@pull-request-quantifier-deprecated

This PR has 113 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       : +50 -63
Percentile : 42.6%

Total files changed: 27

Change summary by file extension:
.cs : +50 -63

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? 👍  :ok_hand:  :thumbsdown: (Email)
Customize PullRequestQuantifier for this repository.

Copy link
Member

@daxian-dbw daxian-dbw left a comment

Choose a reason for hiding this comment

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

LGTM

@iSazonov
Copy link
Collaborator Author

@daxian-dbw Could you please merge?

@ghost ghost removed the Review - Needed The PR is being reviewed label Oct 27, 2022
@daxian-dbw daxian-dbw merged commit 8965af3 into PowerShell:master Oct 27, 2022
@daxian-dbw
Copy link
Member

Thanks for the reminder!

@iSazonov iSazonov deleted the remove-static branch October 28, 2022 03:10
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.

3 participants