Skip to content

[perf-improver] perf: eliminate per-call string allocation in AnsiTerminal.SetColor and use HashSet for KnownFileExtensions#8739

Merged
Evangelink merged 1 commit into
mainfrom
perf-assist/ansi-terminal-alloc-reduction-b9fe2463258bb028
Jun 2, 2026
Merged

[perf-improver] perf: eliminate per-call string allocation in AnsiTerminal.SetColor and use HashSet for KnownFileExtensions#8739
Evangelink merged 1 commit into
mainfrom
perf-assist/ansi-terminal-alloc-reduction-b9fe2463258bb028

Conversation

@Evangelink
Copy link
Copy Markdown
Member

🤖 This is an automated contribution from Perf Improver.

Goal and Rationale

Two allocation hotspots in the terminal output path:

  1. AnsiTerminal.SetColor — Every call to SetColor(TerminalColor) built a new string via $"{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 possible TerminalColor values, so the strings can be pre-computed.

  2. KnownFileExtensions — The 13-element string[] was scanned linearly by Array.Contains on every AppendLink call (triggered for each failed-test file link in the run summary). A HashSet<string> gives O(1) lookup.

Approach

  1. 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.

  2. AnsiTerminal.SetColor now calls AnsiCodes.GetSetColorEscapeCode(color) instead of building the string inline.

  3. Changed KnownFileExtensions from private static readonly string[] to private static readonly HashSet<string> initialized with StringComparer.Ordinal (preserving the existing case-sensitive behaviour).

Performance Evidence

Site Before After
AnsiTerminal.SetColor (per call) 1 × string allocation (~6–8 bytes, formatting) 0 — returns interned literal
KnownFileExtensions.Contains O(n) — up to 13 comparisons O(1) HashSet lookup
Frame-level colour allocations (10 assemblies, 500 ms cadence) ~30 strings/sec 0

Methodology: code inspection + diff review. The switch expression returns literal strings which the runtime interns; no heap allocation occurs.

Trade-offs

  • AnsiCodes gains one method (~20 lines). The switch explicitly enumerates all 17 TerminalColor values; adding a new value without updating the switch falls back to the interpolated string (no regression).
  • KnownFileExtensions is now a HashSet (one allocation at class init) instead of an array. The comparer is Ordinal to preserve existing case-sensitivity.

Test Status

  • Microsoft.Testing.Platform.UnitTests (net8.0): 995 passed, 0 failed, 3 skipped

Reproducibility

./build.sh
artifacts/bin/Microsoft.Testing.Platform.UnitTests/Debug/net8.0/Microsoft.Testing.Platform.UnitTests

Generated by Perf Improver · sonnet46 7.6M ·

Add this agentic workflows to your repo

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/perf-improver.md@main

…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>
Copilot AI review requested due to automatic review settings June 1, 2026 16:14
@Evangelink Evangelink added area/performance Runtime / build performance / efficiency. type/automation Created or maintained by an agentic workflow. labels Jun 1, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.SetColor by routing through a precomputed escape-code mapping.
  • Replace linear extension lookup (string[] + Contains) with a HashSet<string> for O(1) membership checks in AppendLink.
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

@Evangelink Evangelink marked this pull request as ready for review June 2, 2026 06:26
@Evangelink Evangelink enabled auto-merge (squash) June 2, 2026 06:26
@Evangelink Evangelink merged commit 42bc37b into main Jun 2, 2026
28 of 29 checks passed
@Evangelink Evangelink deleted the perf-assist/ansi-terminal-alloc-reduction-b9fe2463258bb028 branch June 2, 2026 08:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/performance Runtime / build performance / efficiency. type/automation Created or maintained by an agentic workflow.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants