Skip to content

Commit b00ac0b

Browse files
committed
Speed up cold runs of PSSA by 20% by using a RunspacePool when querying PowerShell for CommandInfo.
There are only 4 rules that could call the CommandInfoCache, therefore sizing the pool to 5. The pool creation is synchronous and adds 15ms of initialization overhead, which we can neglect I think.
1 parent bfec823 commit b00ac0b

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

Engine/CommandInfoCache.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Concurrent;
66
using System.Management.Automation;
77
using System.Linq;
8+
using System.Management.Automation.Runspaces;
89

910
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer
1011
{
@@ -14,7 +15,7 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer
1415
internal class CommandInfoCache
1516
{
1617
private readonly ConcurrentDictionary<CommandLookupKey, Lazy<CommandInfo>> _commandInfoCache;
17-
18+
private readonly RunspacePool _runspacePool;
1819
private readonly Helper _helperInstance;
1920

2021
/// <summary>
@@ -24,6 +25,9 @@ public CommandInfoCache(Helper pssaHelperInstance)
2425
{
2526
_commandInfoCache = new ConcurrentDictionary<CommandLookupKey, Lazy<CommandInfo>>();
2627
_helperInstance = pssaHelperInstance;
28+
// There are only 4 rules that use the CommandInfo cache and each rule does not request more than one concurrent command info request
29+
_runspacePool = RunspaceFactory.CreateRunspacePool(1, 5);
30+
_runspacePool.Open();
2731
}
2832

2933
/// <summary>
@@ -67,10 +71,12 @@ public CommandInfo GetCommandInfoLegacy(string commandOrAliasName, CommandTypes?
6771
/// Get a CommandInfo object of the given command name
6872
/// </summary>
6973
/// <returns>Returns null if command does not exists</returns>
70-
private static CommandInfo GetCommandInfoInternal(string cmdName, CommandTypes? commandType)
74+
private CommandInfo GetCommandInfoInternal(string cmdName, CommandTypes? commandType)
7175
{
7276
using (var ps = System.Management.Automation.PowerShell.Create())
7377
{
78+
ps.RunspacePool = _runspacePool;
79+
7480
ps.AddCommand("Get-Command")
7581
.AddParameter("Name", cmdName)
7682
.AddParameter("ErrorAction", "SilentlyContinue");

0 commit comments

Comments
 (0)