Repository navigation
Skip clearing reference-free HashSet entries - #137582
Merged
Merged
Conversation
In HashSet<T>.Clear, call Array.Clear on the _entries field only when RuntimeHelpers.IsReferenceOrContainsReferences<Entry>() returns true.
|
Azure Pipelines: Successfully started running 3 pipeline(s). 12 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-collections |
prozolic
marked this pull request as ready for review
November 15, 2026 13:35
This was referenced Nov 15, 2026
Open
jkotas
approved these changes
Nov 15, 2026
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are reported for the reference-free HashSet clearing optimization.
No open issues 🎉
What changed in this PR
Applies the reference-free clearing optimization from Dictionary PR #136605 to HashSet<T>, avoiding unnecessary entry-array writes while retaining clearing for entries that contain managed references.
Changes:
- Guards entry-array clearing with
RuntimeHelpers.IsReferenceOrContainsReferences<Entry>(), preserving bucket and count resets. - Adds repeated clear-and-refill tests covering removed entries, capacity preservation, lookup results, and existing enumerators.
| File | Description |
|---|---|
src/libraries/System.Private.CoreLib/src/System/Collections/Generic/HashSet.cs |
Skips clearing reference-free entries and documents why reuse remains safe. |
src/libraries/System.Collections/tests/Generic/HashSet/HashSet.Generic.Tests.cs |
Adds clear-and-refill coverage across several set sizes. |
Review effort: Balanced
Member
|
/ba-g opened #137589 on the build failure |
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.
This PR uses the optimization pattern used in #136605.
In
HashSet<T>.Clear, callArray.Clearon the_entriesfield only whenRuntimeHelpers.IsReferenceOrContainsReferences<Entry>()returns true.Benchmark
Benchmark source (generated with Claude Code)