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

Move potential expensive task of ItemManager to ThreadPool #68082

Merged
merged 1 commit into from
May 4, 2023
Merged
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 @@ -20,6 +20,7 @@
using Microsoft.VisualStudio.Language.Intellisense.AsyncCompletion;
using Microsoft.VisualStudio.Language.Intellisense.AsyncCompletion.Data;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Threading;
using Roslyn.Utilities;
using RoslynCompletionItem = Microsoft.CodeAnalysis.Completion.CompletionItem;
using VSCompletionItem = Microsoft.VisualStudio.Language.Intellisense.AsyncCompletion.Data.CompletionItem;
Expand Down Expand Up @@ -139,7 +140,7 @@ public CompletionListUpdater(
// Determine the list of items to be included in the completion list.
// This is computed based on the filter text as well as the current
// selection of filters and expander.
AddCompletionItems(itemsToBeIncluded, threadLocalPatternMatchHelper, cancellationToken);
await AddCompletionItemsAsync(itemsToBeIncluded, threadLocalPatternMatchHelper, cancellationToken).ConfigureAwait(false);

// Decide if we want to dismiss an empty completion list based on CompletionRules and filter usage.
if (itemsToBeIncluded.Count == 0)
Expand Down Expand Up @@ -236,7 +237,7 @@ private bool ShouldDismissCompletionListImmediately()
return false;
}

private void AddCompletionItems(List<MatchResult> list, ThreadLocal<PatternMatchHelper> threadLocalPatternMatchHelper, CancellationToken cancellationToken)
private async Task AddCompletionItemsAsync(List<MatchResult> list, ThreadLocal<PatternMatchHelper> threadLocalPatternMatchHelper, CancellationToken cancellationToken)
{
// Convert initial and update trigger reasons to corresponding Roslyn type so
// we can interact with Roslyn's completion system
Expand All @@ -249,6 +250,9 @@ private void AddCompletionItems(List<MatchResult> list, ThreadLocal<PatternMatch
var includedPreferredItems = new ConcurrentSet<string>();
var includedDefaults = new ConcurrentDictionary<string, MatchResult>();

// Make sure we are on threadpool thread before running PLinq query to avoid sync waiting on the special high-pri thread of async-completion.
await TaskScheduler.Default;

Enumerable.Range(0, _snapshotData.InitialSortedItemList.Count)
.AsParallel()
.WithCancellation(cancellationToken)
Expand All @@ -258,6 +262,7 @@ private void AddCompletionItems(List<MatchResult> list, ThreadLocal<PatternMatch

void CreateMatchResultAndProcessMatchingDefaults(int index)
{
cancellationToken.ThrowIfCancellationRequested();
var item = _snapshotData.InitialSortedItemList[index];

// All items passed in should contain a CompletionItemData object in the property bag,
Expand Down