Repository navigation
Reduce allocations for cached command lookups - #2221
Closed
Przemysław Kłys (PrzemyslawKlys) wants to merge 1 commit into
Closed
Przemysław Kłys (PrzemyslawKlys) wants to merge 1 commit into
Przemysław Kłys (PrzemyslawKlys) wants to merge 1 commit into
Conversation
Przemysław Kłys (PrzemyslawKlys)
requested review from
a team and
Christoph Bergmeister (bergmeister)
as code owners
October 5, 2026 10:10
Copilot started reviewing on behalf of
Przemysław Kłys (PrzemyslawKlys)
October 5, 2026 10:10
View session
Contributor
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
The focused optimization preserves existing semantics and is adequately covered by targeted tests.
Review effort: Balanced
Findings: None
What changed in this PR
Optimizes cached command lookups while preserving lookup behavior and thread safety.
Changes:
- Avoids miss-only allocations on cache hits.
- Uses ordinal case-insensitive hashing.
- Adds cache identity, filtering, and culture tests.
| File | Description |
|---|---|
Engine/CommandInfoCache.cs |
Adds allocation-free cache-hit path and matching hash semantics. |
Tests/Engine/CommandInfoCache.tests.ps1 |
Tests casing, command types, and culture independence. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
Fixed in #2206 with more significant improvements. Verdict: PR #2221’s implementation is already covered in #2206. The two substantive optimizations are present:
|
Jesse Houwing (jessehouwing)
added a commit
to jessehouwing/PSScriptAnalyzer
that referenced
this pull request
Oct 7, 2026
…d command-type filtering Adding additional test cases from PowerShell#2221.
Contributor
Author
|
I did steal your additional tests! Thanks! Would be helpful if you can run the code from that PR and report your findings. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Summary
Repeated command lookups allocate a closure, a
Lazy<CommandInfo>, and an uppercased key even when the result is already cached. Return the existing lazy value first, move miss-only allocations into a helper, and useStringComparer.OrdinalIgnoreCasefor hashing to match key equality.Command discovery, command-type filters, bypass behavior, and the winning lazy value on concurrent misses are unchanged. Tests cover cached identity across casing and equivalent command types, separate type filters, and culture-independent lookup.
With command names extracted from Locksmith and Locksmith2, cached-lookup medians fall by 55–61% across two CPU cache domains (two warmups and seven rotated samples per lane). The measured C# lookup loop allocates zero bytes after the change, versus 203 MB / 333 MB before it for 890,000 / 1,400,000 lookups. These are cached-lookup results, not whole-formatting or build speedups. Baseline:
411c3d0b197b488285bddd2375b292ab7b8db5e8.Validation:
net8andnet462builds; 41 focused tests pass with one skipped on each PowerShell Core and Windows PowerShell. Full formatting of both modules produces identical bytes and diagnostics before/after on each runtime. Locksmith2 retains an existing attribute-construction diagnostic in this isolated test.Related performance discussion: #1528.
PR Checklist