@@ -21,6 +21,8 @@ namespace Rubberduck.Inspections
21
21
{
22
22
public class Inspector : IInspector
23
23
{
24
+ private const int _maxDegreeOfInspectionParallelism = - 1 ;
25
+
24
26
private readonly IGeneralConfigService _configService ;
25
27
private readonly List < IInspection > _inspections ;
26
28
private readonly int AGGREGATION_THRESHOLD = 128 ;
@@ -90,29 +92,22 @@ public async Task<IEnumerable<IInspectionResult>> FindIssuesAsync(RubberduckPars
90
92
}
91
93
}
92
94
93
- var inspections = _inspections . Where ( inspection => inspection . Severity != CodeInspectionSeverity . DoNotShow )
94
- . Select ( inspection =>
95
- Task . Run ( ( ) =>
96
- {
97
- token . ThrowIfCancellationRequested ( ) ;
98
- try
99
- {
100
- var inspectionResults = inspection . GetInspectionResults ( ) ;
101
-
102
- foreach ( var inspectionResult in inspectionResults )
103
- {
104
- allIssues . Add ( inspectionResult ) ;
105
- }
106
- }
107
- catch ( Exception e )
108
- {
109
- LogManager . GetCurrentClassLogger ( ) . Warn ( e ) ;
110
- }
111
- } , token ) ) . ToList ( ) ;
95
+ var inspectionsToRun = _inspections . Where ( inspection => inspection . Severity != CodeInspectionSeverity . DoNotShow ) ;
112
96
113
97
try
114
98
{
115
- await Task . WhenAll ( inspections ) ;
99
+ await Task . Run ( ( ) => RunInspectionsInParallel ( inspectionsToRun , allIssues , token ) ) ;
100
+ }
101
+ catch ( AggregateException exception )
102
+ {
103
+ if ( exception . Flatten ( ) . InnerExceptions . All ( ex => ex is OperationCanceledException ) )
104
+ {
105
+ LogManager . GetCurrentClassLogger ( ) . Debug ( "Inspections got canceled." ) ;
106
+ }
107
+ else
108
+ {
109
+ LogManager . GetCurrentClassLogger ( ) . Error ( exception ) ;
110
+ }
116
111
}
117
112
catch ( Exception e )
118
113
{
@@ -131,6 +126,38 @@ public async Task<IEnumerable<IInspectionResult>> FindIssuesAsync(RubberduckPars
131
126
return results ;
132
127
}
133
128
129
+ private static void RunInspectionsInParallel ( IEnumerable < IInspection > inspectionsToRun ,
130
+ ConcurrentBag < IInspectionResult > allIssues , CancellationToken token )
131
+ {
132
+ var options = new ParallelOptions
133
+ {
134
+ CancellationToken = token ,
135
+ MaxDegreeOfParallelism = _maxDegreeOfInspectionParallelism
136
+ } ;
137
+
138
+ Parallel . ForEach ( inspectionsToRun ,
139
+ options ,
140
+ inspection => RunInspection ( inspection , allIssues )
141
+ ) ;
142
+ }
143
+
144
+ private static void RunInspection ( IInspection inspection , ConcurrentBag < IInspectionResult > allIssues )
145
+ {
146
+ try
147
+ {
148
+ var inspectionResults = inspection . GetInspectionResults ( ) ;
149
+
150
+ foreach ( var inspectionResult in inspectionResults )
151
+ {
152
+ allIssues . Add ( inspectionResult ) ;
153
+ }
154
+ }
155
+ catch ( Exception e )
156
+ {
157
+ LogManager . GetCurrentClassLogger ( ) . Warn ( e ) ;
158
+ }
159
+ }
160
+
134
161
private void WalkTrees ( CodeInspectionSettings settings , RubberduckParserState state , IEnumerable < IParseTreeInspection > inspections , ParsePass pass )
135
162
{
136
163
var listeners = inspections
0 commit comments