Optimize HashSet.UnionWith to copy data from another HashSet when empty#122952
Merged
stephentoub merged 4 commits intomainfrom Jan 12, 2026
Merged
Optimize HashSet.UnionWith to copy data from another HashSet when empty#122952stephentoub merged 4 commits intomainfrom
stephentoub merged 4 commits intomainfrom
Conversation
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-collections |
…rce and add tests Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix HashSet<T>.UnionWith to copy data from another HashSet<T>
Optimize HashSet.UnionWith to copy data from another HashSet when empty
Jan 7, 2026
stephentoub
reviewed
Jan 7, 2026
src/libraries/System.Collections/tests/Generic/HashSet/HashSet.Generic.Tests.cs
Show resolved
Hide resolved
stephentoub
approved these changes
Jan 7, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR optimizes HashSet<T>.UnionWith to directly copy internal data structures when called on an empty HashSet with another HashSet that has the same effective comparer, rather than iterating through elements and adding them individually.
- Adds fast-path optimization in
UnionWithusing existingConstructFrommethod - Adds comprehensive test coverage for the new optimization path
- Mirrors existing optimizations in the HashSet constructor and Dictionary.AddRange
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/libraries/System.Private.CoreLib/src/System/Collections/Generic/HashSet.cs | Adds optimization check before element iteration to use ConstructFrom when destination is empty and comparers match |
| src/libraries/System.Collections/tests/Generic/HashSet/HashSet.Generic.Tests.cs | Adds 4 new test methods covering normal sets, sparsely-filled sets, empty source, and non-empty destination fallback scenarios |
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
stephentoub
reviewed
Jan 7, 2026
src/libraries/System.Collections/tests/Generic/HashSet/HashSet.Generic.Tests.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
auto-merge was automatically disabled
January 7, 2026 02:02
Head branch was pushed to by a user without write access
This was referenced Jan 7, 2026
This was referenced Jan 7, 2026
stephentoub
approved these changes
Jan 7, 2026
eiriktsarpalis
approved these changes
Jan 12, 2026
This was referenced Jan 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
When
UnionWithis called on an emptyHashSet<T>with anotherHashSet<T>that has the same effective comparer, we can useConstructFromto copy the internal data structures directly instead of adding each element individually with collision checks.This mirrors the existing optimization in the
HashSetconstructor and is similar toDictionary<TKey, TValue>'sAddRangeoptimization.Customer Impact
Performance improvement for common pattern of clearing a HashSet and repopulating via
UnionWithfrom another HashSet.Regression
No, this is a new optimization.
Testing
HashSet_Generic_UnionWith_HashSet(int count, bool destinationEmpty, bool sourceSparseFilled)usingMemberDatawith nested foreach loops to generate 12 combinations covering: empty/non-empty destinations, sparse-filled/normal sources, and various element counts (0, 1, 75)Risk
Low. Uses existing
ConstructFrommethod already proven in the constructor path. Optimization only triggers when destination is empty and comparers are identical.Package authoring no longer needed in .NET 9
IMPORTANT: Starting with .NET 9, you no longer need to edit a NuGet package's csproj to enable building and bump the version.
Keep in mind that we still need package authoring in .NET 8 and older versions.
Original prompt
This section details on the original issue you should resolve
<issue_title>[API Proposal]: HashSet.UnionWith should copy data from another HashSet</issue_title>
<issue_description>### Background and motivation
I'll describe my specific case
HashSet<ITarget> _visibleTargetsSet = new HashSet<ITarget>(); HashSet<ITarget> _registerdTargetsSet = new HashSet<ITarget>(); HashSet<ITarget> _safeTargetsSet = new HashSet<ITarget>(); List<ITarget> _visibleTargets = new List<ITarget>(); private void SetupVisibleTargets() { _visibleTargetsSet.Clear(); _visibleTargetsSet.UnionWith(_registeredTargetsSet); _visibleTargetsSet.ExceptWith(_safeTargetsSet); _visibleTargets.Clear(); _visibleTargets.AddRange(_visibleTargetsSet); _safeTargetsSet.Clear(); }In this example, I find the difference between the sets of registered and visible targets. _safeTargetsSet is calculated somewhere else, and registerd targets are needed to stay immutable. I don't want to create new collections, so I use clear and use _safeTargetsSet every time i need a new data for _visibleTargets. The problem is that method UnionWith is just adding every item separately with collision checks even if count is 0, unification occurs with HashSet and comparers are equal.
API Proposal
API Usage
Alternative Designs
It can be done with method overloading, not a current method change.
Risks
Idk</issue_description>
Comments on the Issue (you are @copilot in this section)
@ Tagging subscribers to this area: @dotnet/area-system-collections See info in [area-owners.md](https://github.com/dotnet/runtime/blob/main/docs/area-owners.md) if you want to be subscribed.Issue Details
Background and motivation
I'll describe my specific case
HashSet<ITarget> _visibleTargetsSet = new HashSet<ITarget>(); HashSet<ITarget> _registerdTargetsSet = new HashSet<ITarget>(); HashSet<ITarget> _safeTargetsSet = new HashSet<ITarget>(); List<ITarget> _visibleTargets = new List<ITarget>(); private void SetupVisibleTargets() { _visibleTargetsSet.Clear(); _visibleTargetsSet.UnionWith(_registeredTargetsSet); _visibleTargetsSet.ExceptWith(_safeTargetsSet); _visibleTargets.Clear(); _visibleTargets.AddRange(_visibleTargetsSet); _safeTargetsSet.Clear(); }In this example, I find the difference between the sets of registered and visible targets. _safeTargetsSet is calculated somewhere else, and registerd targets are needed to stay immutable. I don't want to create new collections, so I use clear and use _safeTargetsSet every time i need a new data for _visibleTargets. The problem is that method UnionWith is just adding every item separately with coolision checks even if count is 0, unification occurs with HashSet and comparers are equal.
API Proposal
API Usage