@@ -24,11 +24,11 @@ public class Helper
24
24
25
25
private CommandInvocationIntrinsics invokeCommand ;
26
26
private IOutputWriter outputWriter ;
27
- private Object getCommandLock = new object ( ) ;
28
27
private readonly static Version minSupportedPSVersion = new Version ( 3 , 0 ) ;
29
28
private Dictionary < string , Dictionary < string , object > > ruleArguments ;
30
29
private PSVersionTable psVersionTable ;
31
- private Dictionary < CommandLookupKey , CommandInfo > commandInfoCache ;
30
+
31
+ private readonly Lazy < CommandInfoCache > _commandInfoCacheLazy ;
32
32
33
33
#endregion
34
34
@@ -100,14 +100,20 @@ internal set
100
100
private string [ ] functionScopes = new string [ ] { "global:" , "local:" , "script:" , "private:" } ;
101
101
102
102
private string [ ] variableScopes = new string [ ] { "global:" , "local:" , "script:" , "private:" , "variable:" , ":" } ;
103
+
104
+ /// <summary>
105
+ /// Store of command info objects for commands. Memoizes results.
106
+ /// </summary>
107
+ private CommandInfoCache CommandInfoCache => _commandInfoCacheLazy . Value ;
108
+
103
109
#endregion
104
110
105
111
/// <summary>
106
112
/// Initializes the Helper class.
107
113
/// </summary>
108
114
private Helper ( )
109
115
{
110
-
116
+ _commandInfoCacheLazy = new Lazy < CommandInfoCache > ( ( ) => new CommandInfoCache ( pssaHelperInstance : this ) ) ;
111
117
}
112
118
113
119
/// <summary>
@@ -123,7 +129,7 @@ private Helper()
123
129
/// </param>
124
130
public Helper (
125
131
CommandInvocationIntrinsics invokeCommand ,
126
- IOutputWriter outputWriter )
132
+ IOutputWriter outputWriter ) : this ( )
127
133
{
128
134
this . invokeCommand = invokeCommand ;
129
135
this . outputWriter = outputWriter ;
@@ -140,10 +146,6 @@ public void Initialize()
140
146
KeywordBlockDictionary = new Dictionary < String , List < Tuple < int , int > > > ( StringComparer . OrdinalIgnoreCase ) ;
141
147
VariableAnalysisDictionary = new Dictionary < Ast , VariableAnalysis > ( ) ;
142
148
ruleArguments = new Dictionary < string , Dictionary < string , object > > ( StringComparer . OrdinalIgnoreCase ) ;
143
- if ( commandInfoCache == null )
144
- {
145
- commandInfoCache = new Dictionary < CommandLookupKey , CommandInfo > ( ) ;
146
- }
147
149
148
150
IEnumerable < CommandInfo > aliases = this . invokeCommand . GetCommands ( "*" , CommandTypes . Alias , true ) ;
149
151
@@ -677,7 +679,6 @@ private CommandInfo GetCommandInfoInternal(string cmdName, CommandTypes? command
677
679
}
678
680
679
681
/// <summary>
680
-
681
682
/// Legacy method, new callers should use <see cref="GetCommandInfo"/> instead.
682
683
/// Given a command's name, checks whether it exists. It does not use the passed in CommandTypes parameter, which is a bug.
683
684
/// But existing method callers are already depending on this behaviour and therefore this could not be simply fixed.
@@ -689,30 +690,7 @@ private CommandInfo GetCommandInfoInternal(string cmdName, CommandTypes? command
689
690
[ Obsolete ]
690
691
public CommandInfo GetCommandInfoLegacy ( string name , CommandTypes ? commandType = null )
691
692
{
692
- if ( string . IsNullOrWhiteSpace ( name ) )
693
- {
694
- return null ;
695
- }
696
-
697
- // check if it is an alias
698
- string cmdletName = Helper . Instance . GetCmdletNameFromAlias ( name ) ;
699
- if ( string . IsNullOrWhiteSpace ( cmdletName ) )
700
- {
701
- cmdletName = name ;
702
- }
703
-
704
- var key = new CommandLookupKey ( name , commandType ) ;
705
- lock ( getCommandLock )
706
- {
707
- if ( commandInfoCache . ContainsKey ( key ) )
708
- {
709
- return commandInfoCache [ key ] ;
710
- }
711
-
712
- var commandInfo = GetCommandInfoInternal ( cmdletName , commandType ) ;
713
- commandInfoCache . Add ( key , commandInfo ) ;
714
- return commandInfo ;
715
- }
693
+ return CommandInfoCache . GetCommandInfoLegacy ( commandOrAliasName : name , commandTypes : commandType ) ;
716
694
}
717
695
718
696
/// <summary>
@@ -723,23 +701,7 @@ public CommandInfo GetCommandInfoLegacy(string name, CommandTypes? commandType =
723
701
/// <returns></returns>
724
702
public CommandInfo GetCommandInfo ( string name , CommandTypes ? commandType = null )
725
703
{
726
- if ( string . IsNullOrWhiteSpace ( name ) )
727
- {
728
- return null ;
729
- }
730
-
731
- var key = new CommandLookupKey ( name , commandType ) ;
732
- lock ( getCommandLock )
733
- {
734
- if ( commandInfoCache . ContainsKey ( key ) )
735
- {
736
- return commandInfoCache [ key ] ;
737
- }
738
-
739
- var commandInfo = GetCommandInfoInternal ( name , commandType ) ;
740
- commandInfoCache . Add ( key , commandInfo ) ;
741
- return commandInfo ;
742
- }
704
+ return CommandInfoCache . GetCommandInfo ( name , commandTypes : commandType ) ;
743
705
}
744
706
745
707
/// <summary>
0 commit comments