-
Notifications
You must be signed in to change notification settings - Fork 226
Perf/generator #8212
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
Perf/generator #8212
Changes from 2 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
c7a95d3
Don't re-run declaration code if nothing has changed
chsienki 3523147
Clean up test file
chsienki 04f79ff
Split tag helper discovery into component and compilation discovery
chsienki 6a4a589
Create a wrapper source generator engine that can selectively run phases
chsienki 89660be
Ensure we don't re-use a rewritten syntax tree
chsienki 7616424
Break output into multiple steps that can be largely skipped as needed
chsienki 489f81c
Remove uneeded tag helper discovery from SG engine
chsienki ff6f5b4
Swap asserts
chsienki 312ff75
Formatting
chsienki 9422b03
PR feedback
chsienki ca2a9e5
Formatting
chsienki fb07d1c
PR Feedback
chsienki 30c9815
Fix tests
chsienki baaddfa
Merge branch 'main' into perf/generator
chsienki 46dbf1e
Use array builder pools
chsienki 5cf0d50
Merge remote-tracking branch 'upstream/main' into perf/generator
chsienki c430fea
Fix merge
chsienki 5ce316d
PR Feedback
chsienki 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
2 changes: 1 addition & 1 deletion
2
src/Compiler/Microsoft.CodeAnalysis.Razor/src/DefaultTagHelperDescriptorFactory.cs
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
2 changes: 1 addition & 1 deletion
2
src/Compiler/Microsoft.CodeAnalysis.Razor/src/SymbolExtensions.cs
333fred marked this conversation as resolved.
Show resolved
Hide resolved
|
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 |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |
| using Microsoft.AspNetCore.Razor.Language; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
|
||
| namespace Microsoft.NET.Sdk.Razor.SourceGenerators | ||
| { | ||
|
|
@@ -106,10 +107,10 @@ public void Initialize(IncrementalGeneratorInitializationContext context) | |
|
|
||
| var compilationWithDeclarations = compilation.AddSyntaxTrees(generatedDeclarationSyntaxTree); | ||
|
|
||
| // try and find the specific class this component is declaring, falling back to the assembly if for any reason we can't | ||
| // try and find the specific root class this component is declaring, falling back to the assembly if for any reason the code is not in the shape we expect | ||
| ISymbol targetSymbol = compilationWithDeclarations.Assembly; | ||
|
Member
Author
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. We need to look at the whole compilation with the component added (as it might reference stuff from csharp), but there's no need to actually discover anything else from the compilation, so only search the component itself. |
||
| var classSyntax = generatedDeclarationSyntaxTree.GetRoot(ct).DescendantNodes().SingleOrDefault(n => n.IsKind(CodeAnalysis.CSharp.SyntaxKind.ClassDeclaration)); | ||
| if (classSyntax is not null) | ||
| var root = generatedDeclarationSyntaxTree.GetRoot(ct); | ||
333fred marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (root is CompilationUnitSyntax { Members: [ NamespaceDeclarationSyntax { Members : [ ClassDeclarationSyntax classSyntax, ..] }, .. ] }) | ||
chsienki marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| targetSymbol = compilationWithDeclarations.GetSemanticModel(generatedDeclarationSyntaxTree).GetDeclaredSymbol(classSyntax, ct) ?? targetSymbol; | ||
| } | ||
|
|
@@ -203,7 +204,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context) | |
| .Select(static (pair, _) => | ||
| { | ||
| var ((tagHelpersFromComponents, tagHelpersFromCompilation), tagHelpersFromReferences) = pair; | ||
| var count = tagHelpersFromCompilation.Count + tagHelpersFromReferences.Count+ tagHelpersFromComponents.Length; | ||
| var count = tagHelpersFromCompilation.Count + tagHelpersFromReferences.Count + tagHelpersFromComponents.Length; | ||
| if (count == 0) | ||
| { | ||
| return Array.Empty<TagHelperDescriptor>(); | ||
|
|
@@ -219,60 +220,61 @@ public void Initialize(IncrementalGeneratorInitializationContext context) | |
|
|
||
| var generatedOutput = sourceItems | ||
| .Combine(importFiles.Collect()) | ||
| .WithLambdaComparer((old, @new) => old.Left.Equals(@new.Left) && old.Right.SequenceEqual(@new.Right), (a) => GetHashCode()) | ||
| .WithLambdaComparer((old, @new) => old.Left.Equals(@new.Left) && old.Right.SequenceEqual(@new.Right), (a) => a.GetHashCode()) | ||
| .Combine(razorSourceGeneratorOptions) | ||
| .Select(static (pair, _) => | ||
| { | ||
| var ((sourceItem, imports), razorSourceGeneratorOptions) = pair; | ||
|
|
||
| RazorSourceGeneratorEventSource.Log.ParseRazorDocumentStart(sourceItem.RelativePhysicalPath); | ||
|
|
||
| var projectEngine = GetGenerationProjectEngine(sourceItem, imports, razorSourceGeneratorOptions); | ||
|
|
||
| var document = projectEngine.ProcessInitialParse(sourceItem); | ||
|
|
||
| RazorSourceGeneratorEventSource.Log.ParseRazorDocumentStop(sourceItem.RelativePhysicalPath); | ||
| return (projectEngine, sourceItem.RelativePhysicalPath, document); | ||
| }) | ||
|
|
||
| // Add the tag helpers in, but ignore if they've changed or not, only reprocessing the actual document changed | ||
| .Combine(allTagHelpers) | ||
| .WithLambdaComparer((old, @new) => old.Left.Equals(@new.Left), (item) => item.GetHashCode()) | ||
| .Select((pair, _) => | ||
| { | ||
| var ((projectEngine, filePath, codeDocument), allTagHelpers) = pair; | ||
| return (projectEngine, sourceItem.RelativePhysicalPath, document); | ||
| }) | ||
|
|
||
| // Add the tag helpers in, but ignore if they've changed or not, only reprocessing the actual document changed | ||
| .Combine(allTagHelpers) | ||
| .WithLambdaComparer((old, @new) => old.Left.Equals(@new.Left), (item) => item.GetHashCode()) | ||
| .Select((pair, _) => | ||
| { | ||
| var ((projectEngine, filePath, codeDocument), allTagHelpers) = pair; | ||
| RazorSourceGeneratorEventSource.Log.RewriteTagHelpersStart(filePath); | ||
|
|
||
| codeDocument = projectEngine.ProcessTagHelpers(codeDocument, allTagHelpers, checkForIdempotency: false); | ||
| codeDocument = projectEngine.ProcessTagHelpers(codeDocument, allTagHelpers, checkForIdempotency: false); | ||
|
|
||
| RazorSourceGeneratorEventSource.Log.RewriteTagHelpersStop(filePath); | ||
| return (projectEngine, filePath, codeDocument); | ||
| }) | ||
| RazorSourceGeneratorEventSource.Log.RewriteTagHelpersStop(filePath); | ||
| return (projectEngine, filePath, codeDocument); | ||
| }) | ||
|
|
||
| // next we do a second parse, along with the helpers, but check for idempotency. If the tag helpers used on the previous parse match, the compiler can skip re-computing them | ||
| .Combine(allTagHelpers) | ||
| .Select((pair, _) => { | ||
| // next we do a second parse, along with the helpers, but check for idempotency. If the tag helpers used on the previous parse match, the compiler can skip re-computing them | ||
| .Combine(allTagHelpers) | ||
| .Select((pair, _) => | ||
| { | ||
|
|
||
| var ((projectEngine, filePath, document), allTagHelpers) = pair; | ||
| RazorSourceGeneratorEventSource.Log.CheckAndRewriteTagHelpersStart(filePath); | ||
| var ((projectEngine, filePath, document), allTagHelpers) = pair; | ||
| RazorSourceGeneratorEventSource.Log.CheckAndRewriteTagHelpersStart(filePath); | ||
|
|
||
| document = projectEngine.ProcessTagHelpers(document, allTagHelpers, checkForIdempotency: true); | ||
|
|
||
| document = projectEngine.ProcessTagHelpers(document, allTagHelpers, checkForIdempotency: true); | ||
|
|
||
| RazorSourceGeneratorEventSource.Log.CheckAndRewriteTagHelpersStop(filePath); | ||
| return (projectEngine, filePath, document); | ||
| }) | ||
| return (projectEngine, filePath, document); | ||
| }) | ||
|
|
||
| .Select((pair, _) => | ||
| { | ||
| var (projectEngine, filePath, document) = pair; | ||
| RazorSourceGeneratorEventSource.Log.RazorCodeGenerateStart(filePath); | ||
| .Select((pair, _) => | ||
| { | ||
| var (projectEngine, filePath, document) = pair; | ||
| RazorSourceGeneratorEventSource.Log.RazorCodeGenerateStart(filePath); | ||
| document = projectEngine.ProcessRemaining(document); | ||
| var csharpDocument = document.CodeDocument.GetCSharpDocument(); | ||
| var csharpDocument = document.CodeDocument.GetCSharpDocument(); | ||
|
|
||
| RazorSourceGeneratorEventSource.Log.RazorCodeGenerateStop(filePath); | ||
| return (filePath, csharpDocument); | ||
| }) | ||
| .WithLambdaComparer(static (a, b) => | ||
| }) | ||
| .WithLambdaComparer(static (a, b) => | ||
| { | ||
| if (a.csharpDocument.Diagnostics.Count > 0 || b.csharpDocument.Diagnostics.Count > 0) | ||
| { | ||
|
|
@@ -287,10 +289,10 @@ public void Initialize(IncrementalGeneratorInitializationContext context) | |
| { | ||
| var (filePath, csharpDocument) = pair; | ||
|
|
||
| // Add a generated suffix so tools, such as coverlet, consider the file to be generated | ||
| var hintName = GetIdentifierFromPath(filePath) + ".g.cs"; | ||
| // Add a generated suffix so tools, such as coverlet, consider the file to be generated | ||
| var hintName = GetIdentifierFromPath(filePath) + ".g.cs"; | ||
|
|
||
| RazorSourceGeneratorEventSource.Log.AddSyntaxTrees(hintName); | ||
| RazorSourceGeneratorEventSource.Log.AddSyntaxTrees(hintName); | ||
| for (var i = 0; i < csharpDocument.Diagnostics.Count; i++) | ||
| { | ||
| var razorDiagnostic = csharpDocument.Diagnostics[i]; | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.