AnalysisContext.EnableConcurrentExecution() - what exactly does it do?

Viewed 309

I have read this method's documentation but it is fairly light on details.

Enable concurrent execution of analyzer actions registered by this analyzer.

What exactly is executed concurrently? Say I have:

public override void Initialize(AnalysisContext context)
{
    context.EnableConcurrentExecution();
    context.RegisterSyntaxNodeAction(AnalyzeNode, SyntaxKind.InvocationExpression);
}

If the code under analysis contains multiple InvocationExpressions, will each expression be concurrently analyzed on its own thread?

However, such an analyzer must ensure that its actions can execute correctly in parallel.

What constitutes an "action", and how do I ensure it executes "correctly"? My AnalyzeNode method is static, reads from immutable state, and does not write to any shared state. Is this enough?

Are there any drawbacks to enabling concurrent execution?

Or am I misunderstanding this entirely?

1 Answers

If the code under analysis contains multiple InvocationExpressions, will each expression be concurrently analyzed on its own thread?

They might be, indeed. There is obviously no guarantee that they are, there are only so many threads.

What constitutes an "action"

An "action" is a delegate that you register with methods like RegisterSyntaxNodeAction.

My AnalyzeNode method is static, reads from immutable state, and does not write to any shared state. Is this enough?

Yes.

Are there any drawbacks to enabling concurrent execution?

If your actions were to read from and write to shared state, you would either have to synchronize those accesses, or not enable concurrent execution.

Related