Skip to content

Commit 627e915

Browse files
committed
Constrain use of ConcurrentDictionary to CommandHelpers
Which is where it might be accessed concurrently as a cache, but we can use `IDictionary` (and return a copy of the caches) to the rest of the users.
1 parent 09416f9 commit 627e915

File tree

4 files changed

+10
-17
lines changed

4 files changed

+10
-17
lines changed

Diff for: src/PowerShellEditorServices/Services/PowerShell/Utility/CommandHelpers.cs

+3-8
Original file line numberDiff line numberDiff line change
@@ -177,16 +177,10 @@ public static async Task<string> GetCommandSynopsisAsync(
177177
/// Gets all aliases found in the runspace
178178
/// </summary>
179179
/// <param name="executionService"></param>
180-
public static async Task<(ConcurrentDictionary<string, List<string>>, ConcurrentDictionary<string, string>)> GetAliasesAsync(IInternalPowerShellExecutionService executionService)
180+
public static async Task<(Dictionary<string, List<string>>, Dictionary<string, string>)> GetAliasesAsync(IInternalPowerShellExecutionService executionService)
181181
{
182182
Validate.IsNotNull(nameof(executionService), executionService);
183183

184-
// TODO: Should we return the caches if they're not empty, or always update?
185-
// if (!s_cmdletToAliasCache.IsEmpty || !s_aliasToCmdletCache.IsEmpty)
186-
// {
187-
// return (s_cmdletToAliasCache, s_aliasToCmdletCache);
188-
// }
189-
190184
IEnumerable<CommandInfo> aliases = await executionService.ExecuteDelegateAsync<IEnumerable<CommandInfo>>(
191185
nameof(GetAliasesAsync),
192186
Execution.ExecutionOptions.Default,
@@ -209,7 +203,8 @@ public static async Task<string> GetCommandSynopsisAsync(
209203
s_aliasToCmdletCache.TryAdd(aliasInfo.Name, aliasInfo.Definition);
210204
}
211205

212-
return (s_cmdletToAliasCache, s_aliasToCmdletCache);
206+
return (new Dictionary<string, List<string>>(s_cmdletToAliasCache),
207+
new Dictionary<string, string>(s_aliasToCmdletCache));
213208
}
214209
}
215210
}

Diff for: src/PowerShellEditorServices/Services/Symbols/SymbolsService.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public async Task<List<SymbolReference>> FindReferencesOfSymbol(
189189
return null;
190190
}
191191

192-
(ConcurrentDictionary<string, List<string>> cmdletToAliases, ConcurrentDictionary<string, string> aliasToCmdlets) = await CommandHelpers.GetAliasesAsync(_executionService).ConfigureAwait(false);
192+
(Dictionary<string, List<string>> cmdletToAliases, Dictionary<string, string> aliasToCmdlets) = await CommandHelpers.GetAliasesAsync(_executionService).ConfigureAwait(false);
193193

194194
// We want to look for references first in referenced files, hence we use ordered dictionary
195195
// TODO: File system case-sensitivity is based on filesystem not OS, but OS is a much cheaper heuristic

Diff for: src/PowerShellEditorServices/Services/Symbols/Vistors/AstOperations.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the MIT License.
33

44
using System;
5-
using System.Collections.Concurrent;
65
using System.Collections.Generic;
76
using System.Diagnostics;
87
using System.Linq;
@@ -160,8 +159,8 @@ public static SymbolReference FindCommandAtPosition(Ast scriptAst, int lineNumbe
160159
public static IEnumerable<SymbolReference> FindReferencesOfSymbol(
161160
Ast scriptAst,
162161
SymbolReference symbolReference,
163-
ConcurrentDictionary<string, List<string>> cmdletToAliasDictionary = default,
164-
ConcurrentDictionary<string, string> aliasToCmdletDictionary = default)
162+
IDictionary<string, List<string>> cmdletToAliasDictionary = default,
163+
IDictionary<string, string> aliasToCmdletDictionary = default)
165164
{
166165
// find the symbol evaluators for the node types we are handling
167166
FindReferencesVisitor referencesVisitor = new(

Diff for: src/PowerShellEditorServices/Services/Symbols/Vistors/FindReferencesVisitor.cs

+4-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the MIT License.
33

44
using System;
5-
using System.Collections.Concurrent;
65
using System.Collections.Generic;
76
using System.Management.Automation.Language;
87

@@ -14,8 +13,8 @@ namespace Microsoft.PowerShell.EditorServices.Services.Symbols
1413
internal class FindReferencesVisitor : AstVisitor
1514
{
1615
private readonly SymbolReference _symbolRef;
17-
private readonly ConcurrentDictionary<string, List<string>> _cmdletToAliasDictionary;
18-
private readonly ConcurrentDictionary<string, string> _aliasToCmdletDictionary;
16+
private readonly IDictionary<string, List<string>> _cmdletToAliasDictionary;
17+
private readonly IDictionary<string, string> _aliasToCmdletDictionary;
1918
private readonly string _symbolRefCommandName;
2019
private readonly bool _needsAliases;
2120

@@ -29,8 +28,8 @@ internal class FindReferencesVisitor : AstVisitor
2928
/// <param name="aliasToCmdletDictionary">Dictionary maping aliases to cmdlets for finding alias references</param>
3029
public FindReferencesVisitor(
3130
SymbolReference symbolReference,
32-
ConcurrentDictionary<string, List<string>> cmdletToAliasDictionary = default,
33-
ConcurrentDictionary<string, string> aliasToCmdletDictionary = default)
31+
IDictionary<string, List<string>> cmdletToAliasDictionary = default,
32+
IDictionary<string, string> aliasToCmdletDictionary = default)
3433
{
3534
_symbolRef = symbolReference;
3635
FoundReferences = new List<SymbolReference>();

0 commit comments

Comments
 (0)