Skip to content

Commit

Permalink
Merge pull request #36161 from sharwell/updaterefs-no-capture
Browse files Browse the repository at this point in the history
Avoid in-process captures in UpdateReferencesAync
  • Loading branch information
sharwell authored Jun 8, 2019
2 parents 0b8e89e + 2ec12e3 commit 611578d
Showing 1 changed file with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ private async Task UpdateSymbolTreeInfoAsync(Project project, CancellationToken
// Produce the indices for the source and metadata symbols in parallel.
var tasks = new List<Task>
{
GetTask(project, () => UpdateSourceSymbolTreeInfoAsync(project, cancellationToken), cancellationToken),
GetTask(project, () => UpdateReferencesAync(project, cancellationToken), cancellationToken)
GetTask(project, (self, project, _, cancellationToken) => self.UpdateSourceSymbolTreeInfoAsync(project, cancellationToken), null, cancellationToken),
GetTask(project, (self, project, _, cancellationToken) => self.UpdateReferencesAync(project, cancellationToken), null, cancellationToken)
};

await Task.WhenAll(tasks).ConfigureAwait(false);
Expand All @@ -230,14 +230,21 @@ private async Task UpdateSourceSymbolTreeInfoAsync(Project project, Cancellation
}
}

private Task GetTask(Project project, Func<Task> func, CancellationToken cancellationToken)
[PerformanceSensitive("https://github.com/dotnet/roslyn/issues/36158", AllowCaptures = false, Constraint = "Avoid captures to reduce GC pressure when running in the host workspace.")]
private Task GetTask(Project project, Func<IncrementalAnalyzer, Project, PortableExecutableReference, CancellationToken, Task> func, PortableExecutableReference reference, CancellationToken cancellationToken)
{
var isRemoteWorkspace = project.Solution.Workspace.Kind == WorkspaceKind.RemoteWorkspace;
return isRemoteWorkspace
? Task.Run(func, cancellationToken)
: func();
? GetNewTask(this, func, project, reference, cancellationToken)
: func(this, project, reference, cancellationToken);

static Task GetNewTask(IncrementalAnalyzer self, Func<IncrementalAnalyzer, Project, PortableExecutableReference, CancellationToken, Task> func, Project project, PortableExecutableReference reference, CancellationToken cancellationToken)
{
return Task.Run(() => func(self, project, reference, cancellationToken), cancellationToken);
}
}

[PerformanceSensitive("https://github.com/dotnet/roslyn/issues/36158", AllowCaptures = false)]
private Task UpdateReferencesAync(Project project, CancellationToken cancellationToken)
{
// Process all metadata references. If it remote workspace, do this in parallel.
Expand All @@ -246,7 +253,7 @@ private Task UpdateReferencesAync(Project project, CancellationToken cancellatio
foreach (var reference in project.MetadataReferences.OfType<PortableExecutableReference>())
{
tasks.Add(
GetTask(project, () => UpdateReferenceAsync(project, reference, cancellationToken), cancellationToken));
GetTask(project, (self, project, reference, cancellationToken) => self.UpdateReferenceAsync(project, reference, cancellationToken), reference, cancellationToken));
}

return Task.WhenAll(tasks);
Expand Down

0 comments on commit 611578d

Please sign in to comment.