10000 Filter out compiler generated types for `Add-Type -PassThru` by daxian-dbw · Pull Request #18095 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content

Filter out compiler generated types for Add-Type -PassThru #18095

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

Merged
merged 2 commits into from
Sep 15, 2022

Conversation

daxian-dbw
Copy link
Member
@daxian-dbw daxian-dbw commented Sep 14, 2022

PR Summary

Only return types that are not auto-generated by compiler with Add-Type -PassThru.

An improvement was made to Microsoft.CodeAnalysis.CSharp to allow compiler to generate private helper class sometimes for performance reason (see an example below). Passing through all types from the assembly cause one of our tests to fail:

https://dev.azure.com/powershell/PowerShell/_build/results?buildId=108821&view=logs&j=77295986-da6b-5a15-d60e-dfb16acd57ef&t=a58f8c58-10a4-5f19-f6fe-df1d6acdd5db&l=685

This PR makes -PassThru to only return types that are not compiler generated. It's technically a breaking change, but should be in "Bucket 3 - gray area".

$classDefinition = @'
using System;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Threading;
namespace StackTest {
    public class StackDepthTest {
        public static PowerShell ps;
        public static int size = 512 * 1024;
        public static void CauseError() {
            Thread t = new Thread(RunPS, size);
            t.Start();
            t.Join();
        }

        public static void RunPS() {
            InitialSessionState iss = InitialSessionState.CreateDefault2();
            iss.ThreadOptions = PSThreadOptions.UseCurrentThread;
            ps = PowerShell.Create(iss);
            ps.AddScript("function recurse { recurse }; recurse").Invoke();
        }

        public static void GetPSError() {
            if ( ps.Streams.Error.Count > 0) {
                throw ps.Streams.Error[0].Exception.InnerException;
            }
        }
    }
}
'@

Add-Type -PassThru -TypeDefinition $classDefinition

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    StackDepthTest                           System.Object
False    False    <>O                                      System.Object

PR Checklist

@daxian-dbw daxian-dbw added CL-BreakingChange Indicates that a PR should be marked as a breaking change in the Change Log CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log labels Sep 14, 2022
@daxian-dbw daxian-dbw self-assigned this Sep 14, 2022
@daxian-dbw
Copy link
Member Author

@SeeminglyScience Can you please take a look and see if this will cause any problem to VSCode Extension?

@daxian-dbw daxian-dbw added the Review - Committee The PR/Issue needs a review from the PowerShell Committee label Sep 14, 2022
@SeeminglyScience
Copy link
Collaborator

@SeeminglyScience Can you please take a look and see if this will cause any problem to VSCode Extension?

I think all is good on the VSCode side 🎉

Though, I can imagine someone generating p/invokes as internal and doing (Add-Type -Passthru)[0]::PInvokeName() or something like that to avoid polluting type resolution.

Would definitely be slower, but maybe worth considering looking for CompilerGeneratedAttribute decorations as a filter

@iSazonov
Copy link
Collaborator

A breaking change is made to new versions of Microsoft.CodeAnalysis.CSharp to make the compiler generate private helper class sometimes (see an example below).

Looks like a bug. Why should we hide it?

The fix looks like very sensitive breaking change in the cmdlet.

Copy link
Contributor
@PaulHigin PaulHigin left a comment

Choose a reason for hiding this comment

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

LGTM

@SeeminglyScience
Copy link
Collaborator

A breaking change is made to new versions of Microsoft.CodeAnalysis.CSharp to make the compiler generate private helper class sometimes (see an example below).

Looks like a bug. Why should we hide it?

The fix looks like very sensitive breaking change in the cmdlet.

Roslyn has always created private helper types in certain conditions, typically for performance reasons. I don't think they would consider this change breaking, they expect all tooling that reads types to ignore anything decorated with CompilerGeneratedAttribute when the exclusion of them is required.

@iSazonov
Copy link
Collaborator

A breaking change is made to new versions of Microsoft.CodeAnalysis.CSharp to make the compiler generate private helper class sometimes (see an example below).

Looks like a bug. Why should we hide it?
The fix looks like very sensitive breaking change in the cmdlet.

Roslyn has always created private helper types in certain conditions, typically for performance reasons. I don't think they would consider this change breaking, they expect all tooling that reads types to ignore anything decorated with CompilerGeneratedAttribute when the exclusion of them is required.

Maybe they lost the attribute for member we see? :-)

And your comment raises the same question - why should we hide if they don't?

@pull-request-quantifier-deprecated

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


Quantification details

Label      : Extra Small
Size       : +4 -1
Percentile : 2%

Total files changed: 1

Change summary by file extension:
.cs : +4 -1

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.

@daxian-dbw
Copy link
Member Author
daxian-dbw commented Sep 15, 2022

Would definitely be slower, but maybe worth considering looking for CompilerGeneratedAttribute decorations as a filter

I like this👍
It's about 60 ms slower when filtering on CompilerGeneratedAttribute than calling GetTypes()/ExportedTypes for the assembly System.Management.Automation. But in practice, I believe when -PassThru is specified, the assembly to be inspected will most likely contain a small number of types, so the additional perf cost won't be much.

Copy link
Collaborator
@SeeminglyScience SeeminglyScience left a comment

Choose a reason for hiding this comment

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

LGTM! ❤️

@daxian-dbw
Copy link
Member Author
daxian-dbw commented Sep 15, 2022

Maybe they lost the attribute for member we see? :-)
And your comment raises the same question - why should we hide if they don't?

Nope, nothing is hidden. It's an improvement to the emitted code.

  • The assembly generated from the same source code in PS 7.2.6 doesn't do any optimization around wrapping RunPS to a delegate -- every time CauseError is called, the static method RunPS will be wrapped to a new delegate.
  • With the improvement in the new Microsoft.CodeAnalysis.CSharp, a helper class is generated to hold the delegate, so the delegate gets created only once.

I have updated the PR description to say it's an improvement, instead of a breaking change :)

@daxian-dbw daxian-dbw changed the title Only return public types with Add-Type -PassThru Filter out compiler generated types for Add-Type -PassThru Sep 15, 2022
@adityapatwardhan adityapatwardhan merged commit d0a9147 into PowerShell:master Sep 15, 2022
@daxian-dbw daxian-dbw removed Review - Committee The PR/Issue needs a review from the PowerShell Committee CL-BreakingChange Indicates that a PR should be marked as a breaking change in the Change Log labels Sep 15, 2022
@daxian-dbw daxian-dbw deleted the addtype branch September 15, 2022 20:59
@adityapatwardhan
Copy link
Member

/backport to release/v7.3.0-rc.1

@ghost
Copy link
ghost commented Sep 20, 2022

🎉v7.3.0-preview.8 has been released which incorporates this pull request.:tada:

Handy links:

@TravisEz13 TravisEz13 mentioned this pull request Sep 30, 2022
22 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backport-7.3.x-Done CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log Extra Small
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants
0