-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Reduce CPU costs under AnalyzerExecutor.ExecuteSyntaxNodeActions #76894
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
Changes from all commits
5190cd5
841bcbc
ec95ec4
805be72
73a6bfc
7cefaf9
24429f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
using System.Collections.Immutable; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis.Collections; | ||
using Microsoft.CodeAnalysis.PooledObjects; | ||
using Roslyn.Utilities; | ||
|
||
|
@@ -17,16 +18,25 @@ internal partial class AnalyzerDriver<TLanguageKindEnum> : AnalyzerDriver where | |
/// </summary> | ||
private sealed class GroupedAnalyzerActions : IGroupedAnalyzerActions | ||
{ | ||
public static readonly GroupedAnalyzerActions Empty = new GroupedAnalyzerActions(ImmutableArray<(DiagnosticAnalyzer, GroupedAnalyzerActionsForAnalyzer)>.Empty, AnalyzerActions.Empty); | ||
public static readonly GroupedAnalyzerActions Empty = new GroupedAnalyzerActions( | ||
ImmutableArray<(DiagnosticAnalyzer, GroupedAnalyzerActionsForAnalyzer)>.Empty, | ||
ImmutableSegmentedDictionary<TLanguageKindEnum, ImmutableArray<DiagnosticAnalyzer>>.Empty, | ||
AnalyzerActions.Empty); | ||
|
||
private GroupedAnalyzerActions(ImmutableArray<(DiagnosticAnalyzer, GroupedAnalyzerActionsForAnalyzer)> groupedActionsAndAnalyzers, in AnalyzerActions analyzerActions) | ||
private GroupedAnalyzerActions( | ||
ImmutableArray<(DiagnosticAnalyzer, GroupedAnalyzerActionsForAnalyzer)> groupedActionsAndAnalyzers, | ||
ImmutableSegmentedDictionary<TLanguageKindEnum, ImmutableArray<DiagnosticAnalyzer>> analyzersByKind, | ||
in AnalyzerActions analyzerActions) | ||
{ | ||
GroupedActionsByAnalyzer = groupedActionsAndAnalyzers; | ||
AnalyzerActions = analyzerActions; | ||
AnalyzersByKind = analyzersByKind; | ||
} | ||
|
||
public ImmutableArray<(DiagnosticAnalyzer analyzer, GroupedAnalyzerActionsForAnalyzer groupedActions)> GroupedActionsByAnalyzer { get; } | ||
|
||
public ImmutableSegmentedDictionary<TLanguageKindEnum, ImmutableArray<DiagnosticAnalyzer>> AnalyzersByKind { get; } | ||
|
||
public AnalyzerActions AnalyzerActions { get; } | ||
|
||
public bool IsEmpty | ||
|
@@ -48,7 +58,8 @@ public static GroupedAnalyzerActions Create(DiagnosticAnalyzer analyzer, in Anal | |
|
||
var groupedActions = new GroupedAnalyzerActionsForAnalyzer(analyzer, analyzerActions, analyzerActionsNeedFiltering: false); | ||
var groupedActionsAndAnalyzers = ImmutableArray<(DiagnosticAnalyzer, GroupedAnalyzerActionsForAnalyzer)>.Empty.Add((analyzer, groupedActions)); | ||
return new GroupedAnalyzerActions(groupedActionsAndAnalyzers, in analyzerActions); | ||
var analyzersByKind = CreateAnalyzersByKind(groupedActionsAndAnalyzers); | ||
return new GroupedAnalyzerActions(groupedActionsAndAnalyzers, analyzersByKind, in analyzerActions); | ||
} | ||
|
||
public static GroupedAnalyzerActions Create(ImmutableArray<DiagnosticAnalyzer> analyzers, in AnalyzerActions analyzerActions) | ||
|
@@ -58,7 +69,8 @@ public static GroupedAnalyzerActions Create(ImmutableArray<DiagnosticAnalyzer> a | |
var groups = analyzers.SelectAsArray( | ||
(analyzer, analyzerActions) => (analyzer, new GroupedAnalyzerActionsForAnalyzer(analyzer, analyzerActions, analyzerActionsNeedFiltering: true)), | ||
analyzerActions); | ||
return new GroupedAnalyzerActions(groups, in analyzerActions); | ||
var analyzersByKind = CreateAnalyzersByKind(groups); | ||
return new GroupedAnalyzerActions(groups, analyzersByKind, in analyzerActions); | ||
} | ||
|
||
IGroupedAnalyzerActions IGroupedAnalyzerActions.Append(IGroupedAnalyzerActions igroupedAnalyzerActions) | ||
|
@@ -74,7 +86,22 @@ IGroupedAnalyzerActions IGroupedAnalyzerActions.Append(IGroupedAnalyzerActions i | |
|
||
var newGroupedActions = GroupedActionsByAnalyzer.AddRange(groupedAnalyzerActions.GroupedActionsByAnalyzer); | ||
var newAnalyzerActions = AnalyzerActions.Append(groupedAnalyzerActions.AnalyzerActions); | ||
return new GroupedAnalyzerActions(newGroupedActions, newAnalyzerActions); | ||
var analyzersByKind = CreateAnalyzersByKind(newGroupedActions); | ||
return new GroupedAnalyzerActions(newGroupedActions, analyzersByKind, newAnalyzerActions); | ||
} | ||
|
||
private static ImmutableSegmentedDictionary<TLanguageKindEnum, ImmutableArray<DiagnosticAnalyzer>> CreateAnalyzersByKind(ImmutableArray<(DiagnosticAnalyzer, GroupedAnalyzerActionsForAnalyzer)> groupedActionsAndAnalyzers) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is a marow win by getting us to O(1) lookups per syntax node kind, which is an uber hot spot while doing analyzers. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A major win? Or a narrow win? I feel like I could read the typo either way :P There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lol. Major. |
||
{ | ||
var analyzersByKind = PooledDictionary<TLanguageKindEnum, ArrayBuilder<DiagnosticAnalyzer>>.GetInstance(); | ||
foreach (var (analyzer, groupedActionsForAnalyzer) in groupedActionsAndAnalyzers) | ||
{ | ||
foreach (var (kind, _) in groupedActionsForAnalyzer.NodeActionsByAnalyzerAndKind) | ||
{ | ||
analyzersByKind.AddPooled(kind, analyzer); | ||
ToddGrun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
return analyzersByKind.ToImmutableSegmentedDictionaryAndFree(); | ||
} | ||
} | ||
} | ||
|
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 just helps with code hygiene around repetition.