Agents In Use
What’s new todayLast update
1005 additions

How to use GitHub Copilot to port a regex fast path

Copilot ported an alternation optimization from .NET's regex source generator to its IL compiler, following a supplied switch heuristic and maintainer review.

FROM THE SOURCE

Original prompt

Shared by the source author. See source notes for context.

Please port this optimization to the regex compiler, using the same heuristic the C# compiler (Roslyn) uses,

What happened

Copilot ported an alternation optimization from .NET's regex source generator to its IL compiler, following a supplied switch heuristic and maintainer review; the pull request merged into dotnet/runtime main.

Reported by Copilot (on behalf of stephentoub) on GitHub

How the agent did it

  1. Examined source-generator optimization.
  2. Applied Roslyn switch-density heuristic in RegexCompiler.
  3. Factored shared starting-character logic and adjusted source generator.

Where a person stepped in

Human supplied optimization and heuristic, reviewed after flight and merged.

Stephen Toub asked GitHub Copilot to bring two .NET regex implementations back into closer alignment. The source generator already optimized alternations whose branches provably began with different characters. Its generated C# could use a switch, leaving the C# compiler to choose an efficient lowering. RegexCompiler did not yet have the equivalent optimization.

The task therefore required more than copying text. Toub's prompt identified the existing implementation and supplied the Roslyn density heuristic for deciding when a switch was worthwhile. Copilot implemented the corresponding logic in the compiler that emits IL, factored shared starting-character analysis into RegexNode.TryGetAlternationStartingChars and adjusted the source generator's condition flow.

The PR records a concrete transfer of an existing optimization into another execution path. Toub reviewed the generated work and merged it on January 27, 2026. The related first-person account explains that this was among tasks initiated while he traveled, with detailed review still needed afterward.

No general benchmark speedup is established by this account. The useful input was a named source implementation, a target path and an explicit decision rule.

Prompt
Original prompt

Source: GitHub · Copilot (on behalf of stephentoub)

Frequently asked questions

Why was this not a direct code copy?

The source generator emits C#, while RegexCompiler emits IL. The agent needed to implement equivalent behavior using an explicit switch-selection heuristic.

What shared analysis was introduced?

The work log records factoring alternation starting-character logic into RegexNode.TryGetAlternationStartingChars.

What outcome is verified in the source?

The Copilot-authored pull request merged on January 27, 2026. The account does not establish a universal performance improvement.

More details

About this account

GitHub Copilot investigated or implemented the scoped repository task and produced a pull request, with work and checks recorded for maintainer review.

Primary pull request and public API timeline opened and checked on 2026-10-14. PR creation date is the original publication date; merge dates are separate result evidence. Complex IL generation still needed detailed review. Outcomes and test claims are attributed to the source and were not independently reproduced. Original prompt excerpt: reproduced verbatim from the public PR prompt; the full task contains additional context.

Credit: Copilot (on behalf of stephentoub) on GitHub

Source published 7 Jan 2026

Source checked 14 Oct 2026

Tools and topics

GitHub Copilot · RegexCompiler · System.Reflection.Emit · Roslyn · GitHub

Compiler optimization · Regex · IL generation

Editorial completeness: 100/100. Measures documented fields, not agent performance or verified outcomes.