Skip to content

Commit b5a19fa

Browse files
authored
Merge pull request #1074 from SeeminglyScience/reenable-command-caching
Restore caching in Helper.GetCommandInfo
2 parents 2d439ff + 1217068 commit b5a19fa

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed

Engine/CommandLookupKey.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Management.Automation;
6+
7+
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer
8+
{
9+
internal struct CommandLookupKey : IEquatable<CommandLookupKey>
10+
{
11+
private readonly string Name;
12+
13+
private readonly CommandTypes CommandTypes;
14+
15+
internal CommandLookupKey(string name, CommandTypes? commandTypes)
16+
{
17+
Name = name;
18+
CommandTypes = commandTypes ?? CommandTypes.All;
19+
}
20+
21+
public bool Equals(CommandLookupKey other)
22+
{
23+
return CommandTypes == other.CommandTypes
24+
&& Name.Equals(other.Name, StringComparison.OrdinalIgnoreCase);
25+
}
26+
27+
public override int GetHashCode()
28+
{
29+
// Algorithm from https://stackoverflow.com/questions/1646807/quick-and-simple-hash-code-combinations
30+
unchecked
31+
{
32+
int hash = 17;
33+
hash = hash * 31 + Name.ToUpperInvariant().GetHashCode();
34+
hash = hash * 31 + CommandTypes.GetHashCode();
35+
return hash;
36+
}
37+
}
38+
}
39+
}

Engine/Helper.cs

Lines changed: 10 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
}

0 commit comments

Comments
 (0)