Improve ComparerHelper.CreateDefaultEqualityComparer - #134321
Merged
tannergooding merged 3 commits intoSep 21, 2026
Merged
tannergooding merged 3 commits into
tannergooding merged 3 commits into
Conversation
|
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. |
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-memory |
tannergooding
approved these changes
Sep 21, 2026
tannergooding
enabled auto-merge (squash)
September 21, 2026 13:56
tannergooding
pushed a commit
that referenced
this pull request
Sep 22, 2026
# 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](#126905). This is a follow-up to [#134321](#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 | ``` ```cs [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); } } ```
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR changes the nullable underlying type retrieval from
GetGenericArguments()toGetNullableUnderlyingType()inComparerHelpers.CreateDefaultEqualityComparer.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.Benchmark