Skip to content

Commit 2fae803

Browse files
author
Kapil Borle
committed
Add runspace pool to improve getcommandinfo perf
1 parent 9c6e03c commit 2fae803

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

Engine/Commands/GetScriptAnalyzerRuleCommand.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,20 @@ protected override void ProcessRecord()
129129
}
130130
}
131131

132+
protected override void EndProcessing()
133+
{
134+
ScriptAnalyzer.Instance.CleanUp();
135+
Helper.Instance.CleanUp();
136+
base.EndProcessing();
137+
}
138+
139+
protected override void StopProcessing()
140+
{
141+
ScriptAnalyzer.Instance.CleanUp();
142+
Helper.Instance.CleanUp();
143+
base.StopProcessing();
144+
}
145+
132146
#endregion
133147
}
134148
}

Engine/Commands/InvokeScriptAnalyzerCommand.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,12 +337,14 @@ protected override void ProcessRecord()
337337
protected override void EndProcessing()
338338
{
339339
ScriptAnalyzer.Instance.CleanUp();
340+
Helper.Instance.CleanUp();
340341
base.EndProcessing();
341342
}
342343

343344
protected override void StopProcessing()
344345
{
345346
ScriptAnalyzer.Instance.CleanUp();
347+
Helper.Instance.CleanUp();
346348
base.StopProcessing();
347349
}
348350

Engine/Helper.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class Helper
3939
private Dictionary<string, Dictionary<string, object>> ruleArguments;
4040
private PSVersionTable psVersionTable;
4141
private Dictionary<string, CommandInfo> commandInfoCache;
42+
private RunspacePool runspacePool;
4243

4344
#endregion
4445

@@ -148,6 +149,8 @@ public void Initialize()
148149
VariableAnalysisDictionary = new Dictionary<Ast, VariableAnalysis>();
149150
ruleArguments = new Dictionary<string, Dictionary<string, object>>(StringComparer.OrdinalIgnoreCase);
150151
commandInfoCache = new Dictionary<string, CommandInfo>(StringComparer.OrdinalIgnoreCase);
152+
runspacePool = RunspaceFactory.CreateRunspacePool(InitialSessionState.CreateDefault2());
153+
runspacePool.Open();
151154

152155
IEnumerable<CommandInfo> aliases = this.invokeCommand.GetCommands("*", CommandTypes.Alias, true);
153156

@@ -166,6 +169,14 @@ public void Initialize()
166169
}
167170
}
168171

172+
/// <summary>
173+
/// We are forced to use this to improve performace
174+
/// </summary>
175+
public void CleanUp()
176+
{
177+
runspacePool.Dispose();
178+
}
179+
169180
/// <summary>
170181
/// Returns all the rule arguments
171182
/// </summary>
@@ -699,6 +710,7 @@ private CommandInfo GetCommandInfoInternal(string cmdName, CommandTypes? command
699710
{
700711
using (var ps = System.Management.Automation.PowerShell.Create())
701712
{
713+
ps.RunspacePool = runspacePool;
702714
var cmdInfo = ps.AddCommand("Get-Command")
703715
.AddArgument(cmdName)
704716
.AddParameter("ErrorAction", "SilentlyContinue")

0 commit comments

Comments
 (0)