Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

Commit 821e0c5

Browse files
author
Mikhail Arkhipov
committed
Remove deque & limit analysis passes
1 parent b29a87f commit 821e0c5

8 files changed

Lines changed: 34 additions & 431 deletions

File tree

src/Analysis/Engine/Impl/AnalysisLog.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// permissions and limitations under the License.
1616

1717
using System;
18+
using System.Collections.Generic;
1819
using Microsoft.PythonTools.Analysis.Values;
1920

2021
namespace Microsoft.PythonTools.Analysis {
@@ -76,13 +77,13 @@ public static void Reset() {
7677
LastDisplayedTime = null;
7778
}
7879

79-
public static void Enqueue(Deque<AnalysisUnit> deque, AnalysisUnit unit) {
80+
public static void Enqueue(Queue<AnalysisUnit> deque, AnalysisUnit unit) {
8081
if (Active) {
8182
Add("E", IdDispenser.GetId(unit), deque.Count);
8283
}
8384
}
8485

85-
public static void Dequeue(Deque<AnalysisUnit> deque, AnalysisUnit unit) {
86+
public static void Dequeue(Queue<AnalysisUnit> deque, AnalysisUnit unit) {
8687
if (Active) {
8788
Add("D", IdDispenser.GetId(unit), deque.Count);
8889
}
@@ -108,7 +109,7 @@ public static void ExceedsTypeLimit(string variableDefType, int total, string co
108109
Add("X", variableDefType, total, contents);
109110
}
110111

111-
public static void Cancelled(Deque<AnalysisUnit> queue) {
112+
public static void Cancelled(Queue<AnalysisUnit> queue) {
112113
Add("Cancel", queue.Count);
113114
}
114115

src/Analysis/Engine/Impl/AnalysisUnit.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public AnalysisUnit CopyForEval() {
109109
}
110110

111111
internal void Enqueue() {
112-
if (!ForEval && !IsInQueue && !_suppressEnqueue) {
112+
if (!ForEval && !IsInQueue && !_suppressEnqueue && NeedsAnalysis()) {
113113
State.Queue.Append(this);
114114
AnalysisLog.Enqueue(State.Queue, this);
115115
IsInQueue = true;
@@ -120,6 +120,10 @@ internal void Enqueue() {
120120
}
121121
}
122122

123+
private bool NeedsAnalysis()
124+
=> _analysisCount == 0 || _scope != null && _scope.AllVariables.Select(k => k.Value).Any(v => !v.HasTypes);
125+
126+
123127
/// <summary>
124128
/// The AST which will be analyzed when this node is analyzed
125129
/// </summary>
@@ -361,8 +365,7 @@ public ClassAnalysisUnit(ClassDefinition node, InterpreterScope declScope, Analy
361365
public new ClassDefinition Ast => (ClassDefinition)base.Ast;
362366

363367
internal override void AnalyzeWorker(DDG ddg, CancellationToken cancel) {
364-
InterpreterScope scope;
365-
if (!ddg.Scope.TryGetNodeScope(Ast, out scope)) {
368+
if (!ddg.Scope.TryGetNodeScope(Ast, out var scope)) {
366369
return;
367370
}
368371

@@ -418,8 +421,7 @@ private IAnalysisSet ProcessClassDecorators(DDG ddg, ClassInfo classInfo) {
418421
foreach (var d in Ast.Decorators.Decorators.ExcludeDefault()) {
419422
var decorator = ddg._eval.Evaluate(d);
420423

421-
Expression nextExpr;
422-
if (!_decoratorCalls.TryGetValue(d, out nextExpr)) {
424+
if (!_decoratorCalls.TryGetValue(d, out var nextExpr)) {
423425
nextExpr = _decoratorCalls[d] = new CallExpression(d, new[] { new Arg(expr) });
424426
nextExpr.SetLoc(d.IndexSpan);
425427
}

src/Analysis/Engine/Impl/Analyzer/DDG.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ internal class DDG : PythonWalker {
3030
private SuiteStatement _curSuite;
3131
public readonly HashSet<IPythonProjectEntry> AnalyzedEntries = new HashSet<IPythonProjectEntry>();
3232

33-
public void Analyze(Deque<AnalysisUnit> queue, CancellationToken cancel, Action<int> reportQueueSize = null, int reportQueueInterval = 1) {
33+
public void Analyze(Queue<AnalysisUnit> queue, CancellationToken cancel, Action<int> reportQueueSize = null, int reportQueueInterval = 1) {
3434
if (cancel.IsCancellationRequested) {
3535
return;
3636
}
@@ -46,7 +46,7 @@ public void Analyze(Deque<AnalysisUnit> queue, CancellationToken cancel, Action<
4646
}
4747

4848
while (queue.Count > 0 && !cancel.IsCancellationRequested) {
49-
_unit = queue.PopLeft();
49+
_unit = queue.Dequeue();
5050

5151
if (_unit == endOfQueueMarker) {
5252
AnalysisLog.EndOfQueue(queueCountAtStart, queue.Count);
@@ -107,10 +107,9 @@ public InterpreterScope Scope {
107107
public PythonAnalyzer ProjectState => _unit.State;
108108

109109
public override bool Walk(PythonAst node) {
110-
ModuleReference existingRef;
111110
Debug.Assert(node == _unit.Ast);
112111

113-
if (!ProjectState.Modules.TryImport(_unit.DeclaringModule.Name, out existingRef)) {
112+
if (!ProjectState.Modules.TryImport(_unit.DeclaringModule.Name, out var existingRef)) {
114113
// publish our module ref now so that we don't collect dependencies as we'll be fully processed
115114
if (existingRef == null) {
116115
ProjectState.Modules[_unit.DeclaringModule.Name] = new ModuleReference(_unit.DeclaringModule, _unit.DeclaringModule.Name);
@@ -471,8 +470,7 @@ internal List<AnalysisValue> LookupBaseMethods(string name, IEnumerable<IAnalysi
471470
}
472471

473472
public override bool Walk(FunctionDefinition node) {
474-
InterpreterScope funcScope;
475-
if (_unit.InterpreterScope.TryGetNodeScope(node, out funcScope)) {
473+
if (_unit.InterpreterScope.TryGetNodeScope(node, out var funcScope)) {
476474
var function = ((FunctionScope)funcScope).Function;
477475
var analysisUnit = (FunctionAnalysisUnit)((FunctionScope)funcScope).Function.AnalysisUnit;
478476

@@ -632,8 +630,7 @@ public override bool Walk(AssertStatement node) {
632630
}
633631

634632
private void TryPushIsInstanceScope(Node node, Expression test) {
635-
InterpreterScope newScope;
636-
if (Scope.TryGetNodeScope(node, out newScope)) {
633+
if (Scope.TryGetNodeScope(node, out var newScope)) {
637634
var outerScope = Scope;
638635
var isInstanceScope = (IsInstanceScope)newScope;
639636

src/Analysis/Engine/Impl/Analyzer/DependentKeyValue.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,11 @@ public bool AddTypes(IProjectEntry projectEntry, PythonAnalyzer projectState, IE
5151

5252
bool anyAdded = false;
5353
foreach (var key in keyTypes) {
54-
IAnalysisSet values;
55-
if (!dependencies.KeyValues.TryGetValue(key, out values)) {
54+
if (!dependencies.KeyValues.TryGetValue(key, out var values)) {
5655
values = AnalysisSet.Create(valueTypes);
5756
anyAdded = true;
5857
} else {
59-
bool added;
60-
values = values.Union(valueTypes, out added);
58+
values = values.Union(valueTypes, out var added);
6159
anyAdded |= added;
6260
}
6361
if (anyAdded && values.Count > projectState.Limits.DictValueTypes) {
@@ -163,8 +161,7 @@ public Dictionary<AnalysisValue, IAnalysisSet> KeyValueTypes {
163161
}
164162
} else {
165163
foreach (var keyValue in mod.KeyValues) {
166-
IAnalysisSet existing;
167-
if (!res.TryGetValue(keyValue.Key, out existing)) {
164+
if (!res.TryGetValue(keyValue.Key, out var existing)) {
168165
res[keyValue.Key] = keyValue.Value;
169166
} else {
170167
res[keyValue.Key] = existing.Union(keyValue.Value, canMutate: false);
@@ -194,13 +191,11 @@ internal bool CopyFrom(DependentKeyValue dependentKeyValue, bool enqueue = true)
194191
}
195192

196193
foreach (var keyValue in otherDependency.Value.KeyValues) {
197-
IAnalysisSet union;
198-
if (!deps.KeyValues.TryGetValue(keyValue.Key, out union)) {
194+
if (!deps.KeyValues.TryGetValue(keyValue.Key, out var union)) {
199195
deps.KeyValues[keyValue.Key] = union = keyValue.Value;
200196
anyAdded = true;
201197
} else {
202-
bool added;
203-
deps.KeyValues[keyValue.Key] = union.Union(keyValue.Value, out added, canMutate: false);
198+
deps.KeyValues[keyValue.Key] = union.Union(keyValue.Value, out var added, canMutate: false);
204199
anyAdded |= added;
205200
}
206201
}

0 commit comments

Comments
 (0)