Skip to content

[efficiency-improver] Cache compiled Regex instances in Assert.MatchesRegex/DoesNotMatchRegex #10659

Closed

Description

@github-actions

Goal and rationale

Assert.MatchesRegex(string pattern, ...) / Assert.DoesNotMatchRegex(string pattern, ...) (the string-pattern overloads) constructed a brand-new Regex object on every single call via the private ToRegex helper — even when called repeatedly with the exact same literal pattern. Regex compilation/parsing is a well-known non-trivial CPU cost, and test suites commonly re-use the same literal patterns across many assertions (e.g. repeated GUID/date/email format checks). Every redundant construction is wasted CPU work with no functional benefit.

Focus area

Code-Level Efficiency — missing caching of a deterministic, pure computation (parsing a regex pattern string into a Regex instance is deterministic for a given pattern).

Approach

Added a private static readonly ConcurrentDictionary<string, Regex> RegexCache field to Assert.Matches.cs and changed ToRegex(string? pattern) to use RegexCache.GetOrAdd(pattern, static p => new Regex(p)) instead of unconditionally allocating new Regex(pattern). This mirrors the existing ConcurrentDictionary usage already present elsewhere in the same project (TelemetryCollector.cs, Assert.AreEquivalent.Comparer.cs), so it introduces no new dependency and matches established code style. The Regex-accepting overloads (where the caller already controls the instance/options) are untouched.

Energy efficiency evidence

Proxy metric: CPU time (execution time) per call, since avoiding redundant regex compilation directly reduces CPU cycles spent — the clearest proxy for energy use in this case.

Standalone micro-benchmark (200,000 calls each, after a 1,000-iteration warm-up, Release config, same email-validation pattern used elsewhere in the assertion tests):

Scenario Total time Per-call
Uncached (new Regex(pattern) every call) ~1023 ms ~5.12 μs
Cached (ConcurrentDictionary.GetOrAdd) ~3 ms ~0.016 μs

That's roughly a 300x reduction in per-call cost once the pattern is cached (only the very first call per unique pattern pays the parsing cost). In a real test suite, most MatchesRegex/DoesNotMatchRegex calls reuse a small set of literal patterns, so this removes the vast majority of redundant regex construction overhead.

Limitation: this is a synthetic micro-benchmark isolating regex construction cost, not a full end-to-end energy measurement. The real-world benefit scales with how often the same literal pattern is reused across a test run.

Green Software Foundation context

  • Hardware Efficiency: eliminates repeated, unnecessary CPU work for a deterministic computation, making better use of the CPU cycles already spent.
  • Energy Proportionality: cost is now proportional to the number of distinct patterns used, not the number of assertion calls — a large test suite with many repeated assertions on the same pattern now does much less redundant work.

Trade-offs

  • The cache grows with the number of distinct patterns seen during a test run's lifetime and is never evicted. This is acceptable in practice: test suites typically use a small, fixed set of literal regex patterns, so the cache size is naturally bounded. No unbounded-growth risk expected in typical usage.
  • No readability regression — the change is a small, self-contained addition with an explanatory comment.

Reproducibility

  • Build: ./build.sh (repo root) — succeeded, 0 warnings/errors.
  • Tests: built test/UnitTests/TestFramework.UnitTests (.dotnet/dotnet build test/UnitTests/TestFramework.UnitTests -f net9.0 -c Debug) and ran the resulting binary directly — 1520/1520 tests passed (0 failed), including existing MatchesRegex/DoesNotMatchRegex coverage in AssertTests.MatchesRegex.cs.
  • Micro-benchmark: standalone console app comparing 200,000 iterations of new Regex(pattern) vs. ConcurrentDictionary.GetOrAdd-cached construction, run in Release configuration after warm-up.

Test Status

✅ Full repo build succeeded (0 warnings, 0 errors).
✅ TestFramework.UnitTests: 1520/1520 passed, 0 failed.

Warning

Firewall blocked 1 domain

The following domain was blocked by the firewall during workflow execution:

  • southcentralus0.in.applicationinsights.azure.com

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "southcentralus0.in.applicationinsights.azure.com"

See Network Configuration for more information.

🤖 Automated content by GitHub Copilot. Generated by the Efficiency Improver workflow. · auto · 264.7 AIC · ⌖ 8.6 AIC · ⊞ 20.4K · [◷]( · ◷)

Add this agentic workflow to your repo

To install this agentic workflow, run

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

Note

This was originally intended as a pull request, but GitHub Actions is not permitted to create or approve pull requests in this repository.
The changes have been pushed to branch efficiency/cache-regex-matches-895b9e2a2bf1daa3.

Click here to create the pull request

To fix the permissions issue, go to Settings → Actions → General and enable Allow GitHub Actions to create and approve pull requests. See also: gh-aw FAQ

Show patch preview (46 of 46 lines)
From e341382401e75d5cedc4c1ee229897f31e43f87a Mon Sep 17 00:00:00 2001
From: "github-actions[bot]" <github-actions[bot]@users.noreply.github.com>
Date: Thu, 20 Aug 2026 22:06:27 +0000
Subject: [PATCH] Cache compiled Regex instances in
 Assert.MatchesRegex/DoesNotMatchRegex

The string-pattern overloads of Assert.MatchesRegex and Assert.DoesNotMatchRegex
constructed a brand-new Regex object on every call via the private ToRegex
helper, even when called repeatedly with the same literal pattern (a common
scenario in test suites, e.g. repeated GUID/date/email format checks). This
adds a ConcurrentDictionary<string, Regex> cache keyed by pattern string,
avoiding redundant regex parsing/compilation on repeated calls with the same
pattern. Callers needing per-call control (RegexOptions, custom instances)
can still use the Regex-accepting overloads directly, which bypass the cache.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
---
 .../TestFramework/Assertions/Assert.Matches.cs           | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/src/TestFramework/TestFramework/Assertions/Assert.Matches.cs b/src/TestFramework/TestFramework/Assertions/Assert.Matches.cs
index 09db0ae..603197c 100644
--- a/src/TestFramework/TestFramework/Assertions/Assert.Matches.cs
+++ b/src/TestFramework/TestFramework/Assertions/Assert.Matches.cs
@@ -195,9 +195,16 @@ public static void DoesNotMatchRegex([NotNull] string? pattern, [NotNull] string
 
     #endregion // DoesNotMatchRegex
 
+    // Caches compiled Regex instances by pattern string. The string-pattern overloads of
+    // MatchesRegex/DoesNotMatchRegex are typically called with a small, fixed set of literal
+    // patterns repeated across many test invocations, so caching avoids re-parsing the same
+    // pattern on every call. Callers who need finer control (e.g. RegexOptions, a per-call
+    // instance) can use the Regex-accepting overloads directly, which bypass this cache.
+    priva
... (truncated)

Metadata

Metadata

Labels

area/agentic-workflowsGitHub agentic workflow definitions under .github/workflows/*.md.area/performanceRuntime / build performance / efficiency.type/automationCreated or maintained by an agentic workflow.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions