Skip to content

Commit 064817d

Browse files
authored
Merge pull request #39395 from mavasani/FixCachingCFG
Performance fix in analyzer driver for CFG based analyzers
2 parents fef3d37 + e8ca13d commit 064817d

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerDriver.cs

+9-2
Original file line numberDiff line numberDiff line change
@@ -2269,8 +2269,15 @@ void executeExecutableCodeActions()
22692269

22702270
if (!operationsToAnalyze.IsEmpty)
22712271
{
2272-
executeOperationsActions(operationsToAnalyze);
2273-
executeOperationsBlockActions(operationBlocksToAnalyze, operationsToAnalyze, codeBlockActions);
2272+
try
2273+
{
2274+
executeOperationsActions(operationsToAnalyze);
2275+
executeOperationsBlockActions(operationBlocksToAnalyze, operationsToAnalyze, codeBlockActions);
2276+
}
2277+
finally
2278+
{
2279+
AnalyzerExecutor.OnOperationBlockActionsExecuted(operationBlocksToAnalyze);
2280+
}
22742281
}
22752282
}
22762283

src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerExecutor.cs

+18
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Runtime.CompilerServices;
1010
using System.Threading;
1111
using Microsoft.CodeAnalysis.FlowAnalysis;
12+
using Microsoft.CodeAnalysis.Operations;
1213
using Microsoft.CodeAnalysis.PooledObjects;
1314
using Microsoft.CodeAnalysis.Text;
1415
using Roslyn.Utilities;
@@ -1913,5 +1914,22 @@ private bool IsAnalyzerSuppressedForSymbol(DiagnosticAnalyzer analyzer, ISymbol
19131914

19141915
return true;
19151916
}
1917+
1918+
public void OnOperationBlockActionsExecuted(ImmutableArray<IOperation> operationBlocks)
1919+
{
1920+
// Clear _lazyControlFlowGraphMap entries for each operation block after we have executed
1921+
// all analysis callbacks for the given operation blocks. This avoids holding onto them
1922+
// for the entire compilation lifetime.
1923+
// These control flow graphs are created on demand and shared between flow analysis based analyzers.
1924+
1925+
if (_lazyControlFlowGraphMap?.Count > 0)
1926+
{
1927+
foreach (var operationBlock in operationBlocks)
1928+
{
1929+
var root = operationBlock.GetRootOperation();
1930+
_lazyControlFlowGraphMap.TryRemove(root, out _);
1931+
}
1932+
}
1933+
}
19161934
}
19171935
}

0 commit comments

Comments
 (0)