Skip to content

[API Proposal]: Add ImmutableArray.Contains overload that accepts an IEqualityComparer<T> #66758

Closed

Description

@ChrML

Background and motivation

I recently had a significant inner-loop performance regression when I added an optional IEqualityComparer<T> to my API. I found out that the reason was that I added the optional comparer argument to the Contains invocation on a ImmutableArray<T>.

Apparently there is no such overload, so it the call was directed from:

public readonly struct ImmutableArray<T>
{
    // ...
    public bool Contains(T item)
    // ...
}

And fell back to the much slower Linq extension:

public static class Enumerable
{`
    // ...
    public static bool Contains<TSource>(this IEnumerable<TSource> source, TSource value, IEqualityComparer<TSource>? comparer);
    // ...
}

This caused a significant amount of boxing to IEnumerable<T> each time Contains was called. Most methods in the API at the moment contain overloads that accept an optional IEqualityComparer<T>?, so it was quite surprising that this API did not.

See: https://docs.microsoft.com/en-us/dotnet/api/system.collections.immutable.immutablearray-1.contains?view=net-6.0

API Proposal

namespace System.Linq;

public static partial class ImmutableArrayExtensions
{
    public static bool Contains<T>(this ImmutableArray<T> immutableArray, T item, IEqualityComparer<T>? comparer)
    {
        var self = immutableArray;
        return self.IndexOf(item, 0, self.Length, equalityComparer) >= 0;
    }
}

API Usage

ImmutableArray<string> items = ImmutableArray.Create("abc", "DEF");
Assert.True(items.Contains("Abc", StringComparer.InvariantCultureIgnoreCase));
Assert.True(items.Contains("Def", StringComparer.InvariantCultureIgnoreCase));

Alternative Designs

No response

Risks

This has a really low risk of breaking something. It should increase the overall performance when Contains is used where the programmer was not aware of this issue.

Activity

  1. added
    api-suggestion
    on Mar 17, 2022
  2. ghost added
    untriaged
    on Mar 17, 2022
  3. changed the title [-][API Proposal]: Add ImmutableArray.Contains overload with IEqualityComparer<T>[/-] [+][API Proposal]: Add ImmutableArray.Contains overload that accepts an IEqualityComparer<T>[/+] on Mar 17, 2022
  4. eiriktsarpalis commented on Mar 21, 2022

    @eiriktsarpalis
    Member

    Given that it is fairly common for concrete collections to fall back to slower LINQ methods, I'm not necessarily convinced that it's sufficient justification to add another overload in ImmutableArray<T>. There's codegen overhead to adding more methods to generic types, and as you're pointing out it's fairly trivial to express this overload as an extension method.

  5. ChrML commented on Mar 21, 2022

    @ChrML
    Author

    Given that it is fairly common for concrete collections to fall back to slower LINQ methods, I'm not necessarily convinced that it's sufficient justification to add another overload in ImmutableArray<T>. There's codegen overhead to adding more methods to generic types, and as you're pointing out it's fairly trivial to express this overload as an extension method.

    Yes, the required functionality is already present in ImmutableArray.IndexOf, so this can easily be expressed as an extension method.

    However that argument would also apply to the single-argument Contains method already present.

  6. Joe4evr commented on Mar 21, 2022

    @Joe4evr
    Contributor

    Is it not an option to add this overload to the set of LINQ extensions specialized for ImmutableArray<T>?

  7. ChrML commented on Mar 21, 2022

    @ChrML
    Author

    Is it not an option to add this overload to the set of LINQ extensions specialized for ImmutableArray<T>?

    I think that's the best option.

  8. eiriktsarpalis commented on Mar 22, 2022

    @eiriktsarpalis
    Member

    SGTM, given there's precedent. @ChrML could you update your API proposal to use that namespace?

  9. added this to the Future milestone on Mar 22, 2022
  10. added and removed
    untriaged
    on Mar 22, 2022
  11. Joe4evr commented on Mar 24, 2022

    @Joe4evr
    Contributor

    If OP isn't going to update, then I'll place the revised proposal real quick:

    namespace System.Linq
    {
        public static partial class ImmutableArrayExtensions
        {
            public static bool Contains<T>(this ImmutableArray<T> immutableArray, T value, IEqualityComparer<T>? comparer);
        }
    }
  12. ChrML commented on Mar 24, 2022

    @ChrML
    Author

    I have updated the proposal to what we have discussed here. Sorry for that it took so long.

  13. added
    api-ready-for-review
    and removed
    api-suggestion
    on Mar 24, 2022
  14. Joe4evr commented on Mar 5, 2023

    @Joe4evr
    Contributor

    Question for whenever this comes to review: I recall that some time ago the review team made a blanket policy that System.Linq.Queryable should be kept in sync with (most) methods in System.Linq.Enumerable. Does it make sense to extend that policy to keep ImmutableArrayExtensions in sync as well?

  15. bartonjs commented on May 11, 2023

    @bartonjs
    Member

    Looks good as proposed, except we changed from an extension method to an instance method (as this is an overload of the existing Contains(T)). This seems generally applicable to any struct that is IEnumerable that provides a Contains(T) without a Contains(T, IEqualityComparer<T>)... but we couldn't find a second one during the meeting.

    namespace System.Collections.Immutable;
    
    public static partial struct ImmutableArray<T>
    {
        public bool Contains(T item, IEqualityComparer<T>? comparer);
    }
  16. added
    api-approved
    and removed
    api-ready-for-review
    on May 11, 2023
  17. added
    help wanted
    on May 12, 2023
  18. removed their assignment
    on May 12, 2023
  19. ghost added
    in-pr
    on May 13, 2023
  20. ghost removed
    in-pr
    on May 14, 2023
  21. ghost locked as resolved and limited conversation to collaborators on Jun 13, 2023
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

    api-approvedarea-System.Linqhelp wanted

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions