Skip to content

Commit bd5a4b1

Browse files
authored
Invoke-Formatter: Skip VariableAnalysis, which is not needed to yield a 50% performance improvement (#1451)
1 parent 89f2846 commit bd5a4b1

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

Engine/Formatter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public static string Format<TCmdlet>(
5959

6060
Range updatedRange;
6161
bool fixesWereApplied;
62-
text = ScriptAnalyzer.Instance.Fix(text, range, out updatedRange, out fixesWereApplied);
62+
text = ScriptAnalyzer.Instance.Fix(text, range, out updatedRange, out fixesWereApplied, skipVariableAnalysis: true);
6363
range = updatedRange;
6464
}
6565

Engine/ScriptAnalyzer.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,7 +1499,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeAndFixPath(string path, Func<string,
14991499
/// </summary>
15001500
/// <param name="scriptDefinition">The script to be analyzed</param>
15011501
/// <returns></returns>
1502-
public IEnumerable<DiagnosticRecord> AnalyzeScriptDefinition(string scriptDefinition)
1502+
public IEnumerable<DiagnosticRecord> AnalyzeScriptDefinition(string scriptDefinition, bool skipVariableAnalysis = false)
15031503
{
15041504
ScriptBlockAst scriptAst = null;
15051505
Token[] scriptTokens = null;
@@ -1539,7 +1539,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScriptDefinition(string scriptDefini
15391539
}
15401540

15411541
// now, analyze the script definition
1542-
return diagnosticRecords.Concat(this.AnalyzeSyntaxTree(scriptAst, scriptTokens, String.Empty));
1542+
return diagnosticRecords.Concat(this.AnalyzeSyntaxTree(scriptAst, scriptTokens, String.Empty, skipVariableAnalysis));
15431543
}
15441544

15451545
/// <summary>
@@ -1566,8 +1566,9 @@ public string Fix(string scriptDefinition, out bool fixesWereApplied)
15661566
/// <param name="range">The range in which the fixes are allowed.</param>
15671567
/// <param name="updatedRange">The updated range after the fixes have been applied.</param>
15681568
/// <param name="updatedRange">Whether any warnings were fixed.</param>
1569+
/// <param name="skipVariableAnalysis">Whether to skip variable analysis.</param>
15691570
/// <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>
1570-
public EditableText Fix(EditableText text, Range range, out Range updatedRange, out bool fixesWereApplied)
1571+
public EditableText Fix(EditableText text, Range range, out Range updatedRange, out bool fixesWereApplied, bool skipVariableAnalysis = false)
15711572
{
15721573
if (text == null)
15731574
{
@@ -1589,7 +1590,7 @@ public EditableText Fix(EditableText text, Range range, out Range updatedRange,
15891590
var previousUnusedCorrections = 0;
15901591
do
15911592
{
1592-
var records = AnalyzeScriptDefinition(text.ToString());
1593+
var records = AnalyzeScriptDefinition(text.ToString(), skipVariableAnalysis);
15931594
var corrections = records
15941595
.Select(r => r.SuggestedCorrections)
15951596
.Where(sc => sc != null && sc.Any())
@@ -2016,13 +2017,15 @@ DiagnosticRecord ruleDiagnosticRecord
20162017
/// <param name="scriptAst">The ScriptBlockAst from the parsed script.</param>
20172018
/// <param name="scriptTokens">The tokens found in the script.</param>
20182019
/// <param name="filePath">The path to the file that was parsed.
2020+
/// <param name="skipVariableAnalysis">Whether to skip variable analysis.
20192021
/// If AnalyzeSyntaxTree is called from an ast that we get from ParseInput, then this field will be String.Empty
20202022
/// </param>
20212023
/// <returns>An enumeration of DiagnosticRecords that were found by rules.</returns>
20222024
public IEnumerable<DiagnosticRecord> AnalyzeSyntaxTree(
20232025
ScriptBlockAst scriptAst,
20242026
Token[] scriptTokens,
2025-
string filePath)
2027+
string filePath,
2028+
bool skipVariableAnalysis = false)
20262029
{
20272030
Dictionary<string, List<RuleSuppression>> ruleSuppressions = new Dictionary<string,List<RuleSuppression>>();
20282031
ConcurrentBag<DiagnosticRecord> diagnostics = new ConcurrentBag<DiagnosticRecord>();
@@ -2053,13 +2056,16 @@ public IEnumerable<DiagnosticRecord> AnalyzeSyntaxTree(
20532056
}
20542057
}
20552058

2056-
#region Run VariableAnalysis
2057-
try
2059+
if (!skipVariableAnalysis)
20582060
{
2059-
Helper.Instance.InitializeVariableAnalysis(scriptAst);
2060-
}
2061-
catch { }
2061+
#region Run VariableAnalysis
2062+
try
2063+
{
2064+
Helper.Instance.InitializeVariableAnalysis(scriptAst);
2065+
}
2066+
catch { }
20622067
#endregion
2068+
}
20632069

20642070
Helper.Instance.Tokens = scriptTokens;
20652071
}

0 commit comments

Comments
 (0)