How to use GitHub Copilot to optimize HashSet union
Copilot optimized UnionWith on an empty HashSet by reusing ConstructFrom when comparers match, adding parameterized coverage before a maintainer merged it.
Original prompt
Shared by the source author. See source notes for context.
[API Proposal]: HashSet<T>.UnionWith should copy data from another HashSet<T>
Reach people exploring AI agents
See placements →What happened
Copilot optimized UnionWith on an empty HashSet by reusing ConstructFrom when comparers match, adding parameterized coverage before a maintainer merged it; the pull request merged into dotnet/runtime main.
Reported by Copilot (on behalf of stephentoub) on GitHubHow the agent did it
- Detected empty destination and equivalent comparer.
- Reused ConstructFrom instead of per-element hash/collision checks.
- Added parameterized empty/nonempty/sparse-source tests.
Where a person stepped in
Human assigned proposal and reviewed/merged.
A .NET proposal identified avoidable work when repopulating an empty HashSet through UnionWith. Even when the source was another HashSet with a matching effective comparer, the method added elements individually and performed their usual hash and collision checks. Stephen Toub delegated the proposal to GitHub Copilot.
Copilot added a guarded fast path: if the destination was empty, the source was a HashSet and the effective comparers matched, it reused ConstructFrom to copy the internal structures. That method already served the constructor path, so the change reused existing behavior rather than inventing a separate copy algorithm.
The agent also added a parameterized test covering empty and nonempty destinations, ordinary and sparse source sets, and several element counts. The PR description reports the collections test suite passing. Toub reviewed the changes and merged the pull request on January 12, 2026.
The result is a specific optimized case, not an unconditional replacement of UnionWith's normal loop or a measured universal speedup. The comparer and emptiness guards remain central to the account.
Prompt
Original prompt
Source: GitHub · Copilot (on behalf of stephentoub)
Frequently asked questions
When does the new fast path apply?
The destination must be empty, the source must be another HashSet and their effective comparers must match.
What existing implementation was reused?
Copilot reused ConstructFrom, which was already used by the HashSet constructor, to copy internal data structures.
What test boundaries were exercised?
Parameterized coverage includes empty and nonempty destinations, sparse and ordinary sources, and different element counts.
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. Optimization triggers only for matching comparers and empty destination; no speedup number established. 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 · HashSet · C# · GitHub
Collections · Fast path · Comparer semantics
Editorial completeness: 100/100. Measures documented fields, not agent performance or verified outcomes.