Skip to content

Commit ae3c26c

Browse files
Enable command lookup caching by name and filter
Add a struct to use as a private struct to use as a key for the command lookup cache. Also re-enable caching in Helper.GetCommandInfo.
1 parent 2d439ff commit ae3c26c

File tree

1 file changed

+41
-8
lines changed

1 file changed

+41
-8
lines changed

Engine/Helper.cs

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class Helper
2828
private readonly static Version minSupportedPSVersion = new Version(3, 0);
2929
private Dictionary<string, Dictionary<string, object>> ruleArguments;
3030
private PSVersionTable psVersionTable;
31-
private Dictionary<string, CommandInfo> commandInfoCache;
31+
private Dictionary<CommandLookupKey, CommandInfo> commandInfoCache;
3232

3333
#endregion
3434

@@ -142,7 +142,7 @@ public void Initialize()
142142
ruleArguments = new Dictionary<string, Dictionary<string, object>>(StringComparer.OrdinalIgnoreCase);
143143
if (commandInfoCache == null)
144144
{
145-
commandInfoCache = new Dictionary<string, CommandInfo>(StringComparer.OrdinalIgnoreCase);
145+
commandInfoCache = new Dictionary<CommandLookupKey, CommandInfo>();
146146
}
147147

148148
IEnumerable<CommandInfo> aliases = this.invokeCommand.GetCommands("*", CommandTypes.Alias, true);
@@ -700,15 +700,16 @@ public CommandInfo GetCommandInfoLegacy(string name, CommandTypes? commandType =
700700
cmdletName = name;
701701
}
702702

703+
var key = new CommandLookupKey(name, commandType);
703704
lock (getCommandLock)
704705
{
705-
if (commandInfoCache.ContainsKey(cmdletName))
706+
if (commandInfoCache.ContainsKey(key))
706707
{
707-
return commandInfoCache[cmdletName];
708+
return commandInfoCache[key];
708709
}
709710

710711
var commandInfo = GetCommandInfoInternal(cmdletName, commandType);
711-
commandInfoCache.Add(cmdletName, commandInfo);
712+
commandInfoCache.Add(key, commandInfo);
712713
return commandInfo;
713714
}
714715
}
@@ -726,15 +727,16 @@ public CommandInfo GetCommandInfo(string name, CommandTypes? commandType = null)
726727
return null;
727728
}
728729

730+
var key = new CommandLookupKey(name, commandType);
729731
lock (getCommandLock)
730732
{
731-
if (commandInfoCache.ContainsKey(name))
733+
if (commandInfoCache.ContainsKey(key))
732734
{
733-
return commandInfoCache[name];
735+
return commandInfoCache[key];
734736
}
735737

736738
var commandInfo = GetCommandInfoInternal(name, commandType);
737-
739+
commandInfoCache.Add(key, commandInfo);
738740
return commandInfo;
739741
}
740742
}
@@ -1910,6 +1912,37 @@ public Version GetPSVersion()
19101912
}
19111913

19121914
#endregion Methods
1915+
1916+
private struct CommandLookupKey : IEquatable<CommandLookupKey>
1917+
{
1918+
private readonly string Name;
1919+
1920+
private readonly CommandTypes CommandTypes;
1921+
1922+
internal CommandLookupKey(string name, CommandTypes? commandTypes)
1923+
{
1924+
Name = name;
1925+
CommandTypes = commandTypes ?? CommandTypes.All;
1926+
}
1927+
1928+
public bool Equals(CommandLookupKey other)
1929+
{
1930+
return CommandTypes == other.CommandTypes
1931+
&& Name.Equals(other.Name, StringComparison.OrdinalIgnoreCase);
1932+
}
1933+
1934+
public override int GetHashCode()
1935+
{
1936+
// Algorithm from https://stackoverflow.com/questions/1646807/quick-and-simple-hash-code-combinations
1937+
unchecked
1938+
{
1939+
int hash = 17;
1940+
hash = hash * 31 + Name.ToUpperInvariant().GetHashCode();
1941+
hash = hash * 31 + CommandTypes.GetHashCode();
1942+
return hash;
1943+
}
1944+
}
1945+
}
19131946
}
19141947

19151948

0 commit comments

Comments
 (0)