Skip to content

Commit

Permalink
Constrain use of ConcurrentDictionary to CommandHelpers
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
andyleejordan committed Jan 8, 2022
1 parent 09416f9 commit 627e915
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,10 @@ public static async Task<string> GetCommandSynopsisAsync(
/// Gets all aliases found in the runspace
/// </summary>
/// <param name="executionService"></param>
public static async Task<(ConcurrentDictionary<string, List<string>>, ConcurrentDictionary<string, string>)> GetAliasesAsync(IInternalPowerShellExecutionService executionService)
public static async Task<(Dictionary<string, List<string>>, Dictionary<string, string>)> GetAliasesAsync(IInternalPowerShellExecutionService executionService)
{
Validate.IsNotNull(nameof(executionService), executionService);

// TODO: Should we return the caches if they're not empty, or always update?
// if (!s_cmdletToAliasCache.IsEmpty || !s_aliasToCmdletCache.IsEmpty)
// {
// return (s_cmdletToAliasCache, s_aliasToCmdletCache);
// }

IEnumerable<CommandInfo> aliases = await executionService.ExecuteDelegateAsync<IEnumerable<CommandInfo>>(
nameof(GetAliasesAsync),
Execution.ExecutionOptions.Default,
Expand All @@ -209,7 +203,8 @@ public static async Task<string> GetCommandSynopsisAsync(
s_aliasToCmdletCache.TryAdd(aliasInfo.Name, aliasInfo.Definition);
}

return (s_cmdletToAliasCache, s_aliasToCmdletCache);
return (new Dictionary<string, List<string>>(s_cmdletToAliasCache),
new Dictionary<string, string>(s_aliasToCmdletCache));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public async Task<List<SymbolReference>> FindReferencesOfSymbol(
return null;
}

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

// We want to look for references first in referenced files, hence we use ordered dictionary
// TODO: File system case-sensitivity is based on filesystem not OS, but OS is a much cheaper heuristic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT License.

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
Expand Down Expand Up @@ -160,8 +159,8 @@ public static SymbolReference FindCommandAtPosition(Ast scriptAst, int lineNumbe
public static IEnumerable<SymbolReference> FindReferencesOfSymbol(
Ast scriptAst,
SymbolReference symbolReference,
ConcurrentDictionary<string, List<string>> cmdletToAliasDictionary = default,
ConcurrentDictionary<string, string> aliasToCmdletDictionary = default)
IDictionary<string, List<string>> cmdletToAliasDictionary = default,
IDictionary<string, string> aliasToCmdletDictionary = default)
{
// find the symbol evaluators for the node types we are handling
FindReferencesVisitor referencesVisitor = new(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT License.

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Management.Automation.Language;

Expand All @@ -14,8 +13,8 @@ namespace Microsoft.PowerShell.EditorServices.Services.Symbols
internal class FindReferencesVisitor : AstVisitor
{
private readonly SymbolReference _symbolRef;
private readonly ConcurrentDictionary<string, List<string>> _cmdletToAliasDictionary;
private readonly ConcurrentDictionary<string, string> _aliasToCmdletDictionary;
private readonly IDictionary<string, List<string>> _cmdletToAliasDictionary;
private readonly IDictionary<string, string> _aliasToCmdletDictionary;
private readonly string _symbolRefCommandName;
private readonly bool _needsAliases;

Expand All @@ -29,8 +28,8 @@ internal class FindReferencesVisitor : AstVisitor
/// <param name="aliasToCmdletDictionary">Dictionary maping aliases to cmdlets for finding alias references</param>
public FindReferencesVisitor(
SymbolReference symbolReference,
ConcurrentDictionary<string, List<string>> cmdletToAliasDictionary = default,
ConcurrentDictionary<string, string> aliasToCmdletDictionary = default)
IDictionary<string, List<string>> cmdletToAliasDictionary = default,
IDictionary<string, string> aliasToCmdletDictionary = default)
{
_symbolRef = symbolReference;
FoundReferences = new List<SymbolReference>();
Expand Down

0 comments on commit 627e915

Please sign in to comment.