Skip to content

Commit 585cca9

Browse files
author
Kapil Borle
committed
Use concurrent dictionary for commandinfo cache
1 parent 2fae803 commit 585cca9

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

Engine/Helper.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
2222
using System.Management.Automation.Runspaces;
2323
using System.Collections;
24+
using System.Collections.Concurrent;
2425

2526
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer
2627
{
@@ -38,7 +39,7 @@ public class Helper
3839
private readonly static Version minSupportedPSVersion = new Version(3, 0);
3940
private Dictionary<string, Dictionary<string, object>> ruleArguments;
4041
private PSVersionTable psVersionTable;
41-
private Dictionary<string, CommandInfo> commandInfoCache;
42+
private ConcurrentDictionary<string, CommandInfo> commandInfoCache;
4243
private RunspacePool runspacePool;
4344

4445
#endregion
@@ -148,7 +149,7 @@ public void Initialize()
148149
KeywordBlockDictionary = new Dictionary<String, List<Tuple<int, int>>>(StringComparer.OrdinalIgnoreCase);
149150
VariableAnalysisDictionary = new Dictionary<Ast, VariableAnalysis>();
150151
ruleArguments = new Dictionary<string, Dictionary<string, object>>(StringComparer.OrdinalIgnoreCase);
151-
commandInfoCache = new Dictionary<string, CommandInfo>(StringComparer.OrdinalIgnoreCase);
152+
commandInfoCache = new ConcurrentDictionary<string, CommandInfo>(StringComparer.OrdinalIgnoreCase);
152153
runspacePool = RunspaceFactory.CreateRunspacePool(InitialSessionState.CreateDefault2());
153154
runspacePool.Open();
154155

@@ -740,17 +741,14 @@ public CommandInfo GetCommandInfo(string name, CommandTypes? commandType = null)
740741
cmdletName = name;
741742
}
742743

743-
lock (getCommandLock)
744+
if (commandInfoCache.ContainsKey(cmdletName))
744745
{
745-
if (commandInfoCache.ContainsKey(cmdletName))
746-
{
747-
return commandInfoCache[cmdletName];
748-
}
749-
750-
var commandInfo = GetCommandInfoInternal(cmdletName, commandType);
751-
commandInfoCache.Add(cmdletName, commandInfo);
752-
return commandInfo;
746+
return commandInfoCache[cmdletName];
753747
}
748+
749+
var commandInfo = GetCommandInfoInternal(cmdletName, commandType);
750+
commandInfoCache.AddOrUpdate(cmdletName, (key) => commandInfo, (key, value) => commandInfo);
751+
return commandInfo;
754752
}
755753

756754
/// <summary>

0 commit comments

Comments
 (0)