-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Fix unnecessary casts of generic types to specific types in analyzer driver #76917
Conversation
In the scrolling speedometer test, the Roslyn CodeAnalysis process shows about 25% of CPU spent in this method. Of that, a surprising amount of it (11.2%) is spent in ImmutableSegmentedDictionary.TryGetValue. Debugging through this code, it appears this is because that is called O(m x n) times where m is the number of nodes to analyze and n is the number of items in groupedActions.GroupedActionsByAnalyzer. Instead, add a hook into GroupedAnalyzerActions to allow a mapping of kind -> analyzers. This can be used by executeNodeActionsByKind to get a much quicker way to determine whether the analyzer can contribute for the node in question. Only publishing this early so Cyrus can take a peek, as I still need to do a bit of debugging around these changes. Once Cyrus and I think the changes have merit, I will create a test insertion and publish the speedometer results once those are available. Only if all that goes well will I promote this PR out of draft mode.
@@ -777,10 +859,6 @@ private void ExecuteBlockActionsCore<TBlockStartAction, TBlockAction, TNodeActio | |||
|
|||
var blockEndActions = PooledHashSet<TBlockAction>.GetInstance(); | |||
var blockActions = PooledHashSet<TBlockAction>.GetInstance(); | |||
var executableNodeActions = ArrayBuilder<TNodeAction>.GetInstance(); | |||
var syntaxNodeActions = executableNodeActions as ArrayBuilder<SyntaxNodeAnalyzerAction<TLanguageKindEnum>>; | |||
var operationActions = executableNodeActions as ArrayBuilder<OperationAnalyzerAction>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these two lines were super wacky. we'd make the bulder, then decide if we were the codepath that was running for syntax ndoes or for operations. and hten special case behavior lower down.
var executableNodeActions = ArrayBuilder<TNodeAction>.GetInstance(); | ||
var syntaxNodeActions = executableNodeActions as ArrayBuilder<SyntaxNodeAnalyzerAction<TLanguageKindEnum>>; | ||
var operationActions = executableNodeActions as ArrayBuilder<OperationAnalyzerAction>; | ||
ImmutableArray<IOperation> operationBlocks = executableBlocks[0] is IOperation ? (ImmutableArray<IOperation>)(object)executableBlocks : ImmutableArray<IOperation>.Empty; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
similarly, if this was hte syntax node case, we'd to runtime checks to make this an empty array, or else we'd do runtime casts to convert an argument to the right type. now the callers just pass the right array along.
@@ -792,75 +870,13 @@ private void ExecuteBlockActionsCore<TBlockStartAction, TBlockAction, TNodeActio | |||
|
|||
// Include the stateful actions. | |||
foreach (var startAction in startActions) | |||
{ | |||
if (startAction is CodeBlockStartAnalyzerAction<TLanguageKindEnum> codeBlockStartAction) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we'd do runtime checks to then have syntaxnode-vs-operation specific behavior. now, we just pass the data we need to the 'addActions' function, and each caller does the specific logic they want in a properly strongly typed fashion.
|
||
using var _ = PooledDelegates.GetPooledFunction((d, ct, arg) => arg.self.IsSupportedDiagnostic(arg.analyzer, d, ct), (self: this, analyzer), out Func<Diagnostic, CancellationToken, bool> isSupportedDiagnostic); | ||
|
||
// Execute stateful executable node analyzers, if any. | ||
if (executableNodeActions.Any()) | ||
{ | ||
if (syntaxNodeActions != null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same case, we'd specialize what we wanted to do based on what data we were passed.
this was the code that motived the pr int eh first place and move us to spans. i want to be able to get us off of IEnuemrable, but code like (IEnumerable<SyntaxNode>)getNodesToAnalyze(executableBlocks);
just abuses generics so much this isn't possible.
declaredNode, declaredSymbol, executableCodeBlocks, (codeBlocks) => codeBlocks.SelectMany( | ||
cb => | ||
Debug.Assert(!executableCodeBlocks.IsEmpty); | ||
var executableNodeActions = ArrayBuilder<SyntaxNodeAnalyzerAction<TLanguageKindEnum>>.GetInstance(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved this up here so that we can infer the type arguments to ExecuteBlockActionsCore from just the args passed in.
executableNodeActions, | ||
addActions: static (startAction, codeBlockEndActions, syntaxNodeActions, args, cancellationToken) => | ||
{ | ||
var (@this, codeBlockStartActions, analyzer, declaredNode, declaredSymbol, executableCodeBlocks, semanticModel, getKind, filterSpan, isGeneratedCode) = args; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pattern here is that all passed in lambdas are static (so we don't continually allocate for them), but they are passed an 'args' tuple containing any data they need. this args tuple is then unpackaged as the first step exactly how it is packaged up below.
|
||
var codeBlockScope = new HostCodeBlockStartAnalysisScope<TLanguageKindEnum>(startAction.Analyzer); | ||
var blockStartContext = new AnalyzerCodeBlockStartAnalysisContext<TLanguageKindEnum>( | ||
codeBlockScope, declaredNode, declaredSymbol, semanticModel, @this.AnalyzerOptions, filterSpan, isGeneratedCode, cancellationToken); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all this code was hte same code we had below in ExecuteBlockActionsCore. but we don't need runtime checks to determine to execute it. we know we want to execute exactly this code because we're teh syntax-node case.
src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerExecutor.cs
Outdated
Show resolved
Hide resolved
} | ||
} | ||
} | ||
addActions(startAction, blockEndActions, executableNodeActions, args, cancellationToken); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the lambdas end up doing things like:
data.blockEndActions?.AddAll(data.scope.CodeBlockEndActions);
data.syntaxNodeActions?.AddRange(data.scope.SyntaxNodeActions);
So plural seemed sensible to me. I don't feel hugely strongly.
@dotnet/roslyn-compiler ptal. This is also a precursor step to some more work todd and i are doing to take some of these core hot hot hot loops and reduce costs/allocaitons there. This means updating them to not use IEnumerables as those both box ImmutableArrays, and they have expensive costs for enumeration. These are things we've seen in traces involving large files with lots of analyzers. Thanks! |
var analyzerActionsByKind = !nodeActions.IsEmpty ? | ||
AnalyzerExecutor.GetNodeActionsByKind(nodeActions) : | ||
ImmutableSegmentedDictionary<TLanguageKindEnum, ImmutableArray<SyntaxNodeAnalyzerAction<TLanguageKindEnum>>>.Empty; | ||
var analyzerActionsByKind = AnalyzerExecutor.GetNodeActionsByKind(nodeActions); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
swithcing to a temp array builder allows us to only accept ArrayBuilders in GetNodeActionsByKind instead of needing to take an IEnumerable ot handle ArrayBuilders or ImmutableArrays. This also avoids intermediary copies of data.
Debug.Assert(!executableCodeBlocks.IsEmpty); | ||
|
||
// The actions we discover in 'addActions' and then execute in 'executeActions'. | ||
var ephemeralActions = ArrayBuilder<SyntaxNodeAnalyzerAction<TLanguageKindEnum>>.GetInstance(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this was pulled out of the callee as it prevented being able to do inference on the type arguments passed to it.
data => data.action(data.context), | ||
(action: codeBlockAction.Action, context: context), | ||
static data => data.action(data.context), | ||
argument: (action: codeBlockAction.Action, context), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
made lambdas explicitly static to make it clear they must not capture naything (since that would defeat the purpose). my followup pr applies this to all the lambdas in this file.
{ | ||
return _syntaxNodeActions.OfType<SyntaxNodeAnalyzerAction<TLanguageKindEnum>>().ToImmutableArray(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
linq all over the place.
@@ -836,8 +842,6 @@ internal readonly ImmutableArray<SyntaxNodeAnalyzerAction<TLanguageKindEnum>> Ge | |||
builder.Add(syntaxNodeAction); | |||
} | |||
} | |||
|
|||
return builder.ToImmutableAndFree(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
copies all over the place. we realy want to avoid that while doing our processing.
@RikkiGibson @jcouv this is ready for eyes. |
var (@this, startActions, analyzer, declaredNode, operationBlocks, declaredSymbol, operations, semanticModel, filterSpan, isGeneratedCode, ephemeralActions) = args; | ||
var scope = new HostOperationBlockStartAnalysisScope(startAction.Analyzer); | ||
var startContext = new AnalyzerOperationBlockStartAnalysisContext( | ||
scope, operationBlocks, declaredSymbol, semanticModel.Compilation, @this.AnalyzerOptions, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note: some code uses semanticModel.Compilation, and some code uses this.Compilation. It would probably be better if we did things consistently. I would like to move that to a followup.
@dotnet/roslyn-compiler this is ready for review. |
foreach (var blockAction in blockActions) | ||
{ | ||
var codeBlockAction = blockAction as CodeBlockAnalyzerAction; | ||
if (codeBlockAction != null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all this logic moved into the executeBlockActions
callbacks, so that it can all be strongly typed, without needing runtime type checks to determine what to do.
@RikkiGibson @jcouv @jjonescz this is ready for review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM Thanks (iteration 52)
Found when investigating traces involving lots of analyzers in large projects:
Related pr here: #76894
Todd is fixing up heavy CPU on the dictionary lookups. But we're also seeing enough significance in the use of IEnumerable everywhere here. The main reason the code was written to use IEnuemrable was because some codepths used ImmutableArrays and some ArrayBuilders. I've since separated these all out to consistently use one or the other (immutable when we copute and cache the data, and AB when we build up scratch results we want to process). This is very valuable as it means we're not boxing immutable arrays, nor are we getting boxed iterators as we keep walking the syntax and iop trees.
Doing this revealed some GNARLY code in the analyze driver for processing both SyntaxNode and IOps in a similar fashion. I"ve refactored that code heavily to be entirely typesafe and to not require the core code to have runtime type checks of the generic values within it to customize behavior.