Skip to content

Commit 43f865c

Browse files
committed
Be sure to use the command cache to improve performance
Previous implementation of command cache usage would take the lock during the pipeline execution of Get-Command which could be very long. This breaks up the lock to not include the pipeline invocation.
1 parent 7a46212 commit 43f865c

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

Engine/Helper.cs

+26-1
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,15 @@ private CommandInfo GetCommandInfoInternal(string cmdName, CommandTypes commandT
701701
return null;
702702
}
703703

704+
// Don't hold the lock during the pipeline call
705+
lock (getCommandLock)
706+
{
707+
CommandInfo cmdletInfo;
708+
if (commandInfoCache.TryGetValue(cmdName, out cmdletInfo)) {
709+
return cmdletInfo;
710+
}
711+
}
712+
704713
using (var ps = System.Management.Automation.PowerShell.Create())
705714
{
706715
var psCommand = ps.AddCommand("Get-Command")
@@ -711,6 +720,14 @@ private CommandInfo GetCommandInfoInternal(string cmdName, CommandTypes commandT
711720
var commandInfo = psCommand.Invoke<CommandInfo>()
712721
.FirstOrDefault();
713722

723+
lock (getCommandLock)
724+
{
725+
CommandInfo cmdletInfo;
726+
if (! commandInfoCache.TryGetValue(cmdName, out cmdletInfo) )
727+
{
728+
commandInfoCache.Add(cmdName, commandInfo);
729+
}
730+
}
714731
return commandInfo;
715732
}
716733
}
@@ -723,6 +740,8 @@ private CommandInfo GetCommandInfoInternal(string cmdName, CommandTypes commandT
723740
/// <returns>The first CommandInfo found based on the name and type or null if nothing was found</returns>
724741
public CommandInfo GetCommandInfo(string name, CommandTypes commandType)
725742
{
743+
// We don't check for alias in this case as the user has been explicit about
744+
// the CommandTypes desired
726745
return GetCommandInfoInternal(name, commandType);
727746
}
728747

@@ -733,7 +752,13 @@ public CommandInfo GetCommandInfo(string name, CommandTypes commandType)
733752
/// <returns>The first CommandInfo found based on the name of any command type or null if nothing was found</returns>
734753
public CommandInfo GetCommandInfo(string name)
735754
{
736-
return GetCommandInfo(name, CommandTypes.All);
755+
// check to see if it's an alias and use the resolved cmdlet name if it is
756+
string cmdletName = Helper.Instance.GetCmdletNameFromAlias(name);
757+
if ( string.IsNullOrWhiteSpace(cmdletName))
758+
{
759+
cmdletName = name;
760+
}
761+
return GetCommandInfo(cmdletName, CommandTypes.All);
737762
}
738763

739764
/// <summary>

0 commit comments

Comments
 (0)