Repository navigation
[perf-improver] Cache invalid-path/filename char arrays in DeploymentItemUtility - #11555
Conversation
…temUtility Avoid re-allocating the invalid-path and invalid-filename character arrays on every IsInvalidPath call. This runs once per [DeploymentItem]-attributed test method during test discovery, so caching the static, process-lifetime arrays removes an unnecessary allocation from that path. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Note
🤖 This review was generated automatically by Copilot acting as the expert-reviewer agent, triggered by workflow run 36317438849.
Summary
This is a small, low-risk performance micro-optimization in DeploymentItemUtility.IsInvalidPath. It replaces two repeated calls to Path.GetInvalidPathChars() / Path.GetInvalidFileNameChars() (each of which allocates a new char[] on every invocation) with private static readonly cached fields computed once per process.
Correctness
- Semantic equivalence:
Path.GetInvalidPathChars()andPath.GetInvalidFileNameChars()return a fixed, platform-defined set of characters that does not change during process lifetime (confirmed by .NET's own implementation — these are compile-time constant arrays per platform). Caching them is behavior-preserving. - Mutation safety: The arrays are only read via
IndexOfAny; neither field nor its backing array is written to or exposed anywhere in this file or the widerDeploymentItemUtility/FileUtilitycall sites I checked. No aliasing/mutation risk. - Thread-safety:
static readonlyfield initializers on a type run under the CLR's type-initialization semantics (single-threaded, before-field-init or type constructor lock), so concurrent test-discovery threads all observe the fully-initialized array. No additional synchronization is needed here. - No public API impact: Both new fields are
private static readonlyon aninternal sealed class; noPublicAPI.Unshipped.txtchanges are required, and none were made.
Style / naming conventions
- Verified against
.editorconfig:dotnet_naming_rule.static_readonly_fields_should_be_pascal_case(severitywarning) applies to any field withstatic+readonlymodifiers regardless of accessibility, taking precedence over the private-statics_camelCaserule (which additionally requires the field not bereadonly, per its symbol specrequired_modifiers = staticonly being matched when thereadonlysymbol group doesn't already claim it). SoInvalidPathChars/InvalidFileNameChars(PascalCase) is the correct convention here, nots_invalidPathChars. - Found a direct precedent in this repo:
src/Platform/Microsoft.Testing.Extensions.VideoRecorder/VideoRecorderSessionHandler.csalready cachesprivate static readonly char[] InvalidFileNameChars = Path.GetInvalidFileNameChars();with identical naming — this PR is consistent with existing practice. - The added inline comment explaining the rationale is clear and appropriately scoped (not over-commenting).
Minor observation (non-blocking, posted inline)
- The two new fields are declared between two method members rather than grouped with the other private fields near the top of the class.
SA1201/SA1204(element ordering) are disabled repo-wide in.editorconfig, so this isn't an analyzer violation — just a readability nit.
Testing
- No behavior change, so no new tests are required; existing
DeploymentItemUtilityTestsintest/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Utilities/DeploymentItemUtilityTests.cscontinue to exerciseIsInvalidPathindirectly viaIsValidDeploymentItemand are unaffected by this change.
Verdict
Correct, low-risk, and consistent with existing codebase conventions. No blocking issues found.
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
The focused allocation optimization preserves existing validation behavior and introduces no unresolved issues.
Review effort: Balanced
Findings: None
What changed in this PR
Caches invalid-path character arrays in MSTest adapter deployment validation, eliminating repeated allocations during test discovery without changing behavior.
Changes:
- Adds process-lifetime caches for invalid path and filename characters.
- Reuses cached arrays in
IsInvalidPath.
Confidence at a glance
🟢 Adapter validation and performance — High confidence
The change is thread-safe, behavior-preserving, and covered by existing validation tests.| File | Description |
|---|---|
src/Adapter/MSTestAdapter.PlatformServices/Utilities/DeploymentItemUtility.cs |
Caches and reuses invalid-character arrays. |
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
…temUtility
Avoid re-allocating the invalid-path and invalid-filename character arrays on every IsInvalidPath call. This runs once per [DeploymentItem]-attributed test method during test discovery, so caching the static, process-lifetime arrays removes an unnecessary allocation from that path.
Fixes #11544