Skip to content

Avoid unnecessary allocations during command lookup - #28229

Open
BigWorkk wants to merge 1 commit into
PowerShell:masterfrom
BigWorkk:bigworkk-powershell-perf-improvement
Open

BigWorkk wants to merge 1 commit into
PowerShell:masterfrom
BigWorkk:bigworkk-powershell-perf-improvement

Conversation

@BigWorkk

Copy link
Copy Markdown

PR Summary

Allocate the command-path removal index only when relative lookup paths actually require removal.

PR Context

CommandPathSearch.ResolveCurrentDirectoryInLookupPaths ran for every command-path search and eagerly created a SortedDictionary, even when the PATH entries were absolute and no paths needed removal. Make the allocation lazy so the common no-removal path avoids that object and its internal storage, without changing path resolution.

PR Checklist

  • PR has a meaningful title

    • Use the present tense and imperative mood when describing your changes
  • Summarized changes

  • Make sure all .h, .cpp, .cs, .ps1 and .psm1 files have the correct copyright header (no new files)

  • This PR is ready to merge. This is not a work in progress.

  • Breaking changes

  • User-facing changes

    • Not Applicable
    • OR
    • Documentation needed
      • Issue filed:
  • Testing - New and feature

    • N/A or can only be tested interactively
    • OR
    • Existing command-discovery tests cover the affected behavior; no new behavior was introduced.

    Validation:

    • dotnet build src\System.Management.Automation\System.Management.Automation.csproj -c Release -f net11.0 -p:PublishReadyToRun=false --nologo — passed, 0 warnings/errors.
    • Start-PSBuild -NoPSModuleRestore -Configuration Debug -SkipExperimentalFeatureGeneration with $env:PublishReadyToRun = 'false' — passed.
    • Start-PSPester -Path .\test\powershell\engine\Basic\CommandDiscovery.Tests.ps1 -BinDir .\src\powershell-win-core\bin\Debug\net11.0\win7-x64\publish -Tag CI -Quiet -ThrowOnFailure -SkipTestToolBuild — passed, 37 tests, 0 failures, 2 inconclusive.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@BigWorkk
BigWorkk requested a review from a team as a code owner October 17, 2026 19:10
Copilot AI balanced review requested due to automatic review settings October 17, 2026 19:10
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

Every removal path initializes the dictionary before use, and the guarded loop preserves existing behavior.

No open issues 🎉

What changed in this PR

Reduces allocations during PowerShell command lookup without changing path resolution.

Changes:

  • Allocates the removal-index dictionary only when a path needs removal.
  • Skips the removal loop when no dictionary was allocated.
File Description
src/​System.Management.Automation/​engine/​CommandPathSearch.cs Lazily initializes removal tracking and guards its use.

Review effort: Balanced

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants