Optimize ImmutableHashSet<T>.IsSubsetOf to avoid unnecessary allocations#127920
Draft
aw0lid wants to merge 1 commit intodotnet:mainfrom
Draft
Optimize ImmutableHashSet<T>.IsSubsetOf to avoid unnecessary allocations#127920aw0lid wants to merge 1 commit intodotnet:mainfrom
aw0lid wants to merge 1 commit intodotnet:mainfrom
Conversation
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-collections |
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.
Part of #127279
Summary
ImmutableHashSet<T>.IsSubsetOfalways creates a new intermediateHashSet<T>for theothercollection, leading to avoidable allocations and GC pressure, especially for large datasetsOptimization Logic
O(1) Pre-Scan: Immediately returns false if other is an
ICollectionwith a smaller count than the source.. By performing this validation upfront, the need for tracking variables likematchesis eliminated, as any complete match is now mathematically guaranteed to be a subset.Fast-Path Pattern Matching: Detects
ImmutableHashSet<T>andHashSet<T>to bypass intermediate allocations.Comparer Guard: Validates
EqualityComparercompatibility before triggering fast paths to ensure logical consistency.Short-Circuit Validation: Re-validates$O(n)$ enumeration.
Countwithin specialized paths for an immediate exit beforeReverse-Lookup Strategy: An architectural shift where the ImmutableHashSet (The Source) iterates and queries the other collection if was Hashset. This leverages the O(1) lookup of the HashSet instead of the O(log N) lookup of the immutable tree.
Zero-Allocation Execution: Direct iteration over compatible collections, eliminating the costly
new HashSet<T>(other)fallback.Deferred fallback: Reserves the expensive allocation solely for general
IEnumerabletypes.Click to expand Benchmark Source Code
Click to expand Benchmark Results
Benchmark Results (Before Optimization)
Benchmark Results (After Optimization)
Performance Analysis Summary (100,000 Elements)
Environment
✅ Unit Tests Added
Added unit tests for
IsSubsetOfto cover various edge cases and ensure the correctness of the new logic:Ordinalvs.OrdinalIgnoreCase).ICollection<T>logic to ensure that collections with duplicates are handled correctly by the early exit (Count < origin.Count).