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

Drop file content to conserve memory #1133

Merged
merged 7 commits into from
May 30, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,15 @@ namespace Microsoft.Python.Analysis.Analyzer.Evaluation {
/// and types in a chain of scopes during analysis.
/// </summary>
internal sealed partial class ExpressionEval {
private const string _pepHintKey = "PEP Hint";

public IPythonType GetTypeFromPepHint(Node node) {
if (Ast.TryGetAttribute(node, _pepHintKey, out var typeStringObject) && typeStringObject is string typeString) {
return GetTypeFromString(typeString);
}

var location = GetLocationInfo(node);

var content = (Module as IDocument)?.Content;
if (string.IsNullOrEmpty(content) || !location.EndLine.HasValue) {
return null;
Expand Down Expand Up @@ -79,7 +86,9 @@ public IPythonType GetTypeFromPepHint(Node node) {
}

// Type alone is not a valid syntax, so we need to simulate the annotation.
var typeString = content.Substring(hintStart, i - hintStart).Trim();
typeString = content.Substring(hintStart, i - hintStart).Trim();
Ast.SetAttribute(node, _pepHintKey, typeString);

return GetTypeFromString(typeString);
}

Expand Down
4 changes: 4 additions & 0 deletions src/Analysis/Ast/Impl/Analyzer/Symbols/FunctionEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ public override void Evaluate() {
if (ctor || annotationType.IsUnknown() || Module.ModuleType == ModuleType.User) {
// Return type from the annotation is sufficient for libraries and stubs, no need to walk the body.
FunctionDefinition.Body?.Walk(this);
// For libraries remove declared local function variables to free up some memory.
if (Module.ModuleType != ModuleType.User) {
jakebailey marked this conversation as resolved.
Show resolved Hide resolved
((VariableCollection)Eval.CurrentScope.Variables).Clear();
}
}
}
Result = _function;
Expand Down
4 changes: 4 additions & 0 deletions src/Analysis/Ast/Impl/Modules/PythonModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,10 @@ public void NotifyAnalysisComplete(IDocumentAnalysis analysis) {
// as declare additional variables, etc.
OnAnalysisComplete();
ContentState = State.Analyzed;

if (ModuleType != ModuleType.User) {
_buffer.Reset(_buffer.Version, string.Empty);
}
}

// Do not report issues with libraries or stubs
Expand Down
6 changes: 6 additions & 0 deletions src/Analysis/Ast/Impl/Values/VariableCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,11 @@ internal void RemoveVariable(string name) {
_variables.Remove(name);
}
}

internal void Clear() {
lock (_syncObj) {
_variables.Clear();
}
}
}
}
4 changes: 2 additions & 2 deletions src/Parsing/Impl/Ast/PythonAst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ public override async Task WalkAsync(PythonWalkerAsync walker, CancellationToken

public PythonLanguageVersion LanguageVersion { get; }

internal bool TryGetAttribute(Node node, object key, out object value) {
public bool TryGetAttribute(Node node, object key, out object value) {
if (_attributes.TryGetValue(node, out var nodeAttrs)) {
return nodeAttrs.TryGetValue(key, out value);
}
value = null;
return false;
}

internal void SetAttribute(Node node, object key, object value) {
public void SetAttribute(Node node, object key, object value) {
if (!_attributes.TryGetValue(node, out var nodeAttrs)) {
nodeAttrs = _attributes[node] = new Dictionary<object, object>();
}
Expand Down