Skip to content

Add overload for creating Set/Map that includes an IComparer<'T> #1137

@matthewcrews

Description

@matthewcrews

Add overload for creating Set/Map that includes an IComparer<'T>

I propose we add an overload for the creation of Set<'T> and Map<'K, 'V> which takes an IComparer<'T> to override the default comparison behavior of these two collections. Where I believe this would add the most value is in the case of structs that currently get boxed during comparison leading to performance degradation.

The existing way of approaching this problem in F# is to use the built-in comparison functionality in the F# language.

You can see from the benchmarks in this repo. The Struct and CustomStruct versions are allocating more memory than the default Record.

Method Mean Error StdDev Gen 0 Allocated
Record 2.464 us 0.0479 us 0.0774 us 0.4044 3 KB
StructRecord 2.508 us 0.0500 us 0.0748 us 0.7248 6 KB
CustomStructRecord 3.798 us 0.0758 us 0.0811 us 0.8926 7 KB

If this proposal does not address the GC/performance problem then I suggest we discard it since it is the main concern.

Pros and Cons

The advantages of making this adjustment to F# are that we reduce the GC associated with boxing structs unnecessarily.

The disadvantages of making this adjustment to F# are added complexity of the F# Core library and therefore more code to maintain.

Extra information

Estimated cost (XS, S, M, L, XL, XXL): M

Related suggestions: (put links to related suggestions here)

Affidavit (please submit!)

Please tick this by placing a cross in the box:

  • This is not a question (e.g. like one you might ask on stackoverflow) and I have searched stackoverflow for discussions of this issue
  • I have searched both open and closed suggestions on this site and believe this is not a duplicate
  • This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it.

Please tick all that apply:

  • This is not a breaking change to the F# language design
  • I or my company would be willing to help implement and/or test this

For Readers

If you would like to see this issue implemented, please click the 👍 emoji on this issue. These counts are used to generally order the suggestions by engagement.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions