[efficiency-improver] perf: cache newline+color string in SimpleAnsiTerminal#8834
Merged
Evangelink merged 1 commit intoJun 5, 2026
Conversation
…all allocation
SetColorPerLine previously computed $"\\n{\_foregroundColor}" on every
Append/AppendLine call when a color was active. Add a `_newlineAndColor`
field that is populated once inside SetColor (and cleared in ResetColor),
so the replacement string is built at most once per color-change rather
than once per terminal write.
Proxy metric: heap allocation.
In a typical colored test run (CI output) this eliminates one small string
allocation per colored Append/AppendLine call, reducing GC pressure.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR optimizes Microsoft.Testing.Platform’s ANSI terminal output by removing a per-call string allocation in SimpleAnsiTerminal.SetColorPerLine, which is invoked for every Append/AppendLine while a foreground color is active (common during colored CI logs).
Changes:
- Cache the computed newline+color prefix (
"\n" + setColor) once perSetColorcall. - Reuse the cached value in
SetColorPerLineto avoid allocating the same replacement string on every append. - Clear the cached value in
ResetColorto keep state consistent.
Show a summary per file
| File | Description |
|---|---|
| src/Platform/Microsoft.Testing.Platform/OutputDevice/Terminal/SimpleAnsiTerminal.cs | Adds _newlineAndColor cache and uses it in SetColorPerLine to eliminate repeated newline+color string allocations during colored output. |
Copilot's findings
- Files reviewed: 1/1 changed files
- Comments generated: 0
JanKrivanek
approved these changes
Jun 5, 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.
🤖 Efficiency Improver — automated AI assistant focused on reducing the energy consumption and computational footprint of this repository.
Goal and Rationale
Eliminate a per-call heap allocation in
SimpleAnsiTerminal.SetColorPerLine. This method is called on everyAppendandAppendLineinvocation while a foreground color is active — which is the common case during any colored test run in a CI environment (Azure DevOps, GitHub Actions, etc.).Focus Area
Code-Level Efficiency — unnecessary object allocation per terminal write.
Approach
Previously,
SetColorPerLineconstructed the replacement string$"\n{_foregroundColor}"on every call:Since
_foregroundColoronly changes whenSetColororResetColoris called, the"\n" + _foregroundColorstring can be cached at color-change time and reused for all subsequent writes:Energy Efficiency Evidence
Proxy metric: heap allocation count.
Append/AppendLinecall when a color is activeSetColorinvocationSimpleAnsiTerminalis used in CI environments that support ANSI but not full cursor control (Azure DevOps, GitHub Actions, etc.). A test run producing 1 000 colored output lines would previously allocate ~1 000 unnecessary small strings (~10–15 bytes each). These all become GC garbage immediately, adding pressure on the GC and wasting CPU cycles reclaiming them.GSF — Hardware Efficiency: fewer short-lived heap objects → less GC work → better utilization of CPU and DRAM already in use.
Trade-offs
string?field (_newlineAndColor) is added toSimpleAnsiTerminal. This is negligible — one pointer-sized field on a per-session singleton._newlineAndColoris always"\n" + _foregroundColorwhen non-null, matching the previous inline computation exactly.Reproducibility
./build.sh # verify build succeedsUnit test coverage:
TerminalTestReporterTests.SimpleAnsiTerminal_OutputFormattingIsCorrectexercises colored output throughSimpleAnsiTerminal.Test Status
✅ Build succeeded (0 errors, 0 warnings)
✅
Microsoft.Testing.Platform.UnitTests— all tests passedAdd this agentic workflows to your repo
To install this agentic workflow, run