Skip to content

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

Merged
jkoritzinsky merged 31 commits into
dotnet:feature/DllImportGeneratorfrom
jkoritzinsky:incremental-generator-tpi
Sep 8, 2021
Merged

jkoritzinsky merged 31 commits into
dotnet:feature/DllImportGeneratorfrom
jkoritzinsky:incremental-generator-tpi

Conversation

@jkoritzinsky

Copy link
Copy Markdown
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.
Comment thread DllImportGenerator/Benchmarks/Benchmarks.csproj Outdated
Comment thread DllImportGenerator/DllImportGenerator/Comparers.cs Outdated
Comment thread DllImportGenerator/DllImportGenerator/StubCodeGenerator.cs Outdated
Comment thread DllImportGenerator/DllImportGenerator/StubCodeGenerator.cs Outdated
Comment thread DllImportGenerator/DllImportGenerator/StubCodeGenerator.cs Outdated
Comment thread DllImportGenerator/DllImportGenerator/DllImportGenerator.cs Outdated
Comment thread DllImportGenerator/DllImportGenerator/DllImportGenerator.cs
Comment thread DllImportGenerator/DllImportGenerator/DllImportGenerator.cs
Comment thread DllImportGenerator/DllImportGenerator/DllImportGenerator.cs Outdated
Comment thread DllImportGenerator/DllImportGenerator/DllImportGenerator.cs Outdated
@jkoritzinsky

Copy link
Copy Markdown
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?

Comment thread DllImportGenerator/DllImportGenerator.UnitTests/TestUtils.cs Outdated
@jkoritzinsky
jkoritzinsky requested a review from sharwell August 28, 2021 00:11
@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