You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ImmutableHashSet<T>.SetEquals always creates a new HashSet<T> #90986
The following HashSet<T> calculation could be avoided by using other.WithComparer(origin.KeyComparer) in the specific case where other is already an ImmutableHashSet<T>. It might also be avoidable in the case where other is already a HashSet<T> with an appropriate comparer.
Note that in addition to the overhead of performing these checks at runtime, there may be overhead added by the fact that ImmutableHashSet<T> has a slower enumerator than HashSet<T>, and the newly allocated set is immediately enumerated.
The issue is still not fixed. dotnet/roslyn#69673 only implements a local workaround until it is correctly fixed here in the runtime. @MadL1me Are you still willing to work on this?
@sharwell : I was taking a look at this issue and had a doubt. Why are we saying that other.WithComparer(origin.KeyComparer) should be used instead of new HashSet<T>, as even the WithComparer function creates a new ImmutableHashSet as shown here:
Activity
Avoid intermediate HashSet<T> calculation in SetEquals
MadL1me commentedon Nov 28, 2023
@eiriktsarpalis can I work on this? Looks good as a starting point for runtime contributions
eiriktsarpalis commentedon Nov 29, 2023
Sure, feel free to submit a PR.
MadL1me commentedon Dec 6, 2023
@sharwell aren't you solved the problem with dotnet/roslyn#69673 PR?
manandre commentedon Mar 21, 2024
The issue is still not fixed.
dotnet/roslyn#69673 only implements a local workaround until it is correctly fixed here in the runtime.
@MadL1me Are you still willing to work on this?
MadL1me commentedon Mar 22, 2024
@manandre yes, I am, thanks for the info. I'll dig into it soon
GMPrakhar commentedon May 13, 2024
@sharwell : I was taking a look at this issue and had a doubt. Why are we saying that
other.WithComparer(origin.KeyComparer)should be used instead ofnew HashSet<T>, as even theWithComparerfunction creates a new ImmutableHashSet as shown here:sharwell commentedon May 13, 2024
@GMPrakhar frequently the sets being compared use the same comparer, which would go through the fast path and not create a new collection.
GMPrakhar commentedon May 14, 2024
@sharwell : I have taken a stab at it, please check if the changes make sense.
Optimize ImmutableHashSet<T>.SetEquals to avoid unnecessary allocatio…
Optimize ImmutableHashSet<T>.SetEquals to avoid unnecessary allocatio…