Skip to content

Commit 8726c10

Browse files
author
Kapil Borle
committed
Revert adding runspace pool and concurrent dictionary
1 parent b660cc3 commit 8726c10

File tree

3 files changed

+11
-40
lines changed

3 files changed

+11
-40
lines changed

Engine/Commands/GetScriptAnalyzerRuleCommand.cs

-14
Original file line numberDiff line numberDiff line change
@@ -129,20 +129,6 @@ protected override void ProcessRecord()
129129
}
130130
}
131131

132-
protected override void EndProcessing()
133-
{
134-
ScriptAnalyzer.Instance.CleanUp();
135-
Helper.Instance.CleanUp();
136-
base.EndProcessing();
137-
}
138-
139-
protected override void StopProcessing()
140-
{
141-
ScriptAnalyzer.Instance.CleanUp();
142-
Helper.Instance.CleanUp();
143-
base.StopProcessing();
144-
}
145-
146132
#endregion
147133
}
148134
}

Engine/Commands/InvokeScriptAnalyzerCommand.cs

-2
Original file line numberDiff line numberDiff line change
@@ -337,14 +337,12 @@ protected override void ProcessRecord()
337337
protected override void EndProcessing()
338338
{
339339
ScriptAnalyzer.Instance.CleanUp();
340-
Helper.Instance.CleanUp();
341340
base.EndProcessing();
342341
}
343342

344343
protected override void StopProcessing()
345344
{
346345
ScriptAnalyzer.Instance.CleanUp();
347-
Helper.Instance.CleanUp();
348346
base.StopProcessing();
349347
}
350348

Engine/Helper.cs

+11-24
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
2222
using System.Management.Automation.Runspaces;
2323
using System.Collections;
24-
using System.Collections.Concurrent;
2524

2625
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer
2726
{
@@ -39,8 +38,7 @@ public class Helper
3938
private readonly static Version minSupportedPSVersion = new Version(3, 0);
4039
private Dictionary<string, Dictionary<string, object>> ruleArguments;
4140
private PSVersionTable psVersionTable;
42-
private ConcurrentDictionary<string, CommandInfo> commandInfoCache;
43-
private RunspacePool runspacePool;
41+
private Dictionary<string, CommandInfo> commandInfoCache;
4442

4543
#endregion
4644

@@ -149,12 +147,7 @@ public void Initialize()
149147
KeywordBlockDictionary = new Dictionary<String, List<Tuple<int, int>>>(StringComparer.OrdinalIgnoreCase);
150148
VariableAnalysisDictionary = new Dictionary<Ast, VariableAnalysis>();
151149
ruleArguments = new Dictionary<string, Dictionary<string, object>>(StringComparer.OrdinalIgnoreCase);
152-
commandInfoCache = new ConcurrentDictionary<string, CommandInfo>(StringComparer.OrdinalIgnoreCase);
153-
runspacePool = RunspaceFactory.CreateRunspacePool(InitialSessionState.CreateDefault2());
154-
155-
// After some experimentation, I found out that setting max runspaces more than 3 has marginal returns.
156-
runspacePool.SetMaxRunspaces(3);
157-
runspacePool.Open();
150+
commandInfoCache = new Dictionary<string, CommandInfo>(StringComparer.OrdinalIgnoreCase);
158151

159152
IEnumerable<CommandInfo> aliases = this.invokeCommand.GetCommands("*", CommandTypes.Alias, true);
160153

@@ -173,14 +166,6 @@ public void Initialize()
173166
}
174167
}
175168

176-
/// <summary>
177-
/// We are forced to use this to improve performace
178-
/// </summary>
179-
public void CleanUp()
180-
{
181-
runspacePool.Dispose();
182-
}
183-
184169
/// <summary>
185170
/// Returns all the rule arguments
186171
/// </summary>
@@ -717,7 +702,6 @@ private CommandInfo GetCommandInfoInternal(string cmdName, CommandTypes? command
717702
{
718703
using (var ps = System.Management.Automation.PowerShell.Create())
719704
{
720-
ps.RunspacePool = runspacePool;
721705
var cmdInfo = ps.AddCommand("Get-Command")
722706
.AddArgument(cmdName)
723707
.AddParameter("ErrorAction", "SilentlyContinue")
@@ -747,14 +731,17 @@ public CommandInfo GetCommandInfo(string name, CommandTypes? commandType = null)
747731
cmdletName = name;
748732
}
749733

750-
if (commandInfoCache.ContainsKey(cmdletName))
734+
lock (getCommandLock)
751735
{
752-
return commandInfoCache[cmdletName];
753-
}
736+
if (commandInfoCache.ContainsKey(cmdletName))
737+
{
738+
return commandInfoCache[cmdletName];
739+
}
754740

755-
var commandInfo = GetCommandInfoInternal(cmdletName, commandType);
756-
commandInfoCache.AddOrUpdate(cmdletName, (key) => commandInfo, (key, value) => commandInfo);
757-
return commandInfo;
741+
var commandInfo = GetCommandInfoInternal(cmdletName, commandType);
742+
commandInfoCache.Add(cmdletName, commandInfo);
743+
return commandInfo;
744+
}
758745
}
759746

760747
/// <summary>

0 commit comments

Comments
 (0)