-
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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
Author
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. In the existing implementation, the code iterates over other and queries the Also, That said, I am waiting for my other PR (#126309) to be merged so I can leverage its logic here and further refine this implementation like following this: private static bool IsProperSubsetOf(IEnumerable<T> other, MutationInput origin)
{
Requires.NotNull(other, nameof(other));
if (origin.Root.IsEmpty)
{
return other.Any();
}
if (other is ICollection<T> otherAsICollectionGeneric)
{
// We check for < instead of != because other is not guaranteed to be a set, it could be a collection with duplicates.
if (otherAsICollectionGeneric.Count <= origin.Count)
{
return false;
}
switch (other)
{
case ImmutableHashSet<T> otherAsImmutableHashSet:
if (origin.EqualityComparer.Equals(otherAsImmutableHashSet.KeyComparer))
{
return SetEqualsWithImmutableHashset(otherAsImmutableHashSet, origin);
}
break;
case HashSet<T> otherAsHashset:
if (origin.EqualityComparer.Equals(otherAsHashset.Comparer))
{
return SetEqualsWithHashset(otherAsHashset, origin);
}
break;
}
}
else if (other is ICollection otherAsICollection && otherAsICollection.Count <= origin.Count)
{
return false;
}
var otherSet = new HashSet<T>(other, origin.EqualityComparer);
if (otherSet.Count <= origin.Count)
{
return false;
}
return SetEqualsWithHashset(otherSet, origin);
} |
||||||||||||||||||||||||||||||||||||||
| 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> | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
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>?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.
True, it’s not very common, but these types still exist and are in use. Since we can easily capture the Count property from them to trigger an early exit, I thought: why not include them? It broadens the optimization's reach with very little overhead