Skip to content

Move Windows pinvokes #18603

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 15 commits into from
Nov 30, 2022
Merged

Conversation

iSazonov
Copy link
Collaborator

@iSazonov iSazonov commented Nov 17, 2022

PR Summary

Please review commit by commit.

Contributes to #18553.

  1. Move Windows pinvokes (migrated to LibraryImport in Replace DllImport with LibraryImport in engine #18496 and Replace DllImport with LibraryImport in SMA 1 #18520) to new location.
  2. Exclude from compile in csproj instead of using #if-#endif

PR Context

PR Checklist

@iSazonov iSazonov added the CL-CodeCleanup Indicates that a PR should be marked as a Code Cleanup change in the Change Log label Nov 17, 2022
@ghost ghost assigned daxian-dbw Nov 17, 2022
@jborean93
Copy link
Collaborator

I really don't mind the format that is ultimately used but it would be nice if there was a common standard set out by the pwsh team for PInvoke calls and how to structure them. I moved some of the WinTrust stuff in the PR #17545 and went with the format of

  • Single file per dll
  • Simple wrapper method that wraps the extern call with a more managed interface
    • Makes it simpler for multiple locations to call the same function but without having to add things like error checking in each reference

Here you've moved it to a single file per function and it's just the extern def.

I'm totally not opposed to any route we ultimately go for but it would be nice to choose one rather than fragmenting it even more.

@daxian-dbw
Copy link
Member

daxian-dbw commented Nov 18, 2022

I didn't realize it's organized as one file per function. Wouldn't that be too many files?
I personally prefer to single file per DLL.

@iSazonov
Copy link
Collaborator Author

The only inconvenience that many files might cause is if you tried to open them all at once in the editor. But why would you want to do that?
You will never be able to keep order in one big file. People will prefer IntelliSense to nested region directives.

@daxian-dbw
Copy link
Member

daxian-dbw commented Nov 20, 2022

An inconvenience off the top of my head is that it would be harder to search for something related to a p/invoke call, be it the use of a marshal attribute, a parameter type, or a function when you only remember partial of its name.

I personally prefer to have only a few files dedicated to all p/invokes. In case that a lot of functions get used from a dll, like the WinTrust stuff, then use a single file for that dll. For the rest, use a single file per platform if possible. This doesn't prevent you from using partial classes for all p/invokes of a specific platform.

@iSazonov
Copy link
Collaborator Author

An inconvenience off the top of my head is that it would be harder to search for something related to a p/invoke call, be it the use of a marshal attribute, a parameter type, or a function when you only remember partial of its name.

How is a partial search related to number of files? VS Code has great and very fast multi file search (Ctrl-Shift-F). Given how unpredictable the location of code in PowerShell is sometimes, I seem to use it more often than the single-file search.

I personally prefer to have only a few files dedicated to all p/invokes. In case that a lot of functions get used from a dll, like the WinTrust stuff, then use a single file for that dll. For the rest, use a single file per platform if possible. This doesn't prevent you from using partial classes for all p/invokes of a specific platform.

It's impossible to maintain any order in these files, especially since you're already talking about exceptions. These files are already unnecessarily huge. In fact, the code does not fit on one screen and you will anyway be forced to use search and possibly use additional editor windows even for the same file.
Moreover, this code is leaf and stable, ie rarely defeated change - for this reason alone it should have been removed from these big files.

@daxian-dbw
Copy link
Member

Since @SteveL-MSFT agreed to adopt the .NET style for organizing the p/invokes in another PR, I will stop arguing for my personal preference.

Just a side note, for existing code like WinTrust.cs, you may want to keep all those p/invoke in a single file. Splitting them into 1 file per function is lots of extra work, and also may be troublesome in case that some structs are shared by multiple functions.

@iSazonov
Copy link
Collaborator Author

@daxian-dbw
I expect that the .Net team will actively improve LibraryImport in .Net 8. This is another argument for removing pinvokes and related code from the main code and sticking to their style.
Once they finish this work this code will be so stable that it can be placed in just one file, or even in a separate repository.

By the way, they sometimes put related pinvokes in the same file. The main convenience for the current work.

@iSazonov iSazonov force-pushed the move-windows-pinvokes branch from cef6e8c to b761cce Compare November 23, 2022 11:55
@iSazonov iSazonov force-pushed the move-windows-pinvokes branch from b761cce to 9b2d61f Compare November 23, 2022 12:15
@iSazonov iSazonov force-pushed the move-windows-pinvokes branch from 03b9b22 to db89486 Compare November 23, 2022 16:48
@pull-request-quantifier-deprecated

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


Quantification details

Label      : Extra Large
Size       : +236 -177
Percentile : 80.43%

Total files changed: 27

Change summary by file extension:
.cs : +230 -175
.csproj : +6 -2

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

@SteveL-MSFT SteveL-MSFT 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 merge?

@daxian-dbw daxian-dbw merged commit 3dc95ce into PowerShell:master Nov 30, 2022
@daxian-dbw
Copy link
Member

@iSazonov Thanks for doing this refactoring!

@iSazonov iSazonov deleted the move-windows-pinvokes branch November 30, 2022 18:02
@ghost
Copy link

ghost commented Dec 20, 2022

🎉v7.4.0-preview.1 has been released which incorporates this pull request.:tada:

Handy links:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CL-CodeCleanup Indicates that a PR should be marked as a Code Cleanup change in the Change Log Extra Large
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants