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

Commit 4a2fd79

Browse files
author
Mikhail Arkhipov
authored
Treat persistent module as regular specialized (#1334)
* Remove old qualified name * Node storage * Class and scope to use AST map * Library analysis * Fix SO * Keep small AST with imports * AST reduction * Final field * Initial * Reload * Ignore post-final requests * Drop AST * Remove local variables * Test fixes * Fix overload match * Tests * Add locks * Remove local variables * Drop file content to save memory * Cache PEP hints * Recreate AST * Fix specialization * Fix locations * usings * Test fixes * Add options to keep data in memory * Fix test * Fix lambda parameters * Fix argument set Fix global scope node * Fix overload doc * Fix stub merge errors * Fix async issues * Undo some changes * Fix test * Fix race condition * Partial * Models and views * Restore log null checks * Fix merge conflict * Fix merge issue * Null check * Partial * Partial * Partial * Fix test * Partial * Partial * First test * Baseline comparison * Builtins * Partial * Type fixes * Fix type names, part I * Qualified name * Properly write variables * Partial * Construct module from model * Test * Variable creations * Factories * Factories * Split construction * Restore * Save builtins * Test passes * Qualified name * Better export detection * Test fixes * More consistent qualified names * Sys test * Demo * Complete sys write/read * Partial * Partial * Test staility * Perf bug * Baseline, remove debug code, deactivate db * Test fixes * Test fix * Simplify a bit * Baselines and use : separator * Baselines * PR feedback * Merge master * Remove registry reference * PR feedback * PR feedback * Restore persistence + update test * Better handle persistent module in dependencies * Undo * Fix merge issue * Remove debug code
1 parent ab8ecaf commit 4a2fd79

File tree

10 files changed

+36
-23
lines changed

10 files changed

+36
-23
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ private enum KeyType { Default, Typeshed, LibraryAsDocument }
3434
public AnalysisModuleKey(IPythonModule module) {
3535
Name = module.Name;
3636
FilePath = module.ModuleType == ModuleType.CompiledBuiltin ? null : module.FilePath;
37-
_type = module is StubPythonModule stub && stub.IsTypeshed
37+
_type = module is StubPythonModule stub && stub.IsTypeshed
3838
? KeyType.Typeshed
3939
: module.ModuleType == ModuleType.Library && module is IDocument document && document.IsOpen
4040
? KeyType.LibraryAsDocument

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
using System.Threading;
2121
using System.Threading.Tasks;
2222
using Microsoft.Python.Analysis.Core.DependencyResolution;
23-
using Microsoft.Python.Analysis.Documents;
2423
using Microsoft.Python.Analysis.Modules;
2524
using Microsoft.Python.Analysis.Types;
2625
using Microsoft.Python.Core;
@@ -264,8 +263,8 @@ private HashSet<AnalysisModuleKey> FindDependencies(IPythonModule module, Python
264263
return dependencies;
265264
}
266265

267-
private static bool Ignore(IModuleManagement moduleResolution, string name)
268-
=> moduleResolution.BuiltinModuleName.EqualsOrdinal(name) || moduleResolution.GetSpecializedModule(name) != null;
266+
private static bool Ignore(IModuleManagement moduleResolution, string fullName, string modulePath)
267+
=> moduleResolution.BuiltinModuleName.EqualsOrdinal(fullName) || moduleResolution.GetSpecializedModule(fullName, modulePath) != null;
269268

270269
private void UpdateAnalysisTcs(int analysisVersion) {
271270
_analysisVersion = analysisVersion;
@@ -332,10 +331,10 @@ public override bool Walk(FromImportStatement fromImport) {
332331

333332
private void HandleSearchResults(IImportSearchResult searchResult) {
334333
switch (searchResult) {
335-
case ModuleImport moduleImport when !Ignore(_moduleResolution, moduleImport.FullName):
334+
case ModuleImport moduleImport when !Ignore(_moduleResolution, moduleImport.FullName, moduleImport.ModulePath):
336335
Dependencies.Add(new AnalysisModuleKey(moduleImport.FullName, moduleImport.ModulePath, _isTypeshed));
337336
return;
338-
case PossibleModuleImport possibleModuleImport when !Ignore(_moduleResolution, possibleModuleImport.PrecedingModuleFullName):
337+
case PossibleModuleImport possibleModuleImport when !Ignore(_moduleResolution, possibleModuleImport.PrecedingModuleFullName, possibleModuleImport.PrecedingModulePath):
339338
Dependencies.Add(new AnalysisModuleKey(possibleModuleImport.PrecedingModuleFullName, possibleModuleImport.PrecedingModulePath, _isTypeshed));
340339
return;
341340
default:

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ internal sealed class PythonAnalyzerSession {
4747
private readonly IPythonAnalyzer _analyzer;
4848
private readonly ILogger _log;
4949
private readonly ITelemetryService _telemetry;
50+
private readonly IModuleDatabaseService _moduleDatabaseService;
5051

5152
private State _state;
5253
private bool _isCanceled;
@@ -86,6 +87,7 @@ public PythonAnalyzerSession(IServiceManager services,
8687
_analyzer = _services.GetService<IPythonAnalyzer>();
8788
_log = _services.GetService<ILogger>();
8889
_telemetry = _services.GetService<ITelemetryService>();
90+
_moduleDatabaseService = _services.GetService<IModuleDatabaseService>();
8991
_progress = progress;
9092
}
9193

@@ -297,7 +299,6 @@ private void AnalyzeEntry() {
297299
}
298300

299301
var startTime = stopWatch?.Elapsed ?? TimeSpan.Zero;
300-
301302
AnalyzeEntry(null, _entry, module, ast, Version);
302303

303304
LogCompleted(null, module, stopWatch, startTime);
@@ -383,8 +384,12 @@ private IDocumentAnalysis CreateAnalysis(IDependencyChainNode<PythonAnalyzerEntr
383384

384385
ast.Reduce(x => x is ImportStatement || x is FromImportStatement);
385386
document.SetAst(ast);
387+
386388
var eval = new ExpressionEval(walker.Eval.Services, document, ast);
387-
return new LibraryAnalysis(document, version, walker.GlobalScope, eval, walker.StarImportMemberNames);
389+
var analysis = new LibraryAnalysis(document, version, walker.GlobalScope, eval, walker.StarImportMemberNames);
390+
391+
_moduleDatabaseService?.StoreModuleAnalysisAsync(analysis, CancellationToken.None).DoNotWait();
392+
return analysis;
388393
}
389394

390395
private enum State {

src/Analysis/Ast/Impl/Extensions/NodeExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public static LocationInfo GetLocation(this Node node, IExpressionEvaluator eval
2626

2727
return GetLocation(node, eval.Ast, eval.Module);
2828
}
29-
3029
public static LocationInfo GetLocation(this Node node, IDocumentAnalysis analysis) {
3130
if (node == null || node.StartIndex >= node.EndIndex) {
3231
return LocationInfo.Empty;

src/Analysis/Ast/Impl/Modules/Definitions/IModuleManagement.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public interface IModuleManagement: IModuleResolution {
5656
/// <summary>
5757
/// Returns specialized module, if any.
5858
/// </summary>
59-
IPythonModule GetSpecializedModule(string name);
59+
IPythonModule GetSpecializedModule(string name, string modulePath = null);
6060

6161
/// <summary>
6262
/// Root directory of the path resolver.

src/Analysis/Ast/Impl/Modules/PythonModule.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ protected PythonModule(string name, ModuleType moduleType, IServiceContainer ser
7979
Log = services.GetService<ILogger>();
8080
Interpreter = services.GetService<IPythonInterpreter>();
8181
Analysis = new EmptyAnalysis(services, this);
82+
GlobalScope = Analysis.GlobalScope;
8283

8384
_diagnosticsService = services.GetService<IDiagnosticsService>();
8485
SetDeclaringModule(this);
@@ -150,10 +151,10 @@ public virtual string Documentation {
150151
#endregion
151152

152153
#region IMemberContainer
153-
public virtual IMember GetMember(string name) => Analysis.GlobalScope.Variables[name]?.Value;
154+
public virtual IMember GetMember(string name) => GlobalScope.Variables[name]?.Value;
154155
public virtual IEnumerable<string> GetMemberNames() {
155156
// drop imported modules and typing.
156-
return Analysis.GlobalScope.Variables
157+
return GlobalScope.Variables
157158
.Where(v => {
158159
// Instances are always fine.
159160
if (v.Value is IPythonInstance) {
@@ -209,6 +210,8 @@ protected virtual void Dispose(bool disposing) {
209210
_disposeToken.TryMarkDisposed();
210211
var analyzer = Services.GetService<IPythonAnalyzer>();
211212
analyzer.RemoveAnalysis(this);
213+
_parseCts?.Dispose();
214+
_linkedParseCts?.Dispose();
212215
}
213216
#endregion
214217

src/Analysis/Ast/Impl/Modules/Resolution/MainModuleResolution.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
namespace Microsoft.Python.Analysis.Modules.Resolution {
3737
internal sealed class MainModuleResolution : ModuleResolutionBase, IModuleManagement {
3838
private readonly ConcurrentDictionary<string, IPythonModule> _specialized = new ConcurrentDictionary<string, IPythonModule>();
39+
private IModuleDatabaseService _dbService;
3940
private IRunningDocumentTable _rdt;
4041

4142
public MainModuleResolution(string root, IServiceContainer services)
@@ -76,11 +77,6 @@ protected override IPythonModule CreateModule(string name) {
7677
}
7778
}
7879

79-
var dbs = _services.GetService<IModuleDatabaseService>();
80-
if (dbs != null && dbs.TryCreateModule(name, moduleImport.ModulePath, out var m) != ModuleStorageState.DoesNotExist && m != null) {
81-
return m;
82-
}
83-
8480
// If there is a stub, make sure it is loaded and attached
8581
// First check stub next to the module.
8682
if (!TryCreateModuleStub(name, moduleImport.ModulePath, out var stub)) {
@@ -156,8 +152,17 @@ public IPythonModule SpecializeModule(string name, Func<string, IPythonModule> s
156152
/// <summary>
157153
/// Returns specialized module, if any.
158154
/// </summary>
159-
public IPythonModule GetSpecializedModule(string name)
160-
=> _specialized.TryGetValue(name, out var module) ? module : null;
155+
public IPythonModule GetSpecializedModule(string fullName, string modulePath = null) {
156+
if (_specialized.TryGetValue(fullName, out var module)) {
157+
return module;
158+
}
159+
var dbs = GetDbService();
160+
if (dbs != null && dbs.TryCreateModule(fullName, modulePath, out module) != ModuleStorageState.DoesNotExist && module != null) {
161+
SpecializeModule(fullName, s => module);
162+
return module;
163+
}
164+
return null;
165+
}
161166

162167
internal async Task LoadBuiltinTypesAsync(CancellationToken cancellationToken = default) {
163168
var analyzer = _services.GetService<IPythonAnalyzer>();
@@ -237,5 +242,8 @@ private bool TryCreateModuleStub(string name, string modulePath, out IPythonModu
237242

238243
private IRunningDocumentTable GetRdt()
239244
=> _rdt ?? (_rdt = _services.GetService<IRunningDocumentTable>());
245+
246+
private IModuleDatabaseService GetDbService()
247+
=> _dbService ?? (_dbService = _services.GetService<IModuleDatabaseService>());
240248
}
241249
}

src/Analysis/Ast/Impl/Modules/Resolution/ModuleResolutionBase.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
using Microsoft.Python.Analysis.Caching;
2222
using Microsoft.Python.Analysis.Core.DependencyResolution;
2323
using Microsoft.Python.Analysis.Core.Interpreter;
24-
using Microsoft.Python.Analysis.Documents;
2524
using Microsoft.Python.Analysis.Types;
2625
using Microsoft.Python.Core;
2726
using Microsoft.Python.Core.IO;

src/Analysis/Ast/Impl/Types/PythonPropertyType.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ public string Description {
5151
}
5252

5353
public override IMember Call(IPythonInstance instance, string memberName, IArgumentSet args)
54-
=> _getter?.Call(args, instance?.GetPythonType() ?? DeclaringType);
54+
=> _getter.Call(args, instance?.GetPythonType() ?? DeclaringType);
5555

56-
public IMember ReturnType => _getter?.Call(ArgumentSet.WithoutContext, DeclaringType)?.GetPythonType();
56+
public IMember ReturnType => _getter?.Call(ArgumentSet.WithoutContext, DeclaringType);
5757
#endregion
5858

5959
internal void AddOverload(IPythonFunctionOverload overload) => _getter = _getter ?? overload;

src/Caching/Test/Files/Requests.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@
208208
"Name": "Session"
209209
},
210210
{
211-
"Value": "i:typing:Any",
211+
"Value": "i:requests.structures:LookupDict",
212212
"Id": 753305199,
213213
"Name": "codes"
214214
},

0 commit comments

Comments
 (0)