From 1ba2f85a1c3546e1b465bd0ebfe78c297f1b5acc Mon Sep 17 00:00:00 2001 From: Pent Ploompuu Date: Fri, 13 Mar 2026 21:45:22 +0800 Subject: [PATCH 1/4] Simplify Guid comparisons --- .../System.Private.CoreLib/src/System/Guid.cs | 276 ++---------------- 1 file changed, 20 insertions(+), 256 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Guid.cs b/src/libraries/System.Private.CoreLib/src/System/Guid.cs index 270da4ab7d0b09..5058eb72d9a87a 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Guid.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Guid.cs @@ -1104,8 +1104,6 @@ private static bool EqualsCore(in Guid left, in Guid right) && Unsafe.Add(ref rA, 3) == Unsafe.Add(ref rB, 3); } - private static int GetResult(uint me, uint them) => me < them ? -1 : 1; - public int CompareTo(object? value) { if (value == null) @@ -1123,62 +1121,24 @@ public int CompareTo(Guid value) { if (value._a != _a) { - return GetResult((uint)_a, (uint)value._a); - } - - if (value._b != _b) - { - return GetResult((uint)_b, (uint)value._b); - } - - if (value._c != _c) - { - return GetResult((uint)_c, (uint)value._c); - } - - if (value._d != _d) - { - return GetResult(_d, value._d); - } - - if (value._e != _e) - { - return GetResult(_e, value._e); - } - - if (value._f != _f) - { - return GetResult(_f, value._f); - } - - if (value._g != _g) - { - return GetResult(_g, value._g); + return ((uint)_a).CompareTo((uint)value._a); } - if (value._h != _h) + if ((ushort)value._b != (ushort)_b) { - return GetResult(_h, value._h); + return ((ushort)_b).CompareTo((ushort)value._b); } - if (value._i != _i) + if ((ushort)value._c != (ushort)_c) { - return GetResult(_i, value._i); + return ((ushort)_c).CompareTo((ushort)value._c); } - if (value._j != _j) - { - return GetResult(_j, value._j); - } - - if (value._k != _k) - { - return GetResult(_k, value._k); - } - - return 0; + return GetLow64().CompareTo(value.GetLow64()); } + private ulong GetLow64() => BinaryPrimitives.ReadUInt64BigEndian(MemoryMarshal.CreateReadOnlySpan(in _d, 8)); + public static bool operator ==(Guid a, Guid b) => EqualsCore(a, b); public static bool operator !=(Guid a, Guid b) => !EqualsCore(a, b); @@ -1593,57 +1553,17 @@ private static (Vector128, Vector128, Vector128) FormatGuidVec return (uint)left._a < (uint)right._a; } - if (left._b != right._b) - { - return (uint)left._b < (uint)right._b; - } - - if (left._c != right._c) - { - return (uint)left._c < (uint)right._c; - } - - if (left._d != right._d) - { - return left._d < right._d; - } - - if (left._e != right._e) - { - return left._e < right._e; - } - - if (left._f != right._f) + if ((ushort)left._b != (ushort)right._b) { - return left._f < right._f; + return (ushort)left._b < (ushort)right._b; } - if (left._g != right._g) + if ((ushort)left._c != (ushort)right._c) { - return left._g < right._g; + return (ushort)left._c < (ushort)right._c; } - if (left._h != right._h) - { - return left._h < right._h; - } - - if (left._i != right._i) - { - return left._i < right._i; - } - - if (left._j != right._j) - { - return left._j < right._j; - } - - if (left._k != right._k) - { - return left._k < right._k; - } - - return false; + return left.GetLow64() < right.GetLow64(); } /// @@ -1654,180 +1574,24 @@ private static (Vector128, Vector128, Vector128) FormatGuidVec return (uint)left._a < (uint)right._a; } - if (left._b != right._b) - { - return (uint)left._b < (uint)right._b; - } - - if (left._c != right._c) - { - return (uint)left._c < (uint)right._c; - } - - if (left._d != right._d) - { - return left._d < right._d; - } - - if (left._e != right._e) - { - return left._e < right._e; - } - - if (left._f != right._f) - { - return left._f < right._f; - } - - if (left._g != right._g) - { - return left._g < right._g; - } - - if (left._h != right._h) - { - return left._h < right._h; - } - - if (left._i != right._i) - { - return left._i < right._i; - } - - if (left._j != right._j) + if ((ushort)left._b != (ushort)right._b) { - return left._j < right._j; + return (ushort)left._b < (ushort)right._b; } - if (left._k != right._k) + if ((ushort)left._c != (ushort)right._c) { - return left._k < right._k; + return (ushort)left._c < (ushort)right._c; } - return true; + return left.GetLow64() <= right.GetLow64(); } /// - public static bool operator >(Guid left, Guid right) - { - if (left._a != right._a) - { - return (uint)left._a > (uint)right._a; - } - - if (left._b != right._b) - { - return (uint)left._b > (uint)right._b; - } - - if (left._c != right._c) - { - return (uint)left._c > (uint)right._c; - } - - if (left._d != right._d) - { - return left._d > right._d; - } - - if (left._e != right._e) - { - return left._e > right._e; - } - - if (left._f != right._f) - { - return left._f > right._f; - } - - if (left._g != right._g) - { - return left._g > right._g; - } - - if (left._h != right._h) - { - return left._h > right._h; - } - - if (left._i != right._i) - { - return left._i > right._i; - } - - if (left._j != right._j) - { - return left._j > right._j; - } - - if (left._k != right._k) - { - return left._k > right._k; - } - - return false; - } + public static bool operator >(Guid left, Guid right) => right < left; /// - public static bool operator >=(Guid left, Guid right) - { - if (left._a != right._a) - { - return (uint)left._a > (uint)right._a; - } - - if (left._b != right._b) - { - return (uint)left._b > (uint)right._b; - } - - if (left._c != right._c) - { - return (uint)left._c > (uint)right._c; - } - - if (left._d != right._d) - { - return left._d > right._d; - } - - if (left._e != right._e) - { - return left._e > right._e; - } - - if (left._f != right._f) - { - return left._f > right._f; - } - - if (left._g != right._g) - { - return left._g > right._g; - } - - if (left._h != right._h) - { - return left._h > right._h; - } - - if (left._i != right._i) - { - return left._i > right._i; - } - - if (left._j != right._j) - { - return left._j > right._j; - } - - if (left._k != right._k) - { - return left._k > right._k; - } - - return true; - } + public static bool operator >=(Guid left, Guid right) => right <= left; // // IParsable From 934718f1687e870b8c753a7f05dea71714f76170 Mon Sep 17 00:00:00 2001 From: Pent Ploompuu Date: Tue, 17 Mar 2026 12:18:11 +0800 Subject: [PATCH 2/4] Address PR feedback --- .../System.Private.CoreLib/src/System/Guid.cs | 74 ++++--------------- 1 file changed, 16 insertions(+), 58 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Guid.cs b/src/libraries/System.Private.CoreLib/src/System/Guid.cs index 5058eb72d9a87a..b3a9abd2b6b35b 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Guid.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Guid.cs @@ -1119,26 +1119,20 @@ public int CompareTo(object? value) public int CompareTo(Guid value) { - if (value._a != _a) - { - return ((uint)_a).CompareTo((uint)value._a); - } - - if ((ushort)value._b != (ushort)_b) - { - return ((ushort)_b).CompareTo((ushort)value._b); - } - - if ((ushort)value._c != (ushort)_c) - { - return ((ushort)_c).CompareTo((ushort)value._c); - } - - return GetLow64().CompareTo(value.GetLow64()); + int c; + return (c = ((uint)_a).CompareTo((uint)value._a)) != 0 ? c : + (c = ((ushort)_b).CompareTo((ushort)value._b)) != 0 ? c : + (c = ((ushort)_c).CompareTo((ushort)value._c)) != 0 ? c : + (c = _d.CompareTo(value._d)) != 0 ? c : + (c = _e.CompareTo(value._e)) != 0 ? c : + (c = _f.CompareTo(value._f)) != 0 ? c : + (c = _g.CompareTo(value._g)) != 0 ? c : + (c = _h.CompareTo(value._h)) != 0 ? c : + (c = _i.CompareTo(value._i)) != 0 ? c : + (c = _j.CompareTo(value._j)) != 0 ? c : + _k.CompareTo(value._k); } - private ulong GetLow64() => BinaryPrimitives.ReadUInt64BigEndian(MemoryMarshal.CreateReadOnlySpan(in _d, 8)); - public static bool operator ==(Guid a, Guid b) => EqualsCore(a, b); public static bool operator !=(Guid a, Guid b) => !EqualsCore(a, b); @@ -1546,52 +1540,16 @@ private static (Vector128, Vector128, Vector128) FormatGuidVec // /// - public static bool operator <(Guid left, Guid right) - { - if (left._a != right._a) - { - return (uint)left._a < (uint)right._a; - } - - if ((ushort)left._b != (ushort)right._b) - { - return (ushort)left._b < (ushort)right._b; - } - - if ((ushort)left._c != (ushort)right._c) - { - return (ushort)left._c < (ushort)right._c; - } - - return left.GetLow64() < right.GetLow64(); - } + public static bool operator <(Guid left, Guid right) => left.CompareTo(right) < 0; /// - public static bool operator <=(Guid left, Guid right) - { - if (left._a != right._a) - { - return (uint)left._a < (uint)right._a; - } - - if ((ushort)left._b != (ushort)right._b) - { - return (ushort)left._b < (ushort)right._b; - } - - if ((ushort)left._c != (ushort)right._c) - { - return (ushort)left._c < (ushort)right._c; - } - - return left.GetLow64() <= right.GetLow64(); - } + public static bool operator <=(Guid left, Guid right) => left.CompareTo(right) <= 0; /// - public static bool operator >(Guid left, Guid right) => right < left; + public static bool operator >(Guid left, Guid right) => left.CompareTo(right) > 0; /// - public static bool operator >=(Guid left, Guid right) => right <= left; + public static bool operator >=(Guid left, Guid right) => left.CompareTo(right) >= 0; // // IParsable From 4266688e23c7d47d995387fa50739409a58fc8fb Mon Sep 17 00:00:00 2001 From: Pent Ploompuu Date: Fri, 20 Mar 2026 21:03:03 +0800 Subject: [PATCH 3/4] Address PR feedback p2 --- .../System.Private.CoreLib/src/System/Guid.cs | 36 ++++++++++++------- .../src/System/UInt128.cs | 8 ++--- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Guid.cs b/src/libraries/System.Private.CoreLib/src/System/Guid.cs index b3a9abd2b6b35b..02b7a6908e6edf 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Guid.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Guid.cs @@ -1119,18 +1119,30 @@ public int CompareTo(object? value) public int CompareTo(Guid value) { - int c; - return (c = ((uint)_a).CompareTo((uint)value._a)) != 0 ? c : - (c = ((ushort)_b).CompareTo((ushort)value._b)) != 0 ? c : - (c = ((ushort)_c).CompareTo((ushort)value._c)) != 0 ? c : - (c = _d.CompareTo(value._d)) != 0 ? c : - (c = _e.CompareTo(value._e)) != 0 ? c : - (c = _f.CompareTo(value._f)) != 0 ? c : - (c = _g.CompareTo(value._g)) != 0 ? c : - (c = _h.CompareTo(value._h)) != 0 ? c : - (c = _i.CompareTo(value._i)) != 0 ? c : - (c = _j.CompareTo(value._j)) != 0 ? c : - _k.CompareTo(value._k); + if (!BitConverter.IsLittleEndian) + { + return Unsafe.BitCast(this).CompareTo(Unsafe.BitCast(value)); + } + + if (_a != value._a) + { + return (uint)_a < (uint)value._a ? -1 : 1; + } + + int r = ((ushort)_b).CompareTo((ushort)value._b); + if (r != 0) + { + return r; + } + + r = ((ushort)_c).CompareTo((ushort)value._c); + if (r != 0) + { + return r; + } + + return BinaryPrimitives.ReverseEndianness(Unsafe.BitCast(this)._upper) + .CompareTo(BinaryPrimitives.ReverseEndianness(Unsafe.BitCast(value)._upper)); } public static bool operator ==(Guid a, Guid b) => EqualsCore(a, b); diff --git a/src/libraries/System.Private.CoreLib/src/System/UInt128.cs b/src/libraries/System.Private.CoreLib/src/System/UInt128.cs index 20ad904bc423a2..8b6a1ba5371ae9 100644 --- a/src/libraries/System.Private.CoreLib/src/System/UInt128.cs +++ b/src/libraries/System.Private.CoreLib/src/System/UInt128.cs @@ -26,11 +26,11 @@ public readonly struct UInt128 internal const int Size = 16; #if BIGENDIAN - private readonly ulong _upper; - private readonly ulong _lower; + internal readonly ulong _upper; + internal readonly ulong _lower; #else - private readonly ulong _lower; - private readonly ulong _upper; + internal readonly ulong _lower; + internal readonly ulong _upper; #endif /// Initializes a new instance of the struct. From 2240bdb894c2672d0fb848dcd7b3834a7d876531 Mon Sep 17 00:00:00 2001 From: Pent Ploompuu Date: Fri, 20 Mar 2026 22:21:02 +0800 Subject: [PATCH 4/4] Cleanup --- .../System.Private.CoreLib/src/System/Guid.cs | 18 ++++++++++-------- .../src/System/UInt128.cs | 8 ++++---- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Guid.cs b/src/libraries/System.Private.CoreLib/src/System/Guid.cs index 02b7a6908e6edf..4032e790117f01 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Guid.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Guid.cs @@ -1119,12 +1119,7 @@ public int CompareTo(object? value) public int CompareTo(Guid value) { - if (!BitConverter.IsLittleEndian) - { - return Unsafe.BitCast(this).CompareTo(Unsafe.BitCast(value)); - } - - if (_a != value._a) + if (value._a != _a) { return (uint)_a < (uint)value._a ? -1 : 1; } @@ -1141,8 +1136,15 @@ public int CompareTo(Guid value) return r; } - return BinaryPrimitives.ReverseEndianness(Unsafe.BitCast(this)._upper) - .CompareTo(BinaryPrimitives.ReverseEndianness(Unsafe.BitCast(value)._upper)); + ulong x = Unsafe.BitCast(this).Item2; + ulong y = Unsafe.BitCast(value).Item2; + if (BitConverter.IsLittleEndian) + { + x = BinaryPrimitives.ReverseEndianness(x); + y = BinaryPrimitives.ReverseEndianness(y); + } + + return x.CompareTo(y); } public static bool operator ==(Guid a, Guid b) => EqualsCore(a, b); diff --git a/src/libraries/System.Private.CoreLib/src/System/UInt128.cs b/src/libraries/System.Private.CoreLib/src/System/UInt128.cs index 8b6a1ba5371ae9..20ad904bc423a2 100644 --- a/src/libraries/System.Private.CoreLib/src/System/UInt128.cs +++ b/src/libraries/System.Private.CoreLib/src/System/UInt128.cs @@ -26,11 +26,11 @@ public readonly struct UInt128 internal const int Size = 16; #if BIGENDIAN - internal readonly ulong _upper; - internal readonly ulong _lower; + private readonly ulong _upper; + private readonly ulong _lower; #else - internal readonly ulong _lower; - internal readonly ulong _upper; + private readonly ulong _lower; + private readonly ulong _upper; #endif /// Initializes a new instance of the struct.