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

PERF: Reduce the number of nodes walked during import completion commit. #77305

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.AddImport;
using Microsoft.CodeAnalysis.CodeGeneration;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.PooledObjects;
Expand Down Expand Up @@ -38,7 +36,6 @@ public async Task<Document> AddImportsAsync(
{
var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var addImportsService = document.GetRequiredLanguageService<IAddImportsService>();
var codeGenerator = document.GetRequiredLanguageService<ICodeGenerationService>();
var generator = document.GetRequiredLanguageService<SyntaxGenerator>();

// Create a simple interval tree for simplification spans.
Expand All @@ -53,13 +50,20 @@ public async Task<Document> AddImportsAsync(
//
// We'll dive under the parent because it overlaps with the span. But we only want to include (and dive
// into) B and C not A and D.
var nodes = root.DescendantNodesAndSelf(OverlapsWithSpan).Where(OverlapsWithSpan);

if (strategy == Strategy.AddImportsFromSymbolAnnotations)
return await AddImportDirectivesFromSymbolAnnotationsAsync(document, nodes, addImportsService, generator, options, cancellationToken).ConfigureAwait(false);
{
var nodes = root.DescendantNodesAndSelf(n => OverlapsWithSpan(n) && n.ContainsAnnotations).Where(OverlapsWithSpan);
var annotatedNodes = nodes.Where(x => x.HasAnnotations(SymbolAnnotation.Kind));

return await AddImportDirectivesFromSymbolAnnotationsAsync(document, annotatedNodes, addImportsService, generator, options, cancellationToken).ConfigureAwait(false);
}

if (strategy == Strategy.AddImportsFromSyntaxes)
{
var nodes = root.DescendantNodesAndSelf(OverlapsWithSpan).Where(OverlapsWithSpan);

return await AddImportDirectivesFromSyntaxesAsync(document, nodes, addImportsService, generator, options, cancellationToken).ConfigureAwait(false);
}

throw ExceptionUtilities.UnexpectedValue(strategy);

Expand Down Expand Up @@ -165,7 +169,7 @@ private async Task<Document> AddImportDirectivesFromSyntaxesAsync(

private async Task<Document> AddImportDirectivesFromSymbolAnnotationsAsync(
Document document,
IEnumerable<SyntaxNode> syntaxNodes,
IEnumerable<SyntaxNode> annotatedNodes,
IAddImportsService addImportsService,
SyntaxGenerator generator,
AddImportPlacementOptions options,
Expand All @@ -182,7 +186,6 @@ private async Task<Document> AddImportDirectivesFromSymbolAnnotationsAsync(
#endif

SyntaxNode? first = null, last = null;
var annotatedNodes = syntaxNodes.Where(x => x.HasAnnotations(SymbolAnnotation.Kind));

foreach (var annotatedNode in annotatedNodes)
{
Expand Down
Loading