Skip to content

Commit a401984

Browse files
authored
Merge pull request #3521 from MDoerner/SmallProcessAdjustments
Small process adjustments
2 parents fdcb3c6 + 86b6f6c commit a401984

File tree

3 files changed

+86
-42
lines changed

3 files changed

+86
-42
lines changed

Rubberduck.Inspections/Concrete/BooleanAssignedInIfElseInspection.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,12 @@ public void ClearContexts()
4848

4949
public override void ExitIfStmt(VBAParser.IfStmtContext context)
5050
{
51-
if (context.elseIfBlock().Any())
51+
if (context.elseIfBlock() != null && context.elseIfBlock().Any())
52+
{
53+
return;
54+
}
55+
56+
if (context.elseBlock() == null)
5257
{
5358
return;
5459
}
@@ -60,6 +65,7 @@ public override void ExitIfStmt(VBAParser.IfStmtContext context)
6065
}
6166

6267
// make sure the assignments are the opposite
68+
6369
if (!(ParserRuleContextHelper.GetDescendent<VBAParser.BooleanLiteralIdentifierContext>(context.block()).GetText() == Tokens.True ^
6470
ParserRuleContextHelper.GetDescendent<VBAParser.BooleanLiteralIdentifierContext>(context.elseBlock().block()).GetText() == Tokens.True))
6571
{

Rubberduck.Inspections/Inspector.cs

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ namespace Rubberduck.Inspections
2121
{
2222
public class Inspector : IInspector
2323
{
24+
private const int _maxDegreeOfInspectionParallelism = -1;
25+
2426
private readonly IGeneralConfigService _configService;
2527
private readonly List<IInspection> _inspections;
2628
private readonly int AGGREGATION_THRESHOLD = 128;
@@ -90,29 +92,22 @@ public async Task<IEnumerable<IInspectionResult>> FindIssuesAsync(RubberduckPars
9092
}
9193
}
9294

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);
11296

11397
try
11498
{
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+
}
116111
}
117112
catch (Exception e)
118113
{
@@ -131,6 +126,38 @@ public async Task<IEnumerable<IInspectionResult>> FindIssuesAsync(RubberduckPars
131126
return results;
132127
}
133128

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+
134161
private void WalkTrees(CodeInspectionSettings settings, RubberduckParserState state, IEnumerable<IParseTreeInspection> inspections, ParsePass pass)
135162
{
136163
var listeners = inspections

Rubberduck.Parsing/VBA/ParseCoordinator.cs

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Concurrent;
23
using System.Collections.Generic;
34
using System.Threading;
45
using System.Threading.Tasks;
@@ -64,9 +65,8 @@ public ParseCoordinator(
6465
}
6566

6667
// Do not access this from anywhere but ReparseRequested.
67-
// ReparseRequested needs to have a reference to all the cancellation tokens,
68-
// but the cancelees need to use their own token.
69-
private readonly List<CancellationTokenSource> _cancellationTokens = new List<CancellationTokenSource> { new CancellationTokenSource() };
68+
// ReparseRequested needs to have a reference to the cancellation token.
69+
private CancellationTokenSource _currentCancellationTokenSource = new CancellationTokenSource();
7070

7171
private readonly object _cancellationSyncObject = new object();
7272
private readonly object _parsingRunSyncObject = new object();
@@ -77,9 +77,17 @@ private void ReparseRequested(object sender, EventArgs e)
7777
lock (_cancellationSyncObject)
7878
{
7979
Cancel();
80-
token = _cancellationTokens[0].Token;
80+
81+
if (_currentCancellationTokenSource == null)
82+
{
83+
Logger.Error("Tried to request a parse after the final cancellation.");
84+
return;
85+
}
86+
87+
token = _currentCancellationTokenSource.Token;
8188
}
8289

90+
8391
if (!_isTestScope)
8492
{
8593
Task.Run(() => ParseAll(sender, token), token);
@@ -92,15 +100,19 @@ private void ReparseRequested(object sender, EventArgs e)
92100

93101
private void Cancel(bool createNewTokenSource = true)
94102
{
95-
lock (_cancellationTokens[0])
103+
lock (_cancellationSyncObject)
96104
{
97-
_cancellationTokens[0].Cancel();
98-
_cancellationTokens[0].Dispose();
99-
if (createNewTokenSource)
105+
if (_currentCancellationTokenSource == null)
100106
{
101-
_cancellationTokens.Add(new CancellationTokenSource());
107+
Logger.Error("Tried to cancel a parse after the final cancellation.");
108+
return;
102109
}
103-
_cancellationTokens.RemoveAt(0);
110+
111+
var oldTokenSource = _currentCancellationTokenSource;
112+
_currentCancellationTokenSource = createNewTokenSource ? new CancellationTokenSource() : null;
113+
114+
oldTokenSource.Cancel();
115+
oldTokenSource.Dispose();
104116
}
105117
}
106118

@@ -114,18 +126,17 @@ public void Parse(CancellationTokenSource token)
114126
ParseInternal(token.Token);
115127
}
116128

117-
private void SetSavedCancellationTokenSource(CancellationTokenSource token)
129+
/// <summary>
130+
/// For the use of tests only
131+
/// </summary>
132+
///
133+
private void SetSavedCancellationTokenSource(CancellationTokenSource tokenSource)
118134
{
119-
if (_cancellationTokens.Any())
120-
{
121-
_cancellationTokens[0].Cancel();
122-
_cancellationTokens[0].Dispose();
123-
_cancellationTokens[0] = token;
124-
}
125-
else
126-
{
127-
_cancellationTokens.Add(token);
128-
}
135+
var oldTokenSource = _currentCancellationTokenSource;
136+
_currentCancellationTokenSource = tokenSource;
137+
138+
oldTokenSource?.Cancel();
139+
oldTokenSource?.Dispose();
129140
}
130141

131142
private void ParseInternal(CancellationToken token)

0 commit comments

Comments
 (0)