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

Improve AddImport conflict detection performance #73780

Merged
merged 4 commits into from
May 30, 2024

Conversation

ToddGrun
Copy link
Contributor

We've recently obtained some traces from customers around UI delays during completion commit in C# files. The first profile I took a look at indicated that the delay was due to a snippet commit, in particular during the code used to prevent adding conflicting imports.

This code has been improved in 3 ways, all relating to accessing the semantic model (where the vast majority of the time was spent):

  1. Reduce the number of nodes for which semantic information need be obtained. The code already collects a couple dictionaries keyed on type names which could cause conflicts. This simply changes the code to filter syntax nodes based on this.

  2. Change the kind of semantic model used. About half the time used during binding was due to the model dealing with work around nullability. We don't need that information, so we're good to use the much faster NullableDisabled semantic model.

  3. Process binding nodes to their semantic information in parallel. This does a simple collection of nodes of interest, then uses the ProducerConsumer mechanism to obtain semantic information in parallel.

As a minor tweak, I also did away with the TreeWalker derivation, instead just doing a simple walk over the results of GetDescendantNodes. Should be more performant, and I find it a bit easier to understand when the needs are this simple.

We've recently obtained some traces from customers around ui delays during completion commit in C# files. The first profile I took a look at indicated that the delay was due to committing a snippet, and in particular during the code used to prevent adding conflicting imports.

This code has been improved in 3 ways, all relating to accessing the semantic model (where the *vast* majority of the time was spent):

1) Reduce the number of nodes for which semantic information need be obtained. The code already collects a couple dictionaries keyed on type names which could cause conflicts. This simply changes the code to filter syntax nodes based on this.

2) Change the kind of semantic model used. About half the time used during the binding process was due to the model dealing with work around nullability. We don't need that infomation, so we're good to use the much faster nullabledisabled semantic model.

3) Process all the binding of nodes to their semantic info in parallel. This does a simple collection of nodes of interest, then uses the ProducerConsumer mechanism to obtain semantic information in parallel.

As a minor tweak, I aslo did away with the TreeWalker derivation, instead just doing a simple walk over the results of GetDescendantNodes. Should be more performant, and I find it a bit easier to understand when the needs are this simple.
@ToddGrun ToddGrun requested a review from a team as a code owner May 30, 2024 01:10
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels May 30, 2024
break;
case SyntaxKind.SimpleMemberAccessExpression:
case SyntaxKind.PointerMemberAccessExpression:
if (IsPotentialConflictWithImportedExtensionMethod((MemberAccessExpressionSyntax)node))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fwiw, i don't think the pointer case can happen with imports and extensions. but i don't mind you preserving this.

// Track if we've seen an anonymous method or not. If so, because of how the language binds lambdas and
// overloads, we'll assume any method access we see inside (instance or otherwise) could end up conflicting
// with an extension method we might pull in.
containsAnonymousMethods = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok to preserve this. but this genuinely makes no sense wrt to how the conflict checker has to work.

// Drastically reduce the number of nodes that need to be inspected by filtering
// out nodes whose identifier isn't a potential conflict.
if (!_importedTypes.ContainsKey((node.Identifier.Text, node.Arity)))
return false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

excellent!

cancellationToken).ConfigureAwait(False)

For Each conflict In items
conflicts.Add(conflict)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

conflicts.AddRange(items)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, probably easier that way. Just was in the habit of not doing that in the case where it would box an enumerator.

@ToddGrun
Copy link
Contributor Author

unit test failures -- will look into this tomorrow

@ToddGrun ToddGrun merged commit b55a71a into dotnet:main May 30, 2024
25 checks passed
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone May 30, 2024
@genlu
Copy link
Member

genlu commented Jun 6, 2024

Change the kind of semantic model used. About half the time used during binding was due to the model dealing with work around nullability. We don't need that information, so we're good to use the much faster NullableDisabled semantic model.

There was discussion about experiment with using nullable-disable for all of completion, wondering if this is something we are still planning to do

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants