Skip to content

Improve ComparerHelper.CreateDefaultComparer - #134417

Merged
tannergooding merged 1 commit into
dotnet:mainfrom
hanachiru:ComparerHelpers.CreateDefaultComparer
Sep 22, 2026
Merged

tannergooding merged 1 commit into
dotnet:mainfrom
hanachiru:ComparerHelpers.CreateDefaultComparer

Conversation

@hanachiru

Copy link
Copy Markdown
Contributor

Summary

This PR changes the nullable underlying type retrieval from GetGenericArguments() to GetNullableUnderlyingType() in ComparerHelpers.CreateDefaultComparer.

This change avoids allocating a generic argument array when creating an equality comparer for a closed Nullable<T> and uses the API introduced in #126905.

This is a follow-up to #134321, which applied the same change to the sibling method ComparerHelpers.CreateDefaultEqualityComparer. Both methods in this file now use the same idiom.

Benchmark

| Method                | Job        | Toolchain | type                                | Mean     | Error   | StdDev  | Median   | Min      | Max      | Ratio | Gen0   | Allocated | Alloc Ratio |
|---------------------- |----------- |---------- |------------------------------------ |---------:|--------:|--------:|---------:|---------:|---------:|------:|-------:|----------:|------------:|
| CreateDefaultComparer | Job-HCBSPO | /main     | System.Int32                        | 127.6 ns | 0.98 ns | 0.92 ns | 127.7 ns | 126.4 ns | 130.4 ns |  1.00 | 0.0103 |      88 B |        1.00 |
| CreateDefaultComparer | Job-QOJBTO | /pr       | System.Int32                        | 126.6 ns | 1.44 ns | 1.35 ns | 126.3 ns | 126.0 ns | 131.4 ns |  0.99 | 0.0102 |      88 B |        1.00 |
|                       |            |           |                                     |          |         |         |          |          |          |       |        |           |             |
| CreateDefaultComparer | Job-HCBSPO | /main     | System.Int64                        | 129.1 ns | 1.30 ns | 1.21 ns | 128.7 ns | 128.4 ns | 133.3 ns |  1.00 | 0.0104 |      88 B |        1.00 |
| CreateDefaultComparer | Job-QOJBTO | /pr       | System.Int64                        | 127.6 ns | 1.46 ns | 1.37 ns | 127.2 ns | 126.4 ns | 132.1 ns |  0.99 | 0.0101 |      88 B |        1.00 |
|                       |            |           |                                     |          |         |         |          |          |          |       |        |           |             |
| CreateDefaultComparer | Job-HCBSPO | /main     | System.Nullable`1[System.DateTime]  | 154.3 ns | 1.79 ns | 1.68 ns | 153.9 ns | 152.3 ns | 159.1 ns |  1.00 | 0.0138 |     120 B |        1.00 |
| CreateDefaultComparer | Job-QOJBTO | /pr       | System.Nullable`1[System.DateTime]  | 127.4 ns | 0.43 ns | 0.40 ns | 127.4 ns | 126.8 ns | 128.2 ns |  0.83 | 0.0102 |      88 B |        0.73 |
|                       |            |           |                                     |          |         |         |          |          |          |       |        |           |             |
| CreateDefaultComparer | Job-HCBSPO | /main     | System.Nullable`1[System.DayOfWeek] | 155.0 ns | 1.48 ns | 1.38 ns | 154.7 ns | 154.1 ns | 159.9 ns |  1.00 | 0.0143 |     120 B |        1.00 |
| CreateDefaultComparer | Job-QOJBTO | /pr       | System.Nullable`1[System.DayOfWeek] | 127.8 ns | 0.38 ns | 0.35 ns | 127.9 ns | 127.3 ns | 128.3 ns |  0.82 | 0.0102 |      88 B |        0.73 |
|                       |            |           |                                     |          |         |         |          |          |          |       |        |           |             |
| CreateDefaultComparer | Job-HCBSPO | /main     | System.Nullable`1[System.Int32]     | 154.9 ns | 1.49 ns | 1.40 ns | 154.4 ns | 153.8 ns | 159.6 ns |  1.00 | 0.0141 |     120 B |        1.00 |
| CreateDefaultComparer | Job-QOJBTO | /pr       | System.Nullable`1[System.Int32]     | 126.9 ns | 1.14 ns | 1.07 ns | 126.6 ns | 126.3 ns | 130.6 ns |  0.82 | 0.0102 |      88 B |        0.73 |
|                       |            |           |                                     |          |         |         |          |          |          |       |        |           |             |
| CreateDefaultComparer | Job-HCBSPO | /main     | System.Nullable`1[System.Int64]     | 155.5 ns | 1.72 ns | 1.61 ns | 155.1 ns | 154.7 ns | 161.3 ns |  1.00 | 0.0142 |     120 B |        1.00 |
| CreateDefaultComparer | Job-QOJBTO | /pr       | System.Nullable`1[System.Int64]     | 127.7 ns | 1.55 ns | 1.45 ns | 127.4 ns | 126.8 ns | 132.9 ns |  0.82 | 0.0101 |      88 B |        0.73 |
|                       |            |           |                                     |          |         |         |          |          |          |       |        |           |             |
| CreateDefaultComparer | Job-HCBSPO | /main     | System.String                       | 128.3 ns | 1.54 ns | 1.44 ns | 127.9 ns | 127.5 ns | 133.4 ns |  1.00 | 0.0102 |      88 B |        1.00 |
| CreateDefaultComparer | Job-QOJBTO | /pr       | System.String                       | 128.1 ns | 1.33 ns | 1.25 ns | 127.8 ns | 127.4 ns | 132.6 ns |  1.00 | 0.0102 |      88 B |        1.00 |
[BenchmarkCategory(Categories.Libraries)]
public class Perf_ComparerHelpers
{
    private static readonly Func<Type, object> s_createDefaultComparer =
        (Func<Type, object>)typeof(Comparer<>).Assembly
            .GetType("System.Collections.Generic.ComparerHelpers", throwOnError: true)!
            .GetMethod(
                "CreateDefaultComparer",
                BindingFlags.NonPublic | BindingFlags.Static)!
            .CreateDelegate(typeof(Func<Type, object>));

    public static IEnumerable<Type> Arguments()
    { 
        yield return typeof(int);
        yield return typeof(long);
        yield return typeof(string);
        yield return typeof(int?);
        yield return typeof(long?);
        yield return typeof(DateTime?);
        yield return typeof(DayOfWeek?);
    }

    [Benchmark]
    [ArgumentsSource(nameof(Arguments))]
    [MemoryRandomization]
    public object CreateDefaultComparer(Type type)
    {
        return s_createDefaultComparer(type);
    }
}

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Sep 22, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

@jkotas jkotas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@tannergooding
tannergooding merged commit e419487 into dotnet:main Sep 22, 2026
121 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Sep 23, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 23, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-System.Memory community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants