Skip to content

Commit

Permalink
Invoke-Formatter: Skip VariableAnalysis, which is not needed to yield…
Browse files Browse the repository at this point in the history
… a 50% performance improvement (#1451)
  • Loading branch information
bergmeister authored Apr 21, 2020
1 parent 89f2846 commit bd5a4b1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Engine/Formatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static string Format<TCmdlet>(

Range updatedRange;
bool fixesWereApplied;
text = ScriptAnalyzer.Instance.Fix(text, range, out updatedRange, out fixesWereApplied);
text = ScriptAnalyzer.Instance.Fix(text, range, out updatedRange, out fixesWereApplied, skipVariableAnalysis: true);
range = updatedRange;
}

Expand Down
26 changes: 16 additions & 10 deletions Engine/ScriptAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1499,7 +1499,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeAndFixPath(string path, Func<string,
/// </summary>
/// <param name="scriptDefinition">The script to be analyzed</param>
/// <returns></returns>
public IEnumerable<DiagnosticRecord> AnalyzeScriptDefinition(string scriptDefinition)
public IEnumerable<DiagnosticRecord> AnalyzeScriptDefinition(string scriptDefinition, bool skipVariableAnalysis = false)
{
ScriptBlockAst scriptAst = null;
Token[] scriptTokens = null;
Expand Down Expand Up @@ -1539,7 +1539,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScriptDefinition(string scriptDefini
}

// now, analyze the script definition
return diagnosticRecords.Concat(this.AnalyzeSyntaxTree(scriptAst, scriptTokens, String.Empty));
return diagnosticRecords.Concat(this.AnalyzeSyntaxTree(scriptAst, scriptTokens, String.Empty, skipVariableAnalysis));
}

/// <summary>
Expand All @@ -1566,8 +1566,9 @@ public string Fix(string scriptDefinition, out bool fixesWereApplied)
/// <param name="range">The range in which the fixes are allowed.</param>
/// <param name="updatedRange">The updated range after the fixes have been applied.</param>
/// <param name="updatedRange">Whether any warnings were fixed.</param>
/// <param name="skipVariableAnalysis">Whether to skip variable analysis.</param>
/// <returns>The same instance of `EditableText` that was passed to the method, but the instance encapsulates the fixed script text. This helps in chaining the Fix method.</returns>
public EditableText Fix(EditableText text, Range range, out Range updatedRange, out bool fixesWereApplied)
public EditableText Fix(EditableText text, Range range, out Range updatedRange, out bool fixesWereApplied, bool skipVariableAnalysis = false)
{
if (text == null)
{
Expand All @@ -1589,7 +1590,7 @@ public EditableText Fix(EditableText text, Range range, out Range updatedRange,
var previousUnusedCorrections = 0;
do
{
var records = AnalyzeScriptDefinition(text.ToString());
var records = AnalyzeScriptDefinition(text.ToString(), skipVariableAnalysis);
var corrections = records
.Select(r => r.SuggestedCorrections)
.Where(sc => sc != null && sc.Any())
Expand Down Expand Up @@ -2016,13 +2017,15 @@ DiagnosticRecord ruleDiagnosticRecord
/// <param name="scriptAst">The ScriptBlockAst from the parsed script.</param>
/// <param name="scriptTokens">The tokens found in the script.</param>
/// <param name="filePath">The path to the file that was parsed.
/// <param name="skipVariableAnalysis">Whether to skip variable analysis.
/// If AnalyzeSyntaxTree is called from an ast that we get from ParseInput, then this field will be String.Empty
/// </param>
/// <returns>An enumeration of DiagnosticRecords that were found by rules.</returns>
public IEnumerable<DiagnosticRecord> AnalyzeSyntaxTree(
ScriptBlockAst scriptAst,
Token[] scriptTokens,
string filePath)
string filePath,
bool skipVariableAnalysis = false)
{
Dictionary<string, List<RuleSuppression>> ruleSuppressions = new Dictionary<string,List<RuleSuppression>>();
ConcurrentBag<DiagnosticRecord> diagnostics = new ConcurrentBag<DiagnosticRecord>();
Expand Down Expand Up @@ -2053,13 +2056,16 @@ public IEnumerable<DiagnosticRecord> AnalyzeSyntaxTree(
}
}

#region Run VariableAnalysis
try
if (!skipVariableAnalysis)
{
Helper.Instance.InitializeVariableAnalysis(scriptAst);
}
catch { }
#region Run VariableAnalysis
try
{
Helper.Instance.InitializeVariableAnalysis(scriptAst);
}
catch { }
#endregion
}

Helper.Instance.Tokens = scriptTokens;
}
Expand Down

0 comments on commit bd5a4b1

Please sign in to comment.