From 7de6607aec8151ed0889c2baf036feb7de99f614 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Thu, 16 Jan 2020 12:47:42 -0800 Subject: [PATCH] Avoid fast-path captures in GetSessionAnalysisScopeTask Addresses 680MB allocations analyzing Roslyn.sln. --- ...nalyzerManager.AnalyzerExecutionContext.cs | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerManager.AnalyzerExecutionContext.cs b/src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerManager.AnalyzerExecutionContext.cs index 09afb49e84f..55e2dc8bb23 100644 --- a/src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerManager.AnalyzerExecutionContext.cs +++ b/src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerManager.AnalyzerExecutionContext.cs @@ -5,6 +5,7 @@ using System.Collections.Immutable; using System.Diagnostics; using System.Threading.Tasks; +using Roslyn.Utilities; namespace Microsoft.CodeAnalysis.Diagnostics { @@ -57,6 +58,9 @@ public AnalyzerExecutionContext(DiagnosticAnalyzer analyzer) /// private ImmutableArray _lazySuppressionDescriptors = default(ImmutableArray); + [PerformanceSensitive( + "https://github.com/dotnet/roslyn/issues/26778", + AllowCaptures = false)] public Task GetSessionAnalysisScopeTask(AnalyzerExecutor analyzerExecutor) { lock (_gate) @@ -67,15 +71,19 @@ public Task GetSessionAnalysisScopeTask(AnalyzerE return _lazySessionScopeTask; } - task = Task.Run(() => - { - var sessionScope = new HostSessionStartAnalysisScope(); - analyzerExecutor.ExecuteInitializeMethod(_analyzer, sessionScope); - return sessionScope; - }, analyzerExecutor.CancellationToken); - + task = getSessionAnalysisScopeTaskSlow(this, analyzerExecutor); _lazySessionScopeTask = task; return task; + + static Task getSessionAnalysisScopeTaskSlow(AnalyzerExecutionContext context, AnalyzerExecutor executor) + { + return Task.Run(() => + { + var sessionScope = new HostSessionStartAnalysisScope(); + executor.ExecuteInitializeMethod(context._analyzer, sessionScope); + return sessionScope; + }, executor.CancellationToken); + } } } -- GitLab