Skip to content

Commit 3426e57

Browse files
committed
Avoid unnecessary lazy init in StructuralComparisons
The only benefit of having these be lazy in this manner is avoiding one of the two objects being constructed. We can instead just combine them into a singleton that's initialized in a static readonly.
1 parent bc0380f commit 3426e57

File tree

1 file changed

+42
-46
lines changed

1 file changed

+42
-46
lines changed

src/libraries/System.Collections/src/System/Collections/StructuralComparisons.cs

+42-46
Original file line numberDiff line numberDiff line change
@@ -7,70 +7,66 @@ namespace System.Collections
77
{
88
public static class StructuralComparisons
99
{
10-
private static volatile IComparer? s_StructuralComparer;
11-
private static volatile IEqualityComparer? s_StructuralEqualityComparer;
10+
private static readonly Comparer s_comparer = new();
1211

13-
public static IComparer StructuralComparer => s_StructuralComparer ??= new StructuralComparer();
12+
public static IComparer StructuralComparer => s_comparer;
1413

15-
public static IEqualityComparer StructuralEqualityComparer => s_StructuralEqualityComparer ??= new StructuralEqualityComparer();
16-
}
14+
public static IEqualityComparer StructuralEqualityComparer => s_comparer;
1715

18-
internal sealed class StructuralEqualityComparer : IEqualityComparer
19-
{
20-
public new bool Equals(object? x, object? y)
16+
private sealed class Comparer : IEqualityComparer, IComparer
2117
{
22-
if (x != null)
18+
public new bool Equals(object? x, object? y)
2319
{
24-
IStructuralEquatable? seObj = x as IStructuralEquatable;
25-
26-
if (seObj != null)
20+
if (x != null)
2721
{
28-
return seObj.Equals(y, this);
29-
}
22+
IStructuralEquatable? seObj = x as IStructuralEquatable;
3023

31-
if (y != null)
32-
{
33-
return x.Equals(y);
34-
}
35-
else
36-
{
37-
return false;
24+
if (seObj != null)
25+
{
26+
return seObj.Equals(y, this);
27+
}
28+
29+
if (y != null)
30+
{
31+
return x.Equals(y);
32+
}
33+
else
34+
{
35+
return false;
36+
}
3837
}
38+
if (y != null) return false;
39+
return true;
3940
}
40-
if (y != null) return false;
41-
return true;
42-
}
4341

44-
public int GetHashCode(object obj)
45-
{
46-
if (obj == null) return 0;
42+
public int GetHashCode(object obj)
43+
{
44+
if (obj == null) return 0;
4745

48-
IStructuralEquatable? seObj = obj as IStructuralEquatable;
46+
IStructuralEquatable? seObj = obj as IStructuralEquatable;
4947

50-
if (seObj != null)
51-
{
52-
return seObj.GetHashCode(this);
48+
if (seObj != null)
49+
{
50+
return seObj.GetHashCode(this);
51+
}
52+
53+
return obj.GetHashCode();
5354
}
5455

55-
return obj.GetHashCode();
56-
}
57-
}
56+
public int Compare(object? x, object? y)
57+
{
58+
if (x == null) return y == null ? 0 : -1;
59+
if (y == null) return 1;
5860

59-
internal sealed class StructuralComparer : IComparer
60-
{
61-
public int Compare(object? x, object? y)
62-
{
63-
if (x == null) return y == null ? 0 : -1;
64-
if (y == null) return 1;
61+
IStructuralComparable? scX = x as IStructuralComparable;
6562

66-
IStructuralComparable? scX = x as IStructuralComparable;
63+
if (scX != null)
64+
{
65+
return scX.CompareTo(y, this);
66+
}
6767

68-
if (scX != null)
69-
{
70-
return scX.CompareTo(y, this);
68+
return Comparer<object>.Default.Compare(x, y);
7169
}
72-
73-
return Comparer<object>.Default.Compare(x, y);
7470
}
7571
}
7672
}

0 commit comments

Comments
 (0)