From b7769c40b5c3e15ecc558ac74c2ad6575e7fd842 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Sun, 21 Apr 2024 07:37:29 -0400 Subject: [PATCH 1/2] Avoid unnecessary lazy init in StructuralComparisons Move the fields to the target types and make them readonly instead of manually lazy. --- .../src/System/Collections/StructuralComparisons.cs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/libraries/System.Collections/src/System/Collections/StructuralComparisons.cs b/src/libraries/System.Collections/src/System/Collections/StructuralComparisons.cs index 988cc218e9fde..282aebd4933a8 100644 --- a/src/libraries/System.Collections/src/System/Collections/StructuralComparisons.cs +++ b/src/libraries/System.Collections/src/System/Collections/StructuralComparisons.cs @@ -7,16 +7,15 @@ namespace System.Collections { public static class StructuralComparisons { - private static volatile IComparer? s_StructuralComparer; - private static volatile IEqualityComparer? s_StructuralEqualityComparer; + public static IComparer StructuralComparer => System.Collections.StructuralComparer.s_instance - public static IComparer StructuralComparer => s_StructuralComparer ??= new StructuralComparer(); - - public static IEqualityComparer StructuralEqualityComparer => s_StructuralEqualityComparer ??= new StructuralEqualityComparer(); + public static IEqualityComparer StructuralEqualityComparer => System.Collections.StructuralEqualityComparer.s_instance; } internal sealed class StructuralEqualityComparer : IEqualityComparer { + internal static readonly StructuralEqualityComparer s_instance = new(); + public new bool Equals(object? x, object? y) { if (x != null) @@ -58,6 +57,8 @@ public int GetHashCode(object obj) internal sealed class StructuralComparer : IComparer { + internal static readonly StructuralComparer s_instance = new(); + public int Compare(object? x, object? y) { if (x == null) return y == null ? 0 : -1; From 0a3f8c05b3cf2a1a79455979ab77dfffd516efc6 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Sun, 21 Apr 2024 08:36:16 -0400 Subject: [PATCH 2/2] Update StructuralComparisons.cs --- .../src/System/Collections/StructuralComparisons.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Collections/src/System/Collections/StructuralComparisons.cs b/src/libraries/System.Collections/src/System/Collections/StructuralComparisons.cs index 282aebd4933a8..d4eb6576b3aed 100644 --- a/src/libraries/System.Collections/src/System/Collections/StructuralComparisons.cs +++ b/src/libraries/System.Collections/src/System/Collections/StructuralComparisons.cs @@ -7,7 +7,7 @@ namespace System.Collections { public static class StructuralComparisons { - public static IComparer StructuralComparer => System.Collections.StructuralComparer.s_instance + public static IComparer StructuralComparer => System.Collections.StructuralComparer.s_instance; public static IEqualityComparer StructuralEqualityComparer => System.Collections.StructuralEqualityComparer.s_instance; }