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

Move to using the new Roslyn IIncrementalGenerator API for better in-VS performance #1374

Conversation

jkoritzinsky
Copy link
Member

Through some local testing, it was determined that using the ISourceGenerator V1 API for DllImportGenerator will cause the same VS performance issues as dotnet/runtime#50741. Since the source our generator generates is required at design-time, we cannot take the same resolution as dotnet/runtime#50741 or use a simple non-incrementable direct port to the V2 API with the new "Implementation" source type to avoid impacting design time builds (dotnet/roslyn#54798).

As a result, this PR moves our source generator to a more incremental design.

I've split the pipeline of the generator into a few sections:

  • Checking if TFM is supported
  • Finding all attributed methods
  • Determining the marshalling info for parameters and return values, as well as the GeneratedDllImport attribute info
    • From this point onwards, we no longer use ISymbol-implementing types. Symbols are not equatable across compilations, so any stages where they are in the output are not cacheable between generator runs.
  • Generating syntax for the stub from the marshalling info
  • Adding whitespace to the syntax
    • This operation is apparently very expensive, so by caching before we do this, we can skip this expensive step when the stub doesn't change.
  • Concatenating all of the stubs into one file.
  • Reporting the new file and all diagnostics.

As part of this refactoring, I moved TypePositionInfo to use a new type model to represent the managed type of the parameter/return value instead of symbols due to the caching issues mentioned above.

The testing for the incremental support is an ad-hoc testing model that will be replaced when dotnet/roslyn#54832 is approved, implemented, and available.

I have validated with this branch that there is no typing lag when working in the System.Private.CoreLib solution from dotnet/runtime on a Surface Book 1 i7 (my work laptop).

…Info level instead of using symbols. This way, we can use the default comparer between TypePositionInfo instances to know if they're identical, even across compilations.
…te step so syntax generation only happens incrementally
…more incremental tests and stub out some that should theoretially work, but we can't actually test since GeneratorDriver doesn't support incrementality in the right way.
…more incremental tests and stub out some that should theoretially work, but we can't actually test since GeneratorDriver doesn't support incrementality in the right way.
@jkoritzinsky
Copy link
Member Author

@sharwell I can't seem to repro the failures locally. It looks like some sort of infra issue with the CodeAnalysis.Testing package. Can you take a quick look and see if you can figure out what's going on?

@jkoritzinsky jkoritzinsky merged commit 90c617d into dotnet:feature/DllImportGenerator Sep 8, 2021
@jkoritzinsky jkoritzinsky deleted the incremental-generator-tpi branch September 8, 2021 18:10
jkoritzinsky added a commit to jkoritzinsky/runtime that referenced this pull request Sep 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-DllImportGenerator Source Generated stubs for P/Invokes in C#
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants