-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Change logic for how we cache GeneratorDrivers to avoid initialization #81323
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
Draft
jasonmalinowski
wants to merge
1
commit into
dotnet:main
Choose a base branch
from
jasonmalinowski:generator-initialization-caching-improvements
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -291,13 +291,20 @@ await newGeneratedDocuments.States.Values.SelectAsArrayAsync( | |
|
|
||
| if (generatorDriver == null) | ||
| { | ||
| generatorDriver = await compilationState.GeneratorDriverCache.CreateAndRunGeneratorDriverAsync(this.ProjectState, compilationToRunGeneratorsOn, ShouldGeneratorRun, cancellationToken).ConfigureAwait(false); | ||
| generatorDriver = await this.ProjectState.GeneratorDriverCache.CreateAndRunGeneratorDriverAsync(this.ProjectState, compilationToRunGeneratorsOn, ShouldGeneratorRun, cancellationToken).ConfigureAwait(false); | ||
| } | ||
| else | ||
| { | ||
| generatorDriver = generatorDriver.RunGenerators(compilationToRunGeneratorsOn, ShouldGeneratorRun, cancellationToken); | ||
| } | ||
|
|
||
| // Since this is our most recent run, we'll update our cache with this one. This has two benefits: | ||
| // | ||
| // 1. If some other fork of this Solution needs a GeneratorDriver created, it'll have one that's probably more update to date. | ||
| // This is obviously speculative -- if it's a really old Solution fork it might not help, but can't hurt for the more common cases. | ||
| // 2. It ensures that we're not holding an old GeneratorDriver alive, which itself may hold onto state that's no longer applicable. | ||
| this.ProjectState.GeneratorDriverCache.UpdateCacheWithForGeneratorDriver(generatorDriver); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| CheckGeneratorDriver(generatorDriver, this.ProjectState); | ||
|
|
||
| var runResult = generatorDriver.GetRunResult(); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,8 +43,6 @@ internal sealed partial class SolutionCompilationState | |
| public bool PartialSemanticsEnabled { get; } | ||
| public TextDocumentStates<SourceGeneratedDocumentState> FrozenSourceGeneratedDocumentStates { get; } | ||
|
|
||
| public GeneratorDriverInitializationCache GeneratorDriverCache { get; } | ||
|
|
||
| // Values for all these are created on demand. | ||
| private ImmutableSegmentedDictionary<ProjectId, ICompilationTracker> _projectIdToTrackerMap; | ||
|
|
||
|
|
@@ -64,15 +62,13 @@ private SolutionCompilationState( | |
| ImmutableSegmentedDictionary<ProjectId, ICompilationTracker> projectIdToTrackerMap, | ||
| SourceGeneratorExecutionVersionMap sourceGeneratorExecutionVersionMap, | ||
| TextDocumentStates<SourceGeneratedDocumentState> frozenSourceGeneratedDocumentStates, | ||
| GeneratorDriverInitializationCache generatorDriverCreationCache, | ||
| AsyncLazy<SolutionCompilationState>? cachedFrozenSnapshot = null) | ||
| { | ||
| SolutionState = solution; | ||
| PartialSemanticsEnabled = partialSemanticsEnabled; | ||
| _projectIdToTrackerMap = projectIdToTrackerMap; | ||
| SourceGeneratorExecutionVersionMap = sourceGeneratorExecutionVersionMap; | ||
| FrozenSourceGeneratedDocumentStates = frozenSourceGeneratedDocumentStates; | ||
| GeneratorDriverCache = generatorDriverCreationCache; | ||
|
|
||
| // when solution state is changed, we recalculate its checksum | ||
| _lazyChecksums = AsyncLazy.Create(static async (self, cancellationToken) => | ||
|
|
@@ -91,14 +87,12 @@ private SolutionCompilationState( | |
|
|
||
| public SolutionCompilationState( | ||
| SolutionState solution, | ||
| bool partialSemanticsEnabled, | ||
| GeneratorDriverInitializationCache generatorDriverCreationCache) | ||
| bool partialSemanticsEnabled) | ||
| : this( | ||
| solution, | ||
| partialSemanticsEnabled, | ||
| projectIdToTrackerMap: ImmutableSegmentedDictionary<ProjectId, ICompilationTracker>.Empty, | ||
| sourceGeneratorExecutionVersionMap: SourceGeneratorExecutionVersionMap.Empty, | ||
| generatorDriverCreationCache: generatorDriverCreationCache, | ||
| frozenSourceGeneratedDocumentStates: TextDocumentStates<SourceGeneratedDocumentState>.Empty) | ||
| { | ||
| } | ||
|
|
@@ -143,7 +137,6 @@ private SolutionCompilationState Branch( | |
| projectIdToTrackerMap.Value, | ||
| sourceGeneratorExecutionVersionMap, | ||
| frozenSourceGeneratedDocumentStates, | ||
| GeneratorDriverCache, | ||
| cachedFrozenSnapshot); | ||
| } | ||
|
|
||
|
|
@@ -1531,11 +1524,6 @@ public SolutionCompilationState UpdateSpecificSourceGeneratorExecutionVersions( | |
| if (newTracker != existingTracker) | ||
| newIdToTrackerMapBuilder[projectId] = newTracker; | ||
| } | ||
|
|
||
| // Clear out the cache of any previously initialized GeneratorDriver. Otherwise we might reuse a | ||
| // driver which will not count as a new "run" in some of our unit tests. We have tests that very explicitly count | ||
| // and assert the number of invocations of a generator. | ||
| GeneratorDriverCache.EmptyCacheForProject(projectId); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yaay |
||
| } | ||
|
|
||
| if (!changed) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cute. Definitely doc this field to explain this will happen. Ah. I see you did doc this well