Skip to content

Optimize comparison methods in immutable collections #127279

Open

Description

@aw0lid

Description

While working on PR #126309 for Issue #90986, I noticed that the current implementation of several comparison methods in the System.Collections.Immutable library always creates a new intermediate collection (like HashSet<T> or SortedSet<T>).

The Problem

This intermediate allocation is often unnecessary. For example, if both collections are of the same type (e.g., both are ImmutableSortedSet<T>) and share the same comparer, we can perform the comparison directly without allocating any new memory on the heap.

The Solution

I propose adding a "Fast Path" to detect these cases and perform the check directly. This will:

  • Eliminate unnecessary allocations when types/comparers are compatible.
  • Improve performance by moving from building a fully new collection to a direct linear comparison of existing elements wherever applicable.

Targeted Methods & Progress

I am taking ownership of this optimization and will be submitting focused PRs for each:

Activity

  1. added
    tenet-performance
    on Apr 22, 2026
  2. added
    untriaged
    on Apr 22, 2026
  3. dotnet-policy-service commented on Apr 22, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-collections
    See info in area-owners.md if you want to be subscribed.

  4. removed
    untriaged
    on Aug 17, 2026
  5. added this to the 12.0.0 milestone on Aug 17, 2026
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

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions