21
21
using Microsoft . Windows . PowerShell . ScriptAnalyzer . Generic ;
22
22
using System . Management . Automation . Runspaces ;
23
23
using System . Collections ;
24
- using System . Collections . Concurrent ;
25
24
26
25
namespace Microsoft . Windows . PowerShell . ScriptAnalyzer
27
26
{
@@ -39,8 +38,7 @@ public class Helper
39
38
private readonly static Version minSupportedPSVersion = new Version ( 3 , 0 ) ;
40
39
private Dictionary < string , Dictionary < string , object > > ruleArguments ;
41
40
private PSVersionTable psVersionTable ;
42
- private ConcurrentDictionary < string , CommandInfo > commandInfoCache ;
43
- private RunspacePool runspacePool ;
41
+ private Dictionary < string , CommandInfo > commandInfoCache ;
44
42
45
43
#endregion
46
44
@@ -149,12 +147,7 @@ public void Initialize()
149
147
KeywordBlockDictionary = new Dictionary < String , List < Tuple < int , int > > > ( StringComparer . OrdinalIgnoreCase ) ;
150
148
VariableAnalysisDictionary = new Dictionary < Ast , VariableAnalysis > ( ) ;
151
149
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 ) ;
158
151
159
152
IEnumerable < CommandInfo > aliases = this . invokeCommand . GetCommands ( "*" , CommandTypes . Alias , true ) ;
160
153
@@ -173,14 +166,6 @@ public void Initialize()
173
166
}
174
167
}
175
168
176
- /// <summary>
177
- /// We are forced to use this to improve performace
178
- /// </summary>
179
- public void CleanUp ( )
180
- {
181
- runspacePool . Dispose ( ) ;
182
- }
183
-
184
169
/// <summary>
185
170
/// Returns all the rule arguments
186
171
/// </summary>
@@ -717,7 +702,6 @@ private CommandInfo GetCommandInfoInternal(string cmdName, CommandTypes? command
717
702
{
718
703
using ( var ps = System . Management . Automation . PowerShell . Create ( ) )
719
704
{
720
- ps . RunspacePool = runspacePool ;
721
705
var cmdInfo = ps . AddCommand ( "Get-Command" )
722
706
. AddArgument ( cmdName )
723
707
. AddParameter ( "ErrorAction" , "SilentlyContinue" )
@@ -747,14 +731,17 @@ public CommandInfo GetCommandInfo(string name, CommandTypes? commandType = null)
747
731
cmdletName = name ;
748
732
}
749
733
750
- if ( commandInfoCache . ContainsKey ( cmdletName ) )
734
+ lock ( getCommandLock )
751
735
{
752
- return commandInfoCache [ cmdletName ] ;
753
- }
736
+ if ( commandInfoCache . ContainsKey ( cmdletName ) )
737
+ {
738
+ return commandInfoCache [ cmdletName ] ;
739
+ }
754
740
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
+ }
758
745
}
759
746
760
747
/// <summary>
0 commit comments