Agents In Use
What’s new todayLast update
108 use cases added today

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.

FROM THE SOURCE

Original prompt

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

[API Proposal]: HashSet<T>.UnionWith should copy data from another HashSet<T>

Evidence at a glance

Sourced account
Original account
Read the source ↗
Published
Source link checked
Reported result
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.
Human involvement
Human assigned proposal and reviewed/merged.

A linked account supports attribution. It does not mean we independently reproduced its result.

Report an issue with this caseOpen correction email

Opens your email app with the case link included. Nothing is sent automatically.

How the agent did it

  1. Detected empty destination and equivalent comparer.
  2. Reused ConstructFrom instead of per-element hash/collision checks.
  3. Added parameterized empty/nonempty/sparse-source tests.

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

Added to Agents In Use

Source checked

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.