Skip to content

ImmutableHashSet<T>.SetEquals always creates a new HashSet<T> #90986

Closed

Description

@sharwell

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.

var otherSet = new HashSet<T>(other, origin.EqualityComparer);

Activity

  1. ghost added
    untriaged
    on Aug 23, 2023
  2. added this to the 9.0.0 milestone on Aug 23, 2023
  3. ghost removed
    untriaged
    on Aug 23, 2023
  4. modified the milestones: 9.0.0, Future on Nov 22, 2023
  5. added
    tenet-performance
    help wanted
    on Nov 22, 2023
  6. MadL1me commented on Nov 28, 2023

    @MadL1me

    @eiriktsarpalis can I work on this? Looks good as a starting point for runtime contributions

  7. eiriktsarpalis commented on Nov 29, 2023

    @eiriktsarpalis
    Member

    Sure, feel free to submit a PR.

  8. MadL1me commented on Dec 6, 2023

    @MadL1me

    @sharwell aren't you solved the problem with dotnet/roslyn#69673 PR?

  9. manandre commented on Mar 21, 2024

    @manandre
    Contributor

    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?

  10. MadL1me commented on Mar 22, 2024

    @MadL1me

    @manandre yes, I am, thanks for the info. I'll dig into it soon

  11. GMPrakhar commented on May 13, 2024

    @GMPrakhar
    Contributor

    @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:

    image

  12. sharwell commented on May 13, 2024

    @sharwell
    ContributorAuthor

    @GMPrakhar frequently the sets being compared use the same comparer, which would go through the fast path and not create a new collection.

  13. GMPrakhar commented on May 14, 2024

    @GMPrakhar
    Contributor

    @sharwell : I have taken a stab at it, please check if the changes make sense.

  14. added
    in-pr
    on Mar 30, 2026
  15. added a commit that references this issue on Apr 30, 2026
    dd17b6d
  16. locked and limited conversation to collaborators on May 31, 2026
  17. added a commit that references this issue on Jul 15, 2026
    5478b52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Collectionshelp wantedin-prtenet-performance

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions