Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion Engine/CommandInfoCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ public CommandInfo GetCommandInfo(string commandName, CommandTypes? commandTypes
{
return GetCommandInfoInternal(commandName, commandTypes);
}

if (_commandInfoCache.TryGetValue(key, out Lazy<CommandInfo> cachedCommand))
{
return cachedCommand.Value;
}

return AddCommandInfo(key, commandName, commandTypes);
}

private CommandInfo AddCommandInfo(CommandLookupKey key, string commandName, CommandTypes? commandTypes)
{
// Keep the closure and Lazy allocation off the cache-hit path. Concurrent misses
// still share the winning Lazy so command discovery runs once for each key.
// Atomically either use PowerShell to query a command info object, or fetch it from the cache
return _commandInfoCache.GetOrAdd(key, new Lazy<CommandInfo>(() => GetCommandInfoInternal(commandName, commandTypes))).Value;
}
Expand Down Expand Up @@ -146,7 +159,7 @@ public override int GetHashCode()
unchecked
{
int hash = 17;
hash = hash * 31 + Name.ToUpperInvariant().GetHashCode();
hash = hash * 31 + StringComparer.OrdinalIgnoreCase.GetHashCode(Name);
hash = hash * 31 + CommandTypes.GetHashCode();
return hash;
}
Expand Down
42 changes: 42 additions & 0 deletions Tests/Engine/CommandInfoCache.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

Describe 'CommandInfoCache' {
BeforeEach {
$cacheType = [Microsoft.Windows.PowerShell.ScriptAnalyzer.Helper].Assembly.GetType(
'Microsoft.Windows.PowerShell.ScriptAnalyzer.CommandInfoCache', $true)
$cache = [Activator]::CreateInstance($cacheType)
}

AfterEach {
$cache.Dispose()
}

It 'Returns the cached command for differently cased names and equivalent command types' {
$command = $cache.GetCommandInfo('Get-Item', $null, $false)
$command | Should -Not -BeNullOrEmpty
$cached = $cache.GetCommandInfo('gET-iTEM', [System.Management.Automation.CommandTypes]::All, $false)
[object]::ReferenceEquals($command, $cached) | Should -BeTrue
}

It 'Keeps command-type filters separate from an existing cache entry' {
$cache.GetCommandInfo('Get-Item', [System.Management.Automation.CommandTypes]::Function, $false) |
Should -BeNullOrEmpty
$command = $cache.GetCommandInfo('Get-Item', [System.Management.Automation.CommandTypes]::Cmdlet, $false)
$command.CommandType | Should -Be ([System.Management.Automation.CommandTypes]::Cmdlet)
$cache.GetCommandInfo('GET-ITEM', [System.Management.Automation.CommandTypes]::Function, $false) |
Should -BeNullOrEmpty
}

It 'Uses ordinal case-insensitive lookup independently of the current culture' {
$originalCulture = [System.Threading.Thread]::CurrentThread.CurrentCulture
try {
$command = $cache.GetCommandInfo('Get-Item', $null, $false)
[System.Threading.Thread]::CurrentThread.CurrentCulture = [System.Globalization.CultureInfo]::GetCultureInfo('tr-TR')
$cached = $cache.GetCommandInfo('get-item', $null, $false)
[object]::ReferenceEquals($command, $cached) | Should -BeTrue
} finally {
[System.Threading.Thread]::CurrentThread.CurrentCulture = $originalCulture
}
}
}