@@ -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
@@ -676,7 +678,6 @@ private CommandInfo GetCommandInfoInternal(string cmdName, CommandTypes? command
676
678
}
677
679
678
680
/// <summary>
679
-
680
681
/// Legacy method, new callers should use <see cref="GetCommandInfo"/> instead.
681
682
/// Given a command's name, checks whether it exists. It does not use the passed in CommandTypes parameter, which is a bug.
682
683
/// But existing method callers are already depending on this behaviour and therefore this could not be simply fixed.
@@ -688,30 +689,7 @@ private CommandInfo GetCommandInfoInternal(string cmdName, CommandTypes? command
688
689
[ Obsolete ]
689
690
public CommandInfo GetCommandInfoLegacy ( string name , CommandTypes ? commandType = null )
690
691
{
691
- if ( string . IsNullOrWhiteSpace ( name ) )
692
- {
693
- return null ;
694
- }
695
-
696
- // check if it is an alias
697
- string cmdletName = Helper . Instance . GetCmdletNameFromAlias ( name ) ;
698
- if ( string . IsNullOrWhiteSpace ( cmdletName ) )
699
- {
700
- cmdletName = name ;
701
- }
702
-
703
- var key = new CommandLookupKey ( name , commandType ) ;
704
- lock ( getCommandLock )
705
- {
706
- if ( commandInfoCache . ContainsKey ( key ) )
707
- {
708
- return commandInfoCache [ key ] ;
709
- }
710
-
711
- var commandInfo = GetCommandInfoInternal ( cmdletName , commandType ) ;
712
- commandInfoCache . Add ( key , commandInfo ) ;
713
- return commandInfo ;
714
- }
692
+ return CommandInfoCache . GetCommandInfoLegacy ( commandOrAliasName : name , commandTypes : commandType ) ;
715
693
}
716
694
717
695
/// <summary>
@@ -722,23 +700,7 @@ public CommandInfo GetCommandInfoLegacy(string name, CommandTypes? commandType =
722
700
/// <returns></returns>
723
701
public CommandInfo GetCommandInfo ( string name , CommandTypes ? commandType = null )
724
702
{
725
- if ( string . IsNullOrWhiteSpace ( name ) )
726
- {
727
- return null ;
728
- }
729
-
730
- var key = new CommandLookupKey ( name , commandType ) ;
731
- lock ( getCommandLock )
732
- {
733
- if ( commandInfoCache . ContainsKey ( key ) )
734
- {
735
- return commandInfoCache [ key ] ;
736
- }
737
-
738
- var commandInfo = GetCommandInfoInternal ( name , commandType ) ;
739
- commandInfoCache . Add ( key , commandInfo ) ;
740
- return commandInfo ;
741
- }
703
+ return CommandInfoCache . GetCommandInfo ( name , commandTypes : commandType ) ;
742
704
}
743
705
744
706
/// <summary>
0 commit comments