Avoid TestContext write string allocations#10105
Merged
Evangelink merged 2 commits intoJul 21, 2026
Merged
Conversation
Skip null-character replacement when the input contains no null characters. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: abd02d85-66b7-43e1-b3ac-b08419abb848
Contributor
There was a problem hiding this comment.
Pull request overview
Optimizes null-character escaping in TestContext.Write and WriteLine.
Changes:
- Centralizes escaping in
EscapeNullChars. - Adds a pre-check before replacement.
Show a summary per file
| File | Description |
|---|---|
TestContextImplementation.cs |
Updates four write paths to use the helper. |
Review details
- Files reviewed: 1/1 changed files
- Comments generated: 1
- Review effort level: Medium
Contributor
There was a problem hiding this comment.
Note
🤖 Automated review by GitHub Copilot. Generated by the Expert Code Review workflow. To request a follow-up action, reply by tagging @copilot directly.
Review Summary
Verdict: Looks good ✅
This is a clean, focused performance optimization. The new EscapeNullChars helper avoids the Replace allocation when no null characters are present (the common case). The [return: NotNullIfNotNull] annotation preserves nullability flow.
All 22 dimensions checked — no issues found. The change is minimal, correct, and well-structured.
Evangelink
enabled auto-merge (squash)
July 21, 2026 10:56
This reverts commit b8a83fd.
0101
approved these changes
Jul 21, 2026
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.
Summary
Avoid calling
string.ReplaceinTestContext.WriteandWriteLinewhen the input contains no null characters. The new helper returns the original string in the common case and preserves the existing escaping behavior when null characters are present.Fixes #10095
Testing
MSTestAdapter.PlatformServices.UnitTestsfornet8.0MSTestAdapter.PlatformServices.UnitTeststests