2121using Microsoft . Windows . PowerShell . ScriptAnalyzer . Generic ;
2222using System . Management . Automation . Runspaces ;
2323using System . Collections ;
24- using System . Collections . Concurrent ;
2524
2625namespace Microsoft . Windows . PowerShell . ScriptAnalyzer
2726{
@@ -39,8 +38,7 @@ public class Helper
3938 private readonly static Version minSupportedPSVersion = new Version ( 3 , 0 ) ;
4039 private Dictionary < string , Dictionary < string , object > > ruleArguments ;
4140 private PSVersionTable psVersionTable ;
42- private ConcurrentDictionary < string , CommandInfo > commandInfoCache ;
43- private RunspacePool runspacePool ;
41+ private Dictionary < string , CommandInfo > commandInfoCache ;
4442
4543 #endregion
4644
@@ -149,12 +147,7 @@ public void Initialize()
149147 KeywordBlockDictionary = new Dictionary < String , List < Tuple < int , int > > > ( StringComparer . OrdinalIgnoreCase ) ;
150148 VariableAnalysisDictionary = new Dictionary < Ast , VariableAnalysis > ( ) ;
151149 ruleArguments = new Dictionary < string , Dictionary < string , object > > ( StringComparer . OrdinalIgnoreCase ) ;
152- commandInfoCache = new ConcurrentDictionary < string , CommandInfo > ( StringComparer . OrdinalIgnoreCase ) ;
153- runspacePool = RunspaceFactory . CreateRunspacePool ( InitialSessionState . CreateDefault2 ( ) ) ;
154-
155- // After some experimentation, I found out that setting max runspaces more than 3 has marginal returns.
156- runspacePool . SetMaxRunspaces ( 3 ) ;
157- runspacePool . Open ( ) ;
150+ commandInfoCache = new Dictionary < string , CommandInfo > ( StringComparer . OrdinalIgnoreCase ) ;
158151
159152 IEnumerable < CommandInfo > aliases = this . invokeCommand . GetCommands ( "*" , CommandTypes . Alias , true ) ;
160153
@@ -173,14 +166,6 @@ public void Initialize()
173166 }
174167 }
175168
176- /// <summary>
177- /// We are forced to use this to improve performace
178- /// </summary>
179- public void CleanUp ( )
180- {
181- runspacePool . Dispose ( ) ;
182- }
183-
184169 /// <summary>
185170 /// Returns all the rule arguments
186171 /// </summary>
@@ -717,7 +702,6 @@ private CommandInfo GetCommandInfoInternal(string cmdName, CommandTypes? command
717702 {
718703 using ( var ps = System . Management . Automation . PowerShell . Create ( ) )
719704 {
720- ps . RunspacePool = runspacePool ;
721705 var cmdInfo = ps . AddCommand ( "Get-Command" )
722706 . AddArgument ( cmdName )
723707 . AddParameter ( "ErrorAction" , "SilentlyContinue" )
@@ -747,14 +731,17 @@ public CommandInfo GetCommandInfo(string name, CommandTypes? commandType = null)
747731 cmdletName = name ;
748732 }
749733
750- if ( commandInfoCache . ContainsKey ( cmdletName ) )
734+ lock ( getCommandLock )
751735 {
752- return commandInfoCache [ cmdletName ] ;
753- }
736+ if ( commandInfoCache . ContainsKey ( cmdletName ) )
737+ {
738+ return commandInfoCache [ cmdletName ] ;
739+ }
754740
755- var commandInfo = GetCommandInfoInternal ( cmdletName , commandType ) ;
756- commandInfoCache . AddOrUpdate ( cmdletName , ( key ) => commandInfo , ( key , value ) => commandInfo ) ;
757- return commandInfo ;
741+ var commandInfo = GetCommandInfoInternal ( cmdletName , commandType ) ;
742+ commandInfoCache . Add ( cmdletName , commandInfo ) ;
743+ return commandInfo ;
744+ }
758745 }
759746
760747 /// <summary>
0 commit comments