From a1b9931303312820f6bc7a1b7387f687f30e6b6e Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Mon, 20 May 2024 15:23:03 -0700 Subject: [PATCH 1/2] Only fetch options if needed --- ...bstractTableDataSourceFindUsagesContext.cs | 37 +++++++++++-------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/src/VisualStudio/Core/Def/FindReferences/Contexts/AbstractTableDataSourceFindUsagesContext.cs b/src/VisualStudio/Core/Def/FindReferences/Contexts/AbstractTableDataSourceFindUsagesContext.cs index 6fede06526b6e..b42289b5b9678 100644 --- a/src/VisualStudio/Core/Def/FindReferences/Contexts/AbstractTableDataSourceFindUsagesContext.cs +++ b/src/VisualStudio/Core/Def/FindReferences/Contexts/AbstractTableDataSourceFindUsagesContext.cs @@ -428,9 +428,8 @@ protected async Task AddDocumentSpanEntriesAsync( CancellationToken cancellationToken) { var document = documentSpan.Document; - var options = _globalOptions.GetClassificationOptions(document.Project.Language); var sourceText = await document.GetValueTextAsync(cancellationToken).ConfigureAwait(false); - var (excerptResult, lineText) = await ExcerptAsync(sourceText, documentSpan, classifiedSpans, options, cancellationToken).ConfigureAwait(false); + var (excerptResult, lineText) = await ExcerptAsync(sourceText, documentSpan, classifiedSpans, cancellationToken).ConfigureAwait(false); var mappedDocumentSpan = await AbstractDocumentSpanEntry.TryMapAndGetFirstAsync(documentSpan, sourceText, cancellationToken).ConfigureAwait(false); if (mappedDocumentSpan == null) @@ -457,31 +456,39 @@ protected async Task AddDocumentSpanEntriesAsync( ThreadingContext); } - private static async Task<(ExcerptResult, SourceText)> ExcerptAsync( - SourceText sourceText, DocumentSpan documentSpan, ClassifiedSpansAndHighlightSpan? classifiedSpans, ClassificationOptions options, CancellationToken cancellationToken) + private async Task<(ExcerptResult, SourceText)> ExcerptAsync( + SourceText sourceText, DocumentSpan documentSpan, ClassifiedSpansAndHighlightSpan? classifiedSpans, CancellationToken cancellationToken) { - var excerptService = documentSpan.Document.Services.GetService(); + var document = documentSpan.Document; + var sourceSpan = documentSpan.SourceSpan; + + var excerptService = document.Services.GetService(); if (excerptService != null) { - var result = await excerptService.TryExcerptAsync(documentSpan.Document, documentSpan.SourceSpan, ExcerptMode.SingleLine, options, cancellationToken).ConfigureAwait(false); + var options = _globalOptions.GetClassificationOptions(document.Project.Language); + + var result = await excerptService.TryExcerptAsync(document, sourceSpan, ExcerptMode.SingleLine, options, cancellationToken).ConfigureAwait(false); if (result != null) - { return (result.Value, AbstractDocumentSpanEntry.GetLineContainingPosition(result.Value.Content, result.Value.MappedSpan.Start)); - } } - var classificationResult = await ClassifiedSpansAndHighlightSpanFactory.ClassifyAsync( - documentSpan, classifiedSpans, options, cancellationToken).ConfigureAwait(false); + if (classifiedSpans is null) + { + var options = _globalOptions.GetClassificationOptions(document.Project.Language); + + classifiedSpans = await ClassifiedSpansAndHighlightSpanFactory.ClassifyAsync( + documentSpan, classifiedSpans, options, cancellationToken).ConfigureAwait(false); + } // need to fix the span issue tracking here - https://github.com/dotnet/roslyn/issues/31001 var excerptResult = new ExcerptResult( sourceText, - classificationResult.HighlightSpan, - classificationResult.ClassifiedSpans, - documentSpan.Document, - documentSpan.SourceSpan); + classifiedSpans.Value.HighlightSpan, + classifiedSpans.Value.ClassifiedSpans, + document, + sourceSpan); - return (excerptResult, AbstractDocumentSpanEntry.GetLineContainingPosition(sourceText, documentSpan.SourceSpan.Start)); + return (excerptResult, AbstractDocumentSpanEntry.GetLineContainingPosition(sourceText, sourceSpan.Start)); } public sealed override async ValueTask OnReferenceFoundAsync(SourceReferenceItem reference, CancellationToken cancellationToken) From d648dc1d4215d7f36b42f7d6e2846f1869d8916f Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Mon, 20 May 2024 17:55:19 -0700 Subject: [PATCH 2/2] Comment --- .../AbstractTableDataSourceFindUsagesContext.cs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/VisualStudio/Core/Def/FindReferences/Contexts/AbstractTableDataSourceFindUsagesContext.cs b/src/VisualStudio/Core/Def/FindReferences/Contexts/AbstractTableDataSourceFindUsagesContext.cs index b42289b5b9678..79fa5ae84a1e3 100644 --- a/src/VisualStudio/Core/Def/FindReferences/Contexts/AbstractTableDataSourceFindUsagesContext.cs +++ b/src/VisualStudio/Core/Def/FindReferences/Contexts/AbstractTableDataSourceFindUsagesContext.cs @@ -463,21 +463,24 @@ protected async Task AddDocumentSpanEntriesAsync( var sourceSpan = documentSpan.SourceSpan; var excerptService = document.Services.GetService(); + + // Fetching options is expensive enough to try to avoid it if we can. So only fetch this if absolutely necessary. + ClassificationOptions? options = null; if (excerptService != null) { - var options = _globalOptions.GetClassificationOptions(document.Project.Language); + options ??= _globalOptions.GetClassificationOptions(document.Project.Language); - var result = await excerptService.TryExcerptAsync(document, sourceSpan, ExcerptMode.SingleLine, options, cancellationToken).ConfigureAwait(false); + var result = await excerptService.TryExcerptAsync(document, sourceSpan, ExcerptMode.SingleLine, options.Value, cancellationToken).ConfigureAwait(false); if (result != null) return (result.Value, AbstractDocumentSpanEntry.GetLineContainingPosition(result.Value.Content, result.Value.MappedSpan.Start)); } if (classifiedSpans is null) { - var options = _globalOptions.GetClassificationOptions(document.Project.Language); + options ??= _globalOptions.GetClassificationOptions(document.Project.Language); classifiedSpans = await ClassifiedSpansAndHighlightSpanFactory.ClassifyAsync( - documentSpan, classifiedSpans, options, cancellationToken).ConfigureAwait(false); + documentSpan, classifiedSpans, options.Value, cancellationToken).ConfigureAwait(false); } // need to fix the span issue tracking here - https://github.com/dotnet/roslyn/issues/31001