Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invoke-Formatter: Skip VariableAnalysis, which is not used by Formatter rules - this yields a 25% performance improvement #1451

Merged
Show file tree
Hide file tree
Changes from all 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
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