Skip to content

[perf-improver] Cache invalid-path/filename char arrays in DeploymentItemUtility - #11555

Merged
Amaury Levé (Evangelink) merged 1 commit into
mainfrom
perf-assist/deploymentitem-invalidpath-cache-9d38f9eca538d42c
Sep 27, 2026
Merged

Amaury Levé (Evangelink) merged 1 commit into
mainfrom
perf-assist/deploymentitem-invalidpath-cache-9d38f9eca538d42c

Conversation

@Evangelink

Copy link
Copy Markdown
Member

…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

…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>
Copilot AI balanced review requested due to automatic review settings September 27, 2026 11:58
@github-actions github-actions Bot added the state/needs-review Awaiting review from the team. label Sep 27, 2026
@microsoft-github-policy-service microsoft-github-policy-service Bot added state/needs-review Awaiting review from the team. and removed state/needs-review Awaiting review from the team. labels Sep 27, 2026
@github-actions github-actions Bot added state/approved Proposal approved; ready for implementation. and removed state/needs-review Awaiting review from the team. labels Sep 27, 2026

@github-actions github-actions Bot 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.

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() and Path.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 wider DeploymentItemUtility/FileUtility call sites I checked. No aliasing/mutation risk.
  • Thread-safety: static readonly field 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 readonly on an internal sealed class; no PublicAPI.Unshipped.txt changes are required, and none were made.

Style / naming conventions

  • Verified against .editorconfig: dotnet_naming_rule.static_readonly_fields_should_be_pascal_case (severity warning) applies to any field with static + readonly modifiers regardless of accessibility, taking precedence over the private-static s_camelCase rule (which additionally requires the field not be readonly, per its symbol spec required_modifiers = static only being matched when the readonly symbol group doesn't already claim it). So InvalidPathChars / InvalidFileNameChars (PascalCase) is the correct convention here, not s_invalidPathChars.
  • Found a direct precedent in this repo: src/Platform/Microsoft.Testing.Extensions.VideoRecorder/VideoRecorderSessionHandler.cs already caches private 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 DeploymentItemUtilityTests in test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Utilities/DeploymentItemUtilityTests.cs continue to exercise IsInvalidPath indirectly via IsValidDeploymentItem and are unaffected by this change.

Verdict

Correct, low-risk, and consistent with existing codebase conventions. No blocking issues found.

@microsoft-github-policy-service microsoft-github-policy-service Bot added the state/needs-review Awaiting review from the team. label Sep 27, 2026
@github-actions github-actions Bot added state/approved Proposal approved; ready for implementation. and removed state/needs-review Awaiting review from the team. state/approved Proposal approved; ready for implementation. labels Sep 27, 2026

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.

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.

@Evangelink
Amaury Levé (Evangelink) merged commit f37a08b into main Sep 27, 2026
28 checks passed
@Evangelink
Amaury Levé (Evangelink) deleted the perf-assist/deploymentitem-invalidpath-cache-9d38f9eca538d42c branch September 27, 2026 13:13
@github-actions github-actions Bot removed the state/approved Proposal approved; ready for implementation. label Sep 27, 2026
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.

[perf-improver] Cache invalid-path/filename char arrays in DeploymentItemUtility

3 participants