-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Optimize ImmutableHashSet<T>.IsProperSubsetOf to avoid unnecessary allocations #127368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
aw0lid
wants to merge
1
commit into
dotnet:main
Choose a base branch
from
aw0lid:fix-immutablehashset-IsProperSubsetOf-allocs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+70
−27
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -296,6 +296,20 @@ public bool SetEquals(IEnumerable<T> other) | |||||||||||||||||||||||||||||||||||||
| public bool IsProperSubsetOf(IEnumerable<T> other) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| Requires.NotNull(other, nameof(other)); | ||||||||||||||||||||||||||||||||||||||
| if (this.Origin.Root.IsEmpty) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| return other.Any(); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| if (other is ICollection<T> otherAsICollectionGeneric) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| return IsProperSubsetOfFastPath(otherAsICollectionGeneric, this.Origin); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| else if (other is ICollection otherAsICollection && otherAsICollection.Count <= this.Origin.Count) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| return IsProperSubsetOf(other, this.Origin); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -863,49 +877,78 @@ private static MutationResult SymmetricExcept(IEnumerable<T> other, MutationInpu | |||||||||||||||||||||||||||||||||||||
| /// <summary> | ||||||||||||||||||||||||||||||||||||||
| /// Performs the set operation on a given data structure. | ||||||||||||||||||||||||||||||||||||||
| /// </summary> | ||||||||||||||||||||||||||||||||||||||
| private static bool IsProperSubsetOf(IEnumerable<T> other, MutationInput origin) | ||||||||||||||||||||||||||||||||||||||
| private static bool IsProperSubsetOfFastPath(ICollection<T> other, MutationInput origin) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| Requires.NotNull(other, nameof(other)); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| if (origin.Root.IsEmpty) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| return other.Any(); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // To determine whether everything we have is also in another sequence, | ||||||||||||||||||||||||||||||||||||||
| // we enumerate the sequence and "tag" whether it's in this collection, | ||||||||||||||||||||||||||||||||||||||
| // then consider whether every element in this collection was tagged. | ||||||||||||||||||||||||||||||||||||||
| // Since this collection is immutable we cannot directly tag. So instead | ||||||||||||||||||||||||||||||||||||||
| // we simply count how many "hits" we have and ensure it's equal to the | ||||||||||||||||||||||||||||||||||||||
| // size of this collection. Of course for this to work we need to ensure | ||||||||||||||||||||||||||||||||||||||
| // the uniqueness of items in the given sequence, so we create a set based | ||||||||||||||||||||||||||||||||||||||
| // on the sequence first. | ||||||||||||||||||||||||||||||||||||||
| var otherSet = new HashSet<T>(other, origin.EqualityComparer); | ||||||||||||||||||||||||||||||||||||||
| if (origin.Count >= otherSet.Count) | ||||||||||||||||||||||||||||||||||||||
| if (other.Count <= origin.Count) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| int matches = 0; | ||||||||||||||||||||||||||||||||||||||
| bool extraFound = false; | ||||||||||||||||||||||||||||||||||||||
| foreach (T item in otherSet) | ||||||||||||||||||||||||||||||||||||||
| if (other is ImmutableHashSet<T> otherAsImmutableHashSet) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| if (Contains(item, origin)) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| matches++; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||||||
| if (otherAsImmutableHashSet.KeyComparer == origin.EqualityComparer) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| extraFound = true; | ||||||||||||||||||||||||||||||||||||||
| using (var e = new ImmutableHashSet<T>.Enumerator(origin.Root)) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| while (e.MoveNext()) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| if (!otherAsImmutableHashSet.Contains(e.Current)) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+893
to
+902
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Any reason why all of the loops are manually deconstructed instead of using |
||||||||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| if (matches == origin.Count && extraFound) | ||||||||||||||||||||||||||||||||||||||
| else if (other is HashSet<T> otherAsHashSet) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| if (otherAsHashSet.Comparer == origin.EqualityComparer) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| using (var e = new ImmutableHashSet<T>.Enumerator(origin.Root)) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| while (e.MoveNext()) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| if (!otherAsHashSet.Contains(e.Current)) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||
| return IsProperSubsetOf(other, origin); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| /// <summary> | ||||||||||||||||||||||||||||||||||||||
| /// Performs the set operation on a given data structure. | ||||||||||||||||||||||||||||||||||||||
| /// </summary> | ||||||||||||||||||||||||||||||||||||||
| private static bool IsProperSubsetOf(IEnumerable<T> other, MutationInput origin) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| Requires.NotNull(other, nameof(other)); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| var otherSet = new HashSet<T>(other, origin.EqualityComparer); | ||||||||||||||||||||||||||||||||||||||
| if (otherSet.Count <= origin.Count) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| using (var e = new ImmutableHashSet<T>.Enumerator(origin.Root)) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| while (e.MoveNext()) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| if (!otherSet.Contains(e.Current)) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| /// <summary> | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it common for types to implement
ICollectionbut notICollection<T>if they're already implementingIEnumerable<T>?