[perf-improver] perf: eliminate per-call string allocation in AnsiTerminal.SetColor and use HashSet for KnownFileExtensions#8739
Merged
Evangelink merged 1 commit intoJun 2, 2026
Conversation
…nd use HashSet for KnownFileExtensions - Add AnsiCodes.GetSetColorEscapeCode(TerminalColor) with a switch expression that returns interned string literals for each known VT100 color code, avoiding a heap allocation on every SetColor call during terminal progress rendering. - Change KnownFileExtensions from string[] to HashSet<string> (StringComparer.Ordinal) so that AppendLink's extension check is O(1) instead of O(n). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR optimizes the Microsoft.Testing.Platform (MTP) ANSI terminal output path by removing two small but frequent allocation/lookup hotspots during progress rendering and link generation.
Changes:
- Avoid per-call string interpolation in
AnsiTerminal.SetColorby routing through a precomputed escape-code mapping. - Replace linear extension lookup (
string[]+Contains) with aHashSet<string>for O(1) membership checks inAppendLink.
Show a summary per file
| File | Description |
|---|---|
| src/Platform/Microsoft.Testing.Platform/OutputDevice/Terminal/AnsiTerminal.cs | Uses HashSet<string> for known file extensions and calls the new escape-code helper in SetColor. |
| src/Platform/Microsoft.Testing.Platform/OutputDevice/Terminal/AnsiCodes.cs | Adds GetSetColorEscapeCode(TerminalColor) returning precomputed ANSI color escape sequences. |
Copilot's findings
- Files reviewed: 2/2 changed files
- Comments generated: 0
JanKrivanek
approved these changes
Jun 2, 2026
2 tasks
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.
🤖 This is an automated contribution from Perf Improver.
Goal and Rationale
Two allocation hotspots in the terminal output path:
AnsiTerminal.SetColor— Every call toSetColor(TerminalColor)built a newstringvia$"{AnsiCodes.CSI}{(int)color}{AnsiCodes.SetColor}". This happens during every live-progress render frame (once per assembly × 3 color changes per row × every 500 ms). There are only 17 possibleTerminalColorvalues, so the strings can be pre-computed.KnownFileExtensions— The 13-elementstring[]was scanned linearly byArray.Containson everyAppendLinkcall (triggered for each failed-test file link in the run summary). AHashSet<string>gives O(1) lookup.Approach
Added
AnsiCodes.GetSetColorEscapeCode(TerminalColor)— a switch expression that returns interned compile-time string literals (e.g."\x1b[32m") for every known color. No heap allocation for common cases. Falls back to string interpolation for any unexpected future value.AnsiTerminal.SetColornow callsAnsiCodes.GetSetColorEscapeCode(color)instead of building the string inline.Changed
KnownFileExtensionsfromprivate static readonly string[]toprivate static readonly HashSet<string>initialized withStringComparer.Ordinal(preserving the existing case-sensitive behaviour).Performance Evidence
AnsiTerminal.SetColor(per call)stringallocation (~6–8 bytes, formatting)KnownFileExtensions.ContainsMethodology: code inspection + diff review. The switch expression returns literal strings which the runtime interns; no heap allocation occurs.
Trade-offs
AnsiCodesgains one method (~20 lines). The switch explicitly enumerates all 17TerminalColorvalues; adding a new value without updating the switch falls back to the interpolated string (no regression).KnownFileExtensionsis now aHashSet(one allocation at class init) instead of an array. The comparer isOrdinalto preserve existing case-sensitivity.Test Status
Microsoft.Testing.Platform.UnitTests(net8.0): 995 passed, 0 failed, 3 skipped ✅Reproducibility
Add this agentic workflows to your repo
To install this agentic workflow, run