From 600515ddab882a6110dd42e65365dc64b4467e78 Mon Sep 17 00:00:00 2001 From: Todd Grunke Date: Wed, 18 Oct 2023 13:56:58 -0700 Subject: [PATCH] Slight improvement to refactoring preview performance for InlineTemporaryCodeRefactoringProvider The GetSemanticModelAsync calls are expensive now as they run source generators when the document they are being requested on doesn't yet have a compilation. --- .../InlineTemporaryCodeRefactoringProvider.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Features/CSharp/Portable/CodeRefactorings/InlineTemporary/InlineTemporaryCodeRefactoringProvider.cs b/src/Features/CSharp/Portable/CodeRefactorings/InlineTemporary/InlineTemporaryCodeRefactoringProvider.cs index 497e5049139cf..f6f3d36f2505d 100644 --- a/src/Features/CSharp/Portable/CodeRefactorings/InlineTemporary/InlineTemporaryCodeRefactoringProvider.cs +++ b/src/Features/CSharp/Portable/CodeRefactorings/InlineTemporary/InlineTemporaryCodeRefactoringProvider.cs @@ -142,7 +142,6 @@ private static async Task InlineTemporaryAsync(Document document, Vari // Annotate the variable declarator so that we can get back to it later. document = await document.ReplaceNodeAsync(declarator, declarator.WithAdditionalAnnotations(DefinitionAnnotation), cancellationToken).ConfigureAwait(false); - var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); declarator = await FindDeclaratorAsync(document, cancellationToken).ConfigureAwait(false); @@ -155,7 +154,6 @@ private static async Task InlineTemporaryAsync(Document document, Vari (o, n) => n.WithAdditionalAnnotations(ReferenceAnnotation), cancellationToken).ConfigureAwait(false); - semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); declarator = await FindDeclaratorAsync(document, cancellationToken).ConfigureAwait(false); allReferences = await FindReferenceAnnotatedNodesAsync(document, cancellationToken).ConfigureAwait(false); @@ -172,7 +170,6 @@ private static async Task InlineTemporaryAsync(Document document, Vari var newScope = ReferenceRewriter.Visit(scope, conflictReferences, nonConflictReferences, expressionToInline, cancellationToken); document = await document.ReplaceNodeAsync(scope, newScope, cancellationToken).ConfigureAwait(false); - semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); declarator = await FindDeclaratorAsync(document, cancellationToken).ConfigureAwait(false); newScope = GetScope(declarator); @@ -183,7 +180,6 @@ private static async Task InlineTemporaryAsync(Document document, Vari // No semantic conflicts, we can remove the definition. document = await document.ReplaceNodeAsync( newScope, RemoveDeclaratorFromScope(declarator, newScope), cancellationToken).ConfigureAwait(false); - semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); } // Finally, check all the places we inlined an expression and add some final warnings there if appropriate. @@ -192,6 +188,8 @@ private static async Task InlineTemporaryAsync(Document document, Vari .OfType() .Select(e => GetTopMostParentingExpression(e)); + var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); + // Make each topmost parenting statement or Equals Clause Expressions semantically explicit. document = await document.ReplaceNodesAsync(topmostParentingExpressions, (o, n) => {