From 583a5367d853e5a15f23b39fae3461aef92af1d4 Mon Sep 17 00:00:00 2001 From: Marek Safar Date: Tue, 17 Nov 2020 13:06:08 +0100 Subject: [PATCH] Don't use interfaces for sharing Tuple implementation methods (#44684) --- .../src/System/Tuple.cs | 260 ++++++++++++++---- 1 file changed, 214 insertions(+), 46 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Tuple.cs b/src/libraries/System.Private.CoreLib/src/System/Tuple.cs index bb523a750e249..d6e87002d5d56 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Tuple.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Tuple.cs @@ -114,10 +114,15 @@ public Tuple(T1 item1) public override bool Equals(object? obj) { - return ((IStructuralEquatable)this).Equals(obj, EqualityComparer.Default); + return Equals(obj, EqualityComparer.Default); } bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer) + { + return Equals(other, comparer); + } + + private bool Equals(object? other, IEqualityComparer comparer) { if (other == null) return false; @@ -131,10 +136,15 @@ bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer) int IComparable.CompareTo(object? obj) { - return ((IStructuralComparable)this).CompareTo(obj, Comparer.Default); + return CompareTo(obj, Comparer.Default); } int IStructuralComparable.CompareTo(object? other, IComparer comparer) + { + return CompareTo(other, comparer); + } + + private int CompareTo(object? other, IComparer comparer) { if (other == null) return 1; @@ -148,26 +158,37 @@ int IStructuralComparable.CompareTo(object? other, IComparer comparer) public override int GetHashCode() { - return ((IStructuralEquatable)this).GetHashCode(EqualityComparer.Default); + return GetHashCode(EqualityComparer.Default); } int IStructuralEquatable.GetHashCode(IEqualityComparer comparer) { - return comparer.GetHashCode(m_Item1!); + return GetHashCode(comparer); } int ITupleInternal.GetHashCode(IEqualityComparer comparer) { - return ((IStructuralEquatable)this).GetHashCode(comparer); + return GetHashCode(comparer); } + + private int GetHashCode(IEqualityComparer comparer) + { + return comparer.GetHashCode(m_Item1!); + } + public override string ToString() { StringBuilder sb = new StringBuilder(); sb.Append('('); - return ((ITupleInternal)this).ToString(sb); + return ToString(sb); } string ITupleInternal.ToString(StringBuilder sb) + { + return ToString(sb); + } + + private string ToString(StringBuilder sb) { sb.Append(m_Item1); sb.Append(')'); @@ -213,10 +234,15 @@ public Tuple(T1 item1, T2 item2) public override bool Equals(object? obj) { - return ((IStructuralEquatable)this).Equals(obj, EqualityComparer.Default); + return Equals(obj, EqualityComparer.Default); } bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer) + { + return Equals(other, comparer); + } + + private bool Equals(object? other, IEqualityComparer comparer) { if (other == null) return false; @@ -230,10 +256,15 @@ bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer) int IComparable.CompareTo(object? obj) { - return ((IStructuralComparable)this).CompareTo(obj, Comparer.Default); + return CompareTo(obj, Comparer.Default); } int IStructuralComparable.CompareTo(object? other, IComparer comparer) + { + return CompareTo(other, comparer); + } + + private int CompareTo(object? other, IComparer comparer) { if (other == null) return 1; @@ -251,26 +282,37 @@ int IStructuralComparable.CompareTo(object? other, IComparer comparer) public override int GetHashCode() { - return ((IStructuralEquatable)this).GetHashCode(EqualityComparer.Default); + return GetHashCode(EqualityComparer.Default); } int IStructuralEquatable.GetHashCode(IEqualityComparer comparer) { - return Tuple.CombineHashCodes(comparer.GetHashCode(m_Item1!), comparer.GetHashCode(m_Item2!)); + return GetHashCode(comparer); } int ITupleInternal.GetHashCode(IEqualityComparer comparer) { - return ((IStructuralEquatable)this).GetHashCode(comparer); + return GetHashCode(comparer); } + + private int GetHashCode(IEqualityComparer comparer) + { + return Tuple.CombineHashCodes(comparer.GetHashCode(m_Item1!), comparer.GetHashCode(m_Item2!)); + } + public override string ToString() { StringBuilder sb = new StringBuilder(); sb.Append('('); - return ((ITupleInternal)this).ToString(sb); + return ToString(sb); } string ITupleInternal.ToString(StringBuilder sb) + { + return ToString(sb); + } + + private string ToString(StringBuilder sb) { sb.Append(m_Item1); sb.Append(", "); @@ -317,10 +359,15 @@ public Tuple(T1 item1, T2 item2, T3 item3) public override bool Equals(object? obj) { - return ((IStructuralEquatable)this).Equals(obj, EqualityComparer.Default); + return Equals(obj, EqualityComparer.Default); } bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer) + { + return Equals(other, comparer); + } + + private bool Equals(object? other, IEqualityComparer comparer) { if (other == null) return false; @@ -334,10 +381,15 @@ bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer) int IComparable.CompareTo(object? obj) { - return ((IStructuralComparable)this).CompareTo(obj, Comparer.Default); + return CompareTo(obj, Comparer.Default); } int IStructuralComparable.CompareTo(object? other, IComparer comparer) + { + return CompareTo(other, comparer); + } + + private int CompareTo(object? other, IComparer comparer) { if (other == null) return 1; @@ -359,26 +411,37 @@ int IStructuralComparable.CompareTo(object? other, IComparer comparer) public override int GetHashCode() { - return ((IStructuralEquatable)this).GetHashCode(EqualityComparer.Default); + return GetHashCode(EqualityComparer.Default); } int IStructuralEquatable.GetHashCode(IEqualityComparer comparer) { - return Tuple.CombineHashCodes(comparer.GetHashCode(m_Item1!), comparer.GetHashCode(m_Item2!), comparer.GetHashCode(m_Item3!)); + return GetHashCode(comparer); } int ITupleInternal.GetHashCode(IEqualityComparer comparer) { - return ((IStructuralEquatable)this).GetHashCode(comparer); + return GetHashCode(comparer); } + + private int GetHashCode(IEqualityComparer comparer) + { + return Tuple.CombineHashCodes(comparer.GetHashCode(m_Item1!), comparer.GetHashCode(m_Item2!), comparer.GetHashCode(m_Item3!)); + } + public override string ToString() { StringBuilder sb = new StringBuilder(); sb.Append('('); - return ((ITupleInternal)this).ToString(sb); + return ToString(sb); } string ITupleInternal.ToString(StringBuilder sb) + { + return ToString(sb); + } + + private string ToString(StringBuilder sb) { sb.Append(m_Item1); sb.Append(", "); @@ -431,10 +494,15 @@ public Tuple(T1 item1, T2 item2, T3 item3, T4 item4) public override bool Equals(object? obj) { - return ((IStructuralEquatable)this).Equals(obj, EqualityComparer.Default); + return Equals(obj, EqualityComparer.Default); } bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer) + { + return Equals(other, comparer); + } + + private bool Equals(object? other, IEqualityComparer comparer) { if (other == null) return false; @@ -448,10 +516,15 @@ bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer) int IComparable.CompareTo(object? obj) { - return ((IStructuralComparable)this).CompareTo(obj, Comparer.Default); + return CompareTo(obj, Comparer.Default); } int IStructuralComparable.CompareTo(object? other, IComparer comparer) + { + return CompareTo(other, comparer); + } + + private int CompareTo(object? other, IComparer comparer) { if (other == null) return 1; @@ -477,26 +550,37 @@ int IStructuralComparable.CompareTo(object? other, IComparer comparer) public override int GetHashCode() { - return ((IStructuralEquatable)this).GetHashCode(EqualityComparer.Default); + return GetHashCode(EqualityComparer.Default); } int IStructuralEquatable.GetHashCode(IEqualityComparer comparer) { - return Tuple.CombineHashCodes(comparer.GetHashCode(m_Item1!), comparer.GetHashCode(m_Item2!), comparer.GetHashCode(m_Item3!), comparer.GetHashCode(m_Item4!)); + return GetHashCode(comparer); } int ITupleInternal.GetHashCode(IEqualityComparer comparer) { - return ((IStructuralEquatable)this).GetHashCode(comparer); + return GetHashCode(comparer); } + + private int GetHashCode(IEqualityComparer comparer) + { + return Tuple.CombineHashCodes(comparer.GetHashCode(m_Item1!), comparer.GetHashCode(m_Item2!), comparer.GetHashCode(m_Item3!), comparer.GetHashCode(m_Item4!)); + } + public override string ToString() { StringBuilder sb = new StringBuilder(); sb.Append('('); - return ((ITupleInternal)this).ToString(sb); + return ToString(sb); } string ITupleInternal.ToString(StringBuilder sb) + { + return ToString(sb); + } + + private string ToString(StringBuilder sb) { sb.Append(m_Item1); sb.Append(", "); @@ -555,10 +639,15 @@ public Tuple(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5) public override bool Equals(object? obj) { - return ((IStructuralEquatable)this).Equals(obj, EqualityComparer.Default); + return Equals(obj, EqualityComparer.Default); } bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer) + { + return Equals(other, comparer); + } + + private bool Equals(object? other, IEqualityComparer comparer) { if (other == null) return false; @@ -572,10 +661,15 @@ bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer) int IComparable.CompareTo(object? obj) { - return ((IStructuralComparable)this).CompareTo(obj, Comparer.Default); + return CompareTo(obj, Comparer.Default); } int IStructuralComparable.CompareTo(object? other, IComparer comparer) + { + return CompareTo(other, comparer); + } + + private int CompareTo(object? other, IComparer comparer) { if (other == null) return 1; @@ -605,26 +699,37 @@ int IStructuralComparable.CompareTo(object? other, IComparer comparer) public override int GetHashCode() { - return ((IStructuralEquatable)this).GetHashCode(EqualityComparer.Default); + return GetHashCode(EqualityComparer.Default); } int IStructuralEquatable.GetHashCode(IEqualityComparer comparer) { - return Tuple.CombineHashCodes(comparer.GetHashCode(m_Item1!), comparer.GetHashCode(m_Item2!), comparer.GetHashCode(m_Item3!), comparer.GetHashCode(m_Item4!), comparer.GetHashCode(m_Item5!)); + return GetHashCode(comparer); } int ITupleInternal.GetHashCode(IEqualityComparer comparer) { - return ((IStructuralEquatable)this).GetHashCode(comparer); + return GetHashCode(comparer); + } + + private int GetHashCode(IEqualityComparer comparer) + { + return Tuple.CombineHashCodes(comparer.GetHashCode(m_Item1!), comparer.GetHashCode(m_Item2!), comparer.GetHashCode(m_Item3!), comparer.GetHashCode(m_Item4!), comparer.GetHashCode(m_Item5!)); } + public override string ToString() { StringBuilder sb = new StringBuilder(); sb.Append('('); - return ((ITupleInternal)this).ToString(sb); + return ToString(sb); } string ITupleInternal.ToString(StringBuilder sb) + { + return ToString(sb); + } + + private string ToString(StringBuilder sb) { sb.Append(m_Item1); sb.Append(", "); @@ -689,10 +794,15 @@ public Tuple(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6) public override bool Equals(object? obj) { - return ((IStructuralEquatable)this).Equals(obj, EqualityComparer.Default); + return Equals(obj, EqualityComparer.Default); } bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer) + { + return Equals(other, comparer); + } + + private bool Equals(object? other, IEqualityComparer comparer) { if (other == null) return false; @@ -706,10 +816,15 @@ bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer) int IComparable.CompareTo(object? obj) { - return ((IStructuralComparable)this).CompareTo(obj, Comparer.Default); + return CompareTo(obj, Comparer.Default); } int IStructuralComparable.CompareTo(object? other, IComparer comparer) + { + return CompareTo(other, comparer); + } + + private int CompareTo(object? other, IComparer comparer) { if (other == null) return 1; @@ -743,26 +858,37 @@ int IStructuralComparable.CompareTo(object? other, IComparer comparer) public override int GetHashCode() { - return ((IStructuralEquatable)this).GetHashCode(EqualityComparer.Default); + return GetHashCode(EqualityComparer.Default); } int IStructuralEquatable.GetHashCode(IEqualityComparer comparer) { - return Tuple.CombineHashCodes(comparer.GetHashCode(m_Item1!), comparer.GetHashCode(m_Item2!), comparer.GetHashCode(m_Item3!), comparer.GetHashCode(m_Item4!), comparer.GetHashCode(m_Item5!), comparer.GetHashCode(m_Item6!)); + return GetHashCode(comparer); } int ITupleInternal.GetHashCode(IEqualityComparer comparer) { - return ((IStructuralEquatable)this).GetHashCode(comparer); + return GetHashCode(comparer); } + + private int GetHashCode(IEqualityComparer comparer) + { + return Tuple.CombineHashCodes(comparer.GetHashCode(m_Item1!), comparer.GetHashCode(m_Item2!), comparer.GetHashCode(m_Item3!), comparer.GetHashCode(m_Item4!), comparer.GetHashCode(m_Item5!), comparer.GetHashCode(m_Item6!)); + } + public override string ToString() { StringBuilder sb = new StringBuilder(); sb.Append('('); - return ((ITupleInternal)this).ToString(sb); + return ToString(sb); } string ITupleInternal.ToString(StringBuilder sb) + { + return ToString(sb); + } + + private string ToString(StringBuilder sb) { sb.Append(m_Item1); sb.Append(", "); @@ -833,10 +959,15 @@ public Tuple(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 item public override bool Equals(object? obj) { - return ((IStructuralEquatable)this).Equals(obj, EqualityComparer.Default); + return Equals(obj, EqualityComparer.Default); } bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer) + { + return Equals(other, comparer); + } + + private bool Equals(object? other, IEqualityComparer comparer) { if (other == null) return false; @@ -854,6 +985,11 @@ int IComparable.CompareTo(object? obj) } int IStructuralComparable.CompareTo(object? other, IComparer comparer) + { + return CompareTo(other, comparer); + } + + private int CompareTo(object? other, IComparer comparer) { if (other == null) return 1; @@ -891,26 +1027,37 @@ int IStructuralComparable.CompareTo(object? other, IComparer comparer) public override int GetHashCode() { - return ((IStructuralEquatable)this).GetHashCode(EqualityComparer.Default); + return GetHashCode(EqualityComparer.Default); } int IStructuralEquatable.GetHashCode(IEqualityComparer comparer) { - return Tuple.CombineHashCodes(comparer.GetHashCode(m_Item1!), comparer.GetHashCode(m_Item2!), comparer.GetHashCode(m_Item3!), comparer.GetHashCode(m_Item4!), comparer.GetHashCode(m_Item5!), comparer.GetHashCode(m_Item6!), comparer.GetHashCode(m_Item7!)); + return GetHashCode(comparer); } int ITupleInternal.GetHashCode(IEqualityComparer comparer) { - return ((IStructuralEquatable)this).GetHashCode(comparer); + return GetHashCode(comparer); + } + + private int GetHashCode(IEqualityComparer comparer) + { + return Tuple.CombineHashCodes(comparer.GetHashCode(m_Item1!), comparer.GetHashCode(m_Item2!), comparer.GetHashCode(m_Item3!), comparer.GetHashCode(m_Item4!), comparer.GetHashCode(m_Item5!), comparer.GetHashCode(m_Item6!), comparer.GetHashCode(m_Item7!)); } + public override string ToString() { StringBuilder sb = new StringBuilder(); sb.Append('('); - return ((ITupleInternal)this).ToString(sb); + return ToString(sb); } string ITupleInternal.ToString(StringBuilder sb) + { + return ToString(sb); + } + + private string ToString(StringBuilder sb) { sb.Append(m_Item1); sb.Append(", "); @@ -992,10 +1139,15 @@ public Tuple(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 item public override bool Equals(object? obj) { - return ((IStructuralEquatable)this).Equals(obj, EqualityComparer.Default); + return Equals(obj, EqualityComparer.Default); } bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer) + { + return Equals(other, comparer); + } + + private bool Equals(object? other, IEqualityComparer comparer) { if (other == null) return false; @@ -1009,10 +1161,15 @@ bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer) int IComparable.CompareTo(object? obj) { - return ((IStructuralComparable)this).CompareTo(obj, Comparer.Default); + return CompareTo(obj, Comparer.Default); } int IStructuralComparable.CompareTo(object? other, IComparer comparer) + { + return CompareTo(other, comparer); + } + + private int CompareTo(object? other, IComparer comparer) { if (other == null) return 1; @@ -1054,10 +1211,15 @@ int IStructuralComparable.CompareTo(object? other, IComparer comparer) public override int GetHashCode() { - return ((IStructuralEquatable)this).GetHashCode(EqualityComparer.Default); + return GetHashCode(EqualityComparer.Default); } int IStructuralEquatable.GetHashCode(IEqualityComparer comparer) + { + return GetHashCode(comparer); + } + + private int GetHashCode(IEqualityComparer comparer) { // We want to have a limited hash in this case. We'll use the last 8 elements of the tuple ITupleInternal t = (ITupleInternal)m_Rest; @@ -1088,16 +1250,22 @@ int IStructuralEquatable.GetHashCode(IEqualityComparer comparer) int ITupleInternal.GetHashCode(IEqualityComparer comparer) { - return ((IStructuralEquatable)this).GetHashCode(comparer); + return GetHashCode(comparer); } + public override string ToString() { StringBuilder sb = new StringBuilder(); sb.Append('('); - return ((ITupleInternal)this).ToString(sb); + return ToString(sb); } string ITupleInternal.ToString(StringBuilder sb) + { + return ToString(sb); + } + + private string ToString(StringBuilder sb) { sb.Append(m_Item1); sb.Append(", ");