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.
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,
Reach people exploring AI agents
See placements →Evidence at a glance
Sourced account- Source author
- Copilot (on behalf of stephentoub)
- Original account
- Read the source ↗
- Published
- Source link checked
- Reported result
- 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.
- Human involvement
- Human supplied optimization and heuristic, reviewed after flight and merged.
A linked account supports attribution. It does not mean we independently reproduced its result.
Report an issue with this case
Open correction emailOpens your email app with the case link included. Nothing is sent automatically.
How the agent did it
- Examined source-generator optimization.
- Applied Roslyn switch-density heuristic in RegexCompiler.
- Factored shared starting-character logic and adjusted source generator.
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
Added to Agents In Use
Source checked
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.