Skip to content

Commit

Permalink
Avoid task allocations for TextDocument.GetTextAsync
Browse files Browse the repository at this point in the history
Updates production code to use GetValueTaskAsync or
GetTextSynchronously, as appropriate, to avoid task allocations in a
call to GetTextAsync.
  • Loading branch information
sharwell committed May 4, 2023
1 parent f731f97 commit b4f4443
Show file tree
Hide file tree
Showing 163 changed files with 226 additions and 215 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ protected override async Task FixAllAsync(Document document, ImmutableArray<Diag
}),
endOfComment: Token(SyntaxKind.EndOfDocumentationCommentToken).WithoutTrivia());

sourceText ??= await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
sourceText ??= await document.GetValueTextAsync(cancellationToken).ConfigureAwait(false);
var indentation = sourceText.GetLeadingWhitespaceOfLineAtPosition(node.FullSpan.Start);
var newLeadingTrivia = TriviaList(
Whitespace(indentation),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private static async Task<Document> GetChangedDocumentAsync(Document document, i
// 1. Inserts an opening parenthesis
// 2. Re-parses the resulting document (now the colon isn't treated as starting a FormatClause anymore)
// 3. Replaces the missing CloseParenToken with a new one
var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
var text = await document.GetValueTextAsync(cancellationToken).ConfigureAwait(false);
var openParenthesisPosition = conditionalExpressionSyntaxStartPosition;
var textWithOpenParenthesis = text.Replace(openParenthesisPosition, 0, "(");
var documentWithOpenParenthesis = document.WithText(textWithOpenParenthesis);
Expand All @@ -85,7 +85,7 @@ private static async Task<Document> InsertCloseParenthesisAsync(
ParenthesizedExpressionSyntax parenthesizedExpression,
CancellationToken cancellationToken)
{
var sourceText = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
var sourceText = await document.GetValueTextAsync(cancellationToken).ConfigureAwait(false);
if (parenthesizedExpression.Expression is ConditionalExpressionSyntax conditional &&
parenthesizedExpression.GetAncestor<InterpolatedStringExpressionSyntax>()?.StringStartToken.Kind() == SyntaxKind.InterpolatedStringStartToken)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
private static async Task<Document> UpdateDocumentAsync(
Document document, ImmutableArray<Diagnostic> diagnostics, CancellationToken cancellationToken)
{
var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
var text = await document.GetValueTextAsync(cancellationToken).ConfigureAwait(false);
var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
using var _ = ArrayBuilder<TextChange>.GetInstance(out var edits);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
private static async Task<Document> UpdateDocumentAsync(
Document document, ImmutableArray<Diagnostic> diagnostics, CancellationToken cancellationToken)
{
var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
var text = await document.GetValueTextAsync(cancellationToken).ConfigureAwait(false);
var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
using var _ = ArrayBuilder<TextChange>.GetInstance(out var edits);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private static Task<Document> UpdateDocumentAsync(Document document, Diagnostic

public static async Task<Document> FixAllAsync(Document document, ImmutableArray<Diagnostic> diagnostics, CancellationToken cancellationToken)
{
var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
var text = await document.GetValueTextAsync(cancellationToken).ConfigureAwait(false);
var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
using var _ = PooledDictionary<SyntaxToken, SyntaxToken>.GetInstance(out var tokenToToken);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
private static async Task<Document> UpdateDocumentAsync(
Document document, ImmutableArray<Diagnostic> diagnostics, CancellationToken cancellationToken)
{
var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
var text = await document.GetValueTextAsync(cancellationToken).ConfigureAwait(false);
var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
using var _ = PooledDictionary<SyntaxToken, SyntaxToken>.GetInstance(out var replacementMap);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
var document = context.Document;

var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
var text = await document.GetValueTextAsync(cancellationToken).ConfigureAwait(false);

var position = context.Span.Start;
if (!ShouldFix(root, text, position, out var startLine, out var firstMiddleLine, out var secondMiddleLine, out var endLine))
Expand Down Expand Up @@ -311,7 +311,7 @@ private static async Task<Document> AddEditsAsync(
Action<SourceText, ArrayBuilder<TextChange>, int, int, int, int> addEdits,
CancellationToken cancellationToken)
{
var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
var text = await document.GetValueTextAsync(cancellationToken).ConfigureAwait(false);

using var _ = ArrayBuilder<TextChange>.GetInstance(out var edits);
addEdits(text, edits, startPos, firstMiddlePos, secondMiddlePos, endPos);
Expand Down Expand Up @@ -397,7 +397,7 @@ TakeBottomEquivalenceKey or
var orderedDiagnostics = diagnostics.OrderBy(
(d1, d2) => d1.Location.SourceSpan.Start - d2.Location.SourceSpan.Start).ToImmutableArray();

var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
var text = await document.GetValueTextAsync(cancellationToken).ConfigureAwait(false);
var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

// Create a single array of edits to apply. Then walk over all the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private static async Task<bool> MakeMultiLineAsync(
Document document, SyntaxNode condition, SyntaxNode trueSyntax, SyntaxNode falseSyntax, CodeActionOptionsProvider fallbackOptions,
CancellationToken cancellationToken)
{
var sourceText = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
var sourceText = await document.GetValueTextAsync(cancellationToken).ConfigureAwait(false);
if (!sourceText.AreOnSameLine(condition.GetFirstToken(), condition.GetLastToken()) ||
!sourceText.AreOnSameLine(trueSyntax.GetFirstToken(), trueSyntax.GetLastToken()) ||
!sourceText.AreOnSameLine(falseSyntax.GetFirstToken(), falseSyntax.GetLastToken()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public override async Task ProvideCompletionsAsync(CompletionContext context)
var cancellationToken = context.CancellationToken;

// the provider might be invoked in non-interactive context:
var sourceText = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
var sourceText = await document.GetValueTextAsync(cancellationToken).ConfigureAwait(false);
if (Workspace.TryGetWorkspace(sourceText.Container, out var workspace))
{
if (workspace is InteractiveWindowWorkspace interactiveWorkspace)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public Task<bool> CanNavigateToPositionAsync(Workspace workspace, DocumentId doc
if (document is null)
return null;

var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
var text = await document.GetValueTextAsync(cancellationToken).ConfigureAwait(false);
var textSnapshot = text.FindCorrespondingEditorTextSnapshot();
if (textSnapshot == null)
return null;
Expand Down
2 changes: 1 addition & 1 deletion src/EditorFeatures/Core.Wpf/QuickInfo/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static ITextBuffer CloneTextBuffer(this Document document, SourceText sou
/// </summary>
public static async Task<ITextBuffer> CloneTextBufferAsync(this Document document, CancellationToken cancellationToken)
{
var sourceText = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
var sourceText = await document.GetValueTextAsync(cancellationToken).ConfigureAwait(false);
return CloneTextBuffer(document, sourceText);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public async Task<bool> ApplyAsync(
var singleChangedDocument = TryGetSingleChangedText(oldSolution, operations);
if (singleChangedDocument != null)
{
var text = await singleChangedDocument.GetTextAsync(cancellationToken).ConfigureAwait(true);
var text = await singleChangedDocument.GetValueTextAsync(cancellationToken).ConfigureAwait(true);

using (workspace.Services.GetRequiredService<ISourceTextUndoService>().RegisterUndoTransaction(text, title))
{
Expand Down Expand Up @@ -365,7 +365,7 @@ await navigationService.TryNavigateToPositionAsync(
if (pathToRenameToken.TryResolve(openRoot, out resolvedRenameToken) &&
resolvedRenameToken.IsToken)
{
var text = await openDocument.GetTextAsync(cancellationToken).ConfigureAwait(false);
var text = await openDocument.GetValueTextAsync(cancellationToken).ConfigureAwait(false);
var snapshot = text.FindCorrespondingEditorTextSnapshot();
if (snapshot != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ public async ValueTask<ImmutableArray<ActiveStatementSpan>> GetSpansAsync(Soluti
return ImmutableArray<ActiveStatementSpan>.Empty;
}

var sourceText = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
var sourceText = await document.GetValueTextAsync(cancellationToken).ConfigureAwait(false);

lock (_trackingSpans)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async Task QueueUpdateAsync()
if (analyzerConfigDocument is null)
return null;

var originalText = await analyzerConfigDocument.GetTextAsync(token).ConfigureAwait(false);
var originalText = await analyzerConfigDocument.GetValueTextAsync(token).ConfigureAwait(false);
using (await _guard.DisposableWaitAsync(token).ConfigureAwait(false))
{
var newText = GetNewText(originalText, _queue, token);
Expand Down Expand Up @@ -81,7 +81,7 @@ async Task QueueUpdateAsync()
return null;
}

var originalText = await analyzerConfigDocument!.GetTextAsync(token).ConfigureAwait(false);
var originalText = await analyzerConfigDocument!.GetValueTextAsync(token).ConfigureAwait(false);
return newText.GetTextChanges(originalText);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public async Task<ImmutableArray<IntentSource>> ComputeIntentsAsync(IntentReques
return ImmutableArray<IntentSource>.Empty;
}

var currentText = await currentDocument.GetTextAsync(cancellationToken).ConfigureAwait(false);
var currentText = await currentDocument.GetValueTextAsync(cancellationToken).ConfigureAwait(false);
var originalDocument = currentDocument.WithText(currentText.WithChanges(intentRequestContext.PriorTextEdits));

var selectionTextSpan = intentRequestContext.PriorSelection;
Expand Down
4 changes: 2 additions & 2 deletions src/EditorFeatures/Core/InlineRename/InlineRenameService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public async Task<InlineRenameSessionInfo> StartInlineSessionAsync(
return new InlineRenameSessionInfo(renameInfo.LocalizedErrorMessage);
}

var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
var text = await document.GetValueTextAsync(cancellationToken).ConfigureAwait(false);
var snapshot = text.FindCorrespondingEditorTextSnapshot();
Contract.ThrowIfNull(snapshot, "The document used for starting the inline rename session should still be open and associated with a snapshot.");

Expand Down Expand Up @@ -143,7 +143,7 @@ public async Task<InlineRenameSessionInfo> StartInlineSessionAsync(
using var _ = PooledObjects.ArrayBuilder<(ITextBuffer, SnapshotSpan)>.GetInstance(out var buffersAndSpans);
foreach (var documentSpan in inlineRenameInfo.DefinitionLocations)
{
var sourceText = await documentSpan.Document.GetTextAsync(cancellationToken).ConfigureAwait(false);
var sourceText = await documentSpan.Document.GetValueTextAsync(cancellationToken).ConfigureAwait(false);
var textSnapshot = sourceText.FindCorrespondingEditorTextSnapshot();

if (textSnapshot != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ internal void ApplyConflictResolutionEdits(IInlineRenameReplacementInfo conflict
linkedDocumentsMightConflict = false;

// Only need to check the new span's content
var firstDocumentNewText = conflictResolution.NewSolution.GetDocument(firstDocumentReplacements.document.Id).GetTextAsync(cancellationToken).WaitAndGetResult(cancellationToken);
var firstDocumentNewText = conflictResolution.NewSolution.GetDocument(firstDocumentReplacements.document.Id).GetTextSynchronously(cancellationToken);
var firstDocumentNewSpanText = firstDocumentReplacements.Item2.SelectAsArray(replacement => firstDocumentNewText.ToString(replacement.NewSpan));
foreach (var (document, replacements) in documentReplacements)
{
Expand All @@ -416,7 +416,7 @@ internal void ApplyConflictResolutionEdits(IInlineRenameReplacementInfo conflict
continue;
}

var documentNewText = conflictResolution.NewSolution.GetDocument(document.Id).GetTextAsync(cancellationToken).WaitAndGetResult(cancellationToken);
var documentNewText = conflictResolution.NewSolution.GetDocument(document.Id).GetTextSynchronously(cancellationToken);
for (var i = 0; i < replacements.Length; i++)
{
if (documentNewText.ToString(replacements[i].NewSpan) != firstDocumentNewSpanText[i])
Expand Down Expand Up @@ -554,8 +554,8 @@ private static async Task<IEnumerable<TextChange>> GetTextChangesFromTextDiffere
throw new ArgumentException(WorkspacesResources.The_specified_document_is_not_a_version_of_this_document);
}

var oldText = await oldDocument.GetTextAsync(cancellationToken).ConfigureAwait(false);
var newText = await newDocument.GetTextAsync(cancellationToken).ConfigureAwait(false);
var oldText = await oldDocument.GetValueTextAsync(cancellationToken).ConfigureAwait(false);
var newText = await newDocument.GetValueTextAsync(cancellationToken).ConfigureAwait(false);

if (oldText == newText)
{
Expand Down Expand Up @@ -596,7 +596,7 @@ private IEnumerable<InlineRenameReplacement> GetMergedReplacementInfos(
SnapshotSpan? snapshotSpanToClone = null;
string preMergeDocumentTextString = null;

var preMergeDocumentText = preMergeDocument.GetTextAsync(cancellationToken).WaitAndGetResult(cancellationToken);
var preMergeDocumentText = preMergeDocument.GetTextSynchronously(cancellationToken);
var snapshot = preMergeDocumentText.FindCorrespondingEditorTextSnapshot();
if (snapshot != null && _textBufferCloneService != null)
{
Expand All @@ -605,7 +605,7 @@ private IEnumerable<InlineRenameReplacement> GetMergedReplacementInfos(

if (snapshotSpanToClone == null)
{
preMergeDocumentTextString = preMergeDocument.GetTextAsync(cancellationToken).WaitAndGetResult(cancellationToken).ToString();
preMergeDocumentTextString = preMergeDocument.GetTextSynchronously(cancellationToken).ToString();
}

foreach (var replacement in relevantReplacements)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private static async Task<ContainerElement> BuildInteractiveContentAsync(

var tabSize = context.LineFormattingOptions.TabSize;

var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
var text = await document.GetValueTextAsync(cancellationToken).ConfigureAwait(false);
var spans = IndentationHelper.GetSpansWithAlignedIndentation(text, classifiedSpans, tabSize);
var textRunsOfSpan = spans.Select(s => new ClassifiedTextRun(s.ClassificationType, text.GetSubText(s.TextSpan).ToString(), ClassifiedTextRunStyle.UseClassificationFont)).ToList();
if (textRunsOfSpan.Count > 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public async Task<Hover> CreateHoverAsync(
if (!supportsVSExtensions)
return await DefaultLspHoverResultCreationService.CreateDefaultHoverAsync(document, info, clientCapabilities, cancellationToken).ConfigureAwait(false);

var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
var text = await document.GetValueTextAsync(cancellationToken).ConfigureAwait(false);
var language = document.Project.Language;

var classificationOptions = _globalOptions.GetClassificationOptions(language);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ await DefaultLspCompletionResultCreationService.PopulateTextEditAsync(
bool snippetsSupported,
CancellationToken cancellationToken)
{
var documentText = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
var documentText = await document.GetValueTextAsync(cancellationToken).ConfigureAwait(false);

var completionChange = await completionService.GetChangeAsync(
document, selectedItem, cancellationToken: cancellationToken).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -752,8 +752,8 @@ private IHierarchicalDifferenceCollection ComputeEditDifferences(TextDocument ol
cancellationToken.ThrowIfCancellationRequested();

// Get the text that's actually in the editor.
var oldText = oldDocument.GetTextAsync(cancellationToken).WaitAndGetResult(cancellationToken);
var newText = newDocument.GetTextAsync(cancellationToken).WaitAndGetResult(cancellationToken);
var oldText = oldDocument.GetTextSynchronously(cancellationToken);
var newText = newDocument.GetTextSynchronously(cancellationToken);

// Defer to the editor to figure out what changes the client made.
var diffService = _differenceSelectorService.GetTextDifferencingService(
Expand Down
Loading

0 comments on commit b4f4443

Please sign in to comment.