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

Commit f1a2efd

Browse files
author
MikhailArkhipov
committed
Add restoration from db in loop handling
1 parent 732f263 commit f1a2efd

File tree

4 files changed

+52
-24
lines changed

4 files changed

+52
-24
lines changed

src/Analysis/Ast/Impl/Analyzer/Definitions/IPythonAnalyzer.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,12 @@ public interface IPythonAnalyzer {
7171
/// Fires when analysis is complete.
7272
/// </summary>
7373
event EventHandler<AnalysisCompleteEventArgs> AnalysisComplete;
74+
75+
/// <summary>
76+
/// Attempts to restore modules analysis from database.
77+
/// </summary>
78+
/// <param name="module"></param>
79+
/// <returns></returns>
80+
IDocumentAnalysis TryRestoreCachedAnalysis(IPythonModule module);
7481
}
7582
}

src/Analysis/Ast/Impl/Analyzer/Handlers/LoopImportedVariableHandler.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
using System;
1717
using System.Collections.Generic;
18-
using System.Diagnostics;
1918
using System.Linq;
2019
using Microsoft.Python.Analysis.Analyzer.Evaluation;
2120
using Microsoft.Python.Analysis.Modules;
@@ -51,6 +50,10 @@ public IEnumerable<string> GetMemberNames(PythonVariableModule variableModule) {
5150
return default;
5251
}
5352

53+
if(module.Analysis is DocumentAnalysis) {
54+
return module.GetMemberNames();
55+
}
56+
5457
var key = new AnalysisModuleKey(module);
5558
if (_walkers.TryGetValue(key, out var walker)) {
5659
return GetMemberNames(walker, variableModule);
@@ -62,7 +65,8 @@ public IEnumerable<string> GetMemberNames(PythonVariableModule variableModule) {
6265
: variableModule.GetMemberNames().Where(s => !s.StartsWithOrdinal("_"));
6366
}
6467

65-
return GetMemberNames(WalkModule(module, ast), variableModule);
68+
walker = WalkModule(module, ast);
69+
return walker != null ? GetMemberNames(walker, variableModule) : variableModule.GetMemberNames();
6670
}
6771

6872
public IVariable GetVariable(in PythonVariableModule variableModule, in string name) {
@@ -71,6 +75,10 @@ public IVariable GetVariable(in PythonVariableModule variableModule, in string n
7175
return default;
7276
}
7377

78+
if (module.Analysis is DocumentAnalysis) {
79+
return module.Analysis.GlobalScope.Variables[name];
80+
}
81+
7482
var key = new AnalysisModuleKey(module);
7583
if (_walkers.TryGetValue(key, out var walker)) {
7684
return walker.Eval.GlobalScope?.Variables[name];
@@ -81,7 +89,8 @@ public IVariable GetVariable(in PythonVariableModule variableModule, in string n
8189
}
8290

8391
walker = WalkModule(module, ast);
84-
return walker.Eval.GlobalScope?.Variables[name];
92+
var gs = walker != null ? walker.Eval.GlobalScope : module.Analysis.GlobalScope;
93+
return gs?.Variables[name];
8594
}
8695

8796
public void EnsureModule(in PythonVariableModule variableModule) {
@@ -97,6 +106,13 @@ public void EnsureModule(in PythonVariableModule variableModule) {
97106
}
98107

99108
public ModuleWalker WalkModule(IPythonModule module, PythonAst ast) {
109+
var analyzer = _services.GetService<IPythonAnalyzer>();
110+
111+
var analysis = analyzer.TryRestoreCachedAnalysis(module);
112+
if(analysis != null) {
113+
return null;
114+
}
115+
100116
// If module has stub, make sure it is processed too.
101117
if(module.Stub?.Analysis is EmptyAnalysis) {
102118
WalkModule(module.Stub, module.GetAst());
@@ -108,6 +124,7 @@ public ModuleWalker WalkModule(IPythonModule module, PythonAst ast) {
108124
_walkers[new AnalysisModuleKey(module)] = walker;
109125
ast.Walk(walker);
110126
walker.Complete();
127+
111128
return walker;
112129
}
113130

src/Analysis/Ast/Impl/Analyzer/PythonAnalyzer.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using System.Runtime.ExceptionServices;
2121
using System.Threading;
2222
using System.Threading.Tasks;
23+
using Microsoft.Python.Analysis.Analyzer.Evaluation;
2324
using Microsoft.Python.Analysis.Caching;
2425
using Microsoft.Python.Analysis.Dependencies;
2526
using Microsoft.Python.Analysis.Diagnostics;
@@ -69,6 +70,7 @@ public void Dispose() {
6970
_disposeToken.TryMarkDisposed();
7071
}
7172

73+
#region IPythonAnalyzer
7274
public Task WaitForCompleteAnalysisAsync(CancellationToken cancellationToken = default)
7375
=> _analysisCompleteEvent.WaitAsync(cancellationToken);
7476

@@ -210,6 +212,26 @@ public IReadOnlyList<IPythonModule> LoadedModules {
210212

211213
public event EventHandler<AnalysisCompleteEventArgs> AnalysisComplete;
212214

215+
public IDocumentAnalysis TryRestoreCachedAnalysis(IPythonModule module) {
216+
var moduleType = module.ModuleType;
217+
var moduleDatabaseService = _services.GetService<IModuleDatabaseService>();
218+
if (!moduleType.CanBeCached() || moduleDatabaseService == null || !moduleDatabaseService.ModuleExistsInStorage(module.Name, module.FilePath)) {
219+
return null;
220+
}
221+
222+
if (moduleDatabaseService.TryRestoreGlobalScope(module, out var gs)) {
223+
_log?.Log(TraceEventType.Verbose, "Restored from database: ", module.Name);
224+
var analysis = new DocumentAnalysis((IDocument)module, 1, gs, new ExpressionEval(_services, module, module.GetAst()), Array.Empty<string>());
225+
gs.ReconstructVariables();
226+
return analysis;
227+
}
228+
229+
_log?.Log(TraceEventType.Verbose, "Restore from database failed for module ", module.Name);
230+
231+
return null;
232+
}
233+
#endregion
234+
213235
internal void RaiseAnalysisComplete(int moduleCount, double msElapsed) {
214236
_analysisCompleteEvent.Set();
215237
AnalysisComplete?.Invoke(this, new AnalysisCompleteEventArgs(moduleCount, msElapsed));

src/Analysis/Ast/Impl/Analyzer/PythonAnalyzerSession.cs

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ private void Analyze(IDependencyChainNode node, AsyncCountdownEvent ace, Stopwat
279279
case IDependencyChainLoopNode<PythonAnalyzerEntry> loop:
280280
try {
281281
AnalyzeLoop(loop, stopWatch);
282-
} catch (OperationCanceledException oce) {
282+
} catch (OperationCanceledException) {
283283
//loop.Value.TryCancel(oce, _walker.Version);
284284
//LogCanceled(single.Value.Module);
285285
} catch (Exception exception) {
@@ -362,7 +362,7 @@ private void AnalyzeLoop(IDependencyChainLoopNode<PythonAnalyzerEntry> loopNode,
362362

363363
var moduleKey = new AnalysisModuleKey(module);
364364
entries[moduleKey] = (module, entry);
365-
var analysis = TryRestoreCachedAnalysis(module);
365+
var analysis = _analyzer.TryRestoreCachedAnalysis(module);
366366
if (analysis != null) {
367367
AddLoopImportsFromCachedAnalysis(importNames, variables, moduleKey, analysis);
368368
cachedVariables.Add(new AnalysisModuleKey(module), analysis.GlobalScope.Variables);
@@ -547,7 +547,7 @@ private void CompleteAnalysis(PythonAnalyzerEntry entry, IPythonModule module, i
547547
}
548548

549549
private IDocumentAnalysis RestoreOrAnalyzeModule(IDependencyChainSingleNode<PythonAnalyzerEntry> node, IPythonModule module, PythonAst ast, int version) {
550-
var analysis = TryRestoreCachedAnalysis(module);
550+
var analysis = _analyzer.TryRestoreCachedAnalysis(module);
551551
if (analysis != null) {
552552
MarkNodeWalked(node);
553553
return analysis;
@@ -571,24 +571,6 @@ private bool MarkNodeWalked(IDependencyChainNode node) {
571571
return isCanceled;
572572
}
573573

574-
private IDocumentAnalysis TryRestoreCachedAnalysis(IPythonModule module) {
575-
var moduleType = module.ModuleType;
576-
if (!moduleType.CanBeCached() || _moduleDatabaseService == null || !_moduleDatabaseService.ModuleExistsInStorage(module.Name, module.FilePath)) {
577-
return null;
578-
}
579-
580-
if (_moduleDatabaseService.TryRestoreGlobalScope(module, out var gs)) {
581-
_log?.Log(TraceEventType.Verbose, "Restored from database: ", module.Name);
582-
var analysis = new DocumentAnalysis((IDocument)module, 1, gs, new ExpressionEval(_services, module, module.GetAst()), Array.Empty<string>());
583-
gs.ReconstructVariables();
584-
return analysis;
585-
}
586-
587-
_log?.Log(TraceEventType.Verbose, "Restore from database failed for module ", module.Name);
588-
589-
return null;
590-
}
591-
592574
private IDocumentAnalysis CreateAnalysis(IDependencyChainSingleNode<PythonAnalyzerEntry> node, IDocument document, PythonAst ast, int version, ModuleWalker walker) {
593575
var canHaveLibraryAnalysis = false;
594576

0 commit comments

Comments
 (0)