From df148a6b3a19722dc1eea51e756c644279fedf16 Mon Sep 17 00:00:00 2001 From: Advitalitum Date: Sun, 27 Sep 2026 01:35:12 +0300 Subject: [PATCH 01/14] Add fast path for Overlaps --- .../Frozen/FrozenSetInternalBase.cs | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs index 4afe1f7503e757..5680e60cc72a17 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs @@ -130,6 +130,17 @@ private protected override bool OverlapsCore(IEnumerable other) { Debug.Assert(_thisSet.Count != 0, "EmptyFrozenSet should have been used."); + // Fast path for Overlaps when other is IReadOnlySet with fast lookup and same equality comparer + if (other is IReadOnlySet otherAsSet && ComparersAreCompatible(otherAsSet)) + { + return otherAsSet.Count > Count ? OverlapsCompatibleReadOnlySetImpl(this, otherAsSet) : OverlapsCompatibleReadOnlySetImpl(otherAsSet, this); + } + + return OverlapsEnumerableImpl(other); + } + + private bool OverlapsEnumerableImpl(IEnumerable other) + { foreach (T element in other) { if (_thisSet.FindItemIndex(element) >= 0) @@ -141,6 +152,19 @@ private protected override bool OverlapsCore(IEnumerable other) return false; } + private static bool OverlapsCompatibleReadOnlySetImpl(IReadOnlySet smaller, IReadOnlySet larger) + { + foreach (T element in smaller) + { + if (larger.Contains(element)) + { + return true; + } + } + + return false; + } + /// private protected override bool SetEqualsCore(IEnumerable other) { From 1f4f341aafaf3fe7878ea7c15a005518b4c2028b Mon Sep 17 00:00:00 2001 From: Advitalitum Date: Sun, 27 Sep 2026 01:41:38 +0300 Subject: [PATCH 02/14] Add line breaks --- .../src/System/Collections/Frozen/FrozenSetInternalBase.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs index 5680e60cc72a17..7494e26c9d56dc 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs @@ -133,7 +133,9 @@ private protected override bool OverlapsCore(IEnumerable other) // Fast path for Overlaps when other is IReadOnlySet with fast lookup and same equality comparer if (other is IReadOnlySet otherAsSet && ComparersAreCompatible(otherAsSet)) { - return otherAsSet.Count > Count ? OverlapsCompatibleReadOnlySetImpl(this, otherAsSet) : OverlapsCompatibleReadOnlySetImpl(otherAsSet, this); + return otherAsSet.Count > Count + ? OverlapsCompatibleReadOnlySetImpl(this, otherAsSet) + : OverlapsCompatibleReadOnlySetImpl(otherAsSet, this); } return OverlapsEnumerableImpl(other); From 40811cf4e3f6ed83986ec6c587ea28b3534c82a9 Mon Sep 17 00:00:00 2001 From: Advitalitum Date: Sun, 27 Sep 2026 02:54:56 +0300 Subject: [PATCH 03/14] Fix trailing whitespace --- .../src/System/Collections/Frozen/FrozenSetInternalBase.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs index 7494e26c9d56dc..e8e141832173d2 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs @@ -139,7 +139,7 @@ private protected override bool OverlapsCore(IEnumerable other) } return OverlapsEnumerableImpl(other); - } + } private bool OverlapsEnumerableImpl(IEnumerable other) { From 586d79c37849e16f20b58e678d3913e0a2d18460 Mon Sep 17 00:00:00 2001 From: Advitalitum Date: Sun, 27 Sep 2026 03:08:09 +0300 Subject: [PATCH 04/14] FrozenSet not always implement IReadOnlySet --- .../Frozen/FrozenSetInternalBase.cs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs index e8e141832173d2..3f2bdf2c4f2459 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs @@ -134,8 +134,8 @@ private protected override bool OverlapsCore(IEnumerable other) if (other is IReadOnlySet otherAsSet && ComparersAreCompatible(otherAsSet)) { return otherAsSet.Count > Count - ? OverlapsCompatibleReadOnlySetImpl(this, otherAsSet) - : OverlapsCompatibleReadOnlySetImpl(otherAsSet, this); + ? OverlapsCompatibleSetImpl(this, otherAsSet) + : OverlapsCompatibleSetImpl(otherAsSet, this); } return OverlapsEnumerableImpl(other); @@ -154,7 +154,20 @@ private bool OverlapsEnumerableImpl(IEnumerable other) return false; } - private static bool OverlapsCompatibleReadOnlySetImpl(IReadOnlySet smaller, IReadOnlySet larger) + private static bool OverlapsCompatibleSetImpl(FrozenSet smaller, IReadOnlySet larger) + { + foreach (T element in smaller) + { + if (larger.Contains(element)) + { + return true; + } + } + + return false; + } + + private static bool OverlapsCompatibleSetImpl(IReadOnlySet smaller, FrozenSet larger) { foreach (T element in smaller) { From a41ce12fb03802de7620038878ca64ec67a7f47c Mon Sep 17 00:00:00 2001 From: Advitalitum Date: Sun, 27 Sep 2026 03:26:02 +0300 Subject: [PATCH 05/14] Rename new methods --- .../System/Collections/Frozen/FrozenSetInternalBase.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs index 3f2bdf2c4f2459..f36ce9cf0eb0ea 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs @@ -134,8 +134,8 @@ private protected override bool OverlapsCore(IEnumerable other) if (other is IReadOnlySet otherAsSet && ComparersAreCompatible(otherAsSet)) { return otherAsSet.Count > Count - ? OverlapsCompatibleSetImpl(this, otherAsSet) - : OverlapsCompatibleSetImpl(otherAsSet, this); + ? SmallerFrozenSetOverlapsLargerCompatibleReadOnlySetImpl(this, otherAsSet) + : LargerFrozenSetOverlapsSmallerCompatibleReadOnlySetImpl(otherAsSet, this); } return OverlapsEnumerableImpl(other); @@ -154,7 +154,7 @@ private bool OverlapsEnumerableImpl(IEnumerable other) return false; } - private static bool OverlapsCompatibleSetImpl(FrozenSet smaller, IReadOnlySet larger) + private static bool SmallerFrozenSetOverlapsLargerCompatibleReadOnlySetImpl(FrozenSet smaller, IReadOnlySet larger) { foreach (T element in smaller) { @@ -167,7 +167,7 @@ private static bool OverlapsCompatibleSetImpl(FrozenSet smaller, IReadOnlySet return false; } - private static bool OverlapsCompatibleSetImpl(IReadOnlySet smaller, FrozenSet larger) + private static bool LargerFrozenSetOverlapsSmallerCompatibleReadOnlySetImpl(IReadOnlySet smaller, FrozenSet larger) { foreach (T element in smaller) { From 5ecb3690dd718b9f4612354add6ab98edcda293a Mon Sep 17 00:00:00 2001 From: Advitalitum Date: Sat, 3 Oct 2026 19:43:51 +0300 Subject: [PATCH 06/14] Try use common interface --- .../Frozen/FrozenSetInternalBase.cs | 32 +++++++++---------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs index f36ce9cf0eb0ea..40be7acf100adb 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs @@ -131,11 +131,11 @@ private protected override bool OverlapsCore(IEnumerable other) Debug.Assert(_thisSet.Count != 0, "EmptyFrozenSet should have been used."); // Fast path for Overlaps when other is IReadOnlySet with fast lookup and same equality comparer - if (other is IReadOnlySet otherAsSet && ComparersAreCompatible(otherAsSet)) + if (other is ISet otherAsSet && HasFastLookupAndComparersAreCompatible(otherAsSet)) { return otherAsSet.Count > Count - ? SmallerFrozenSetOverlapsLargerCompatibleReadOnlySetImpl(this, otherAsSet) - : LargerFrozenSetOverlapsSmallerCompatibleReadOnlySetImpl(otherAsSet, this); + ? SmallerSetOverlapsLargerCompatibleSetImpl(this, otherAsSet) + : SmallerSetOverlapsLargerCompatibleSetImpl(otherAsSet, this); } return OverlapsEnumerableImpl(other); @@ -154,20 +154,7 @@ private bool OverlapsEnumerableImpl(IEnumerable other) return false; } - private static bool SmallerFrozenSetOverlapsLargerCompatibleReadOnlySetImpl(FrozenSet smaller, IReadOnlySet larger) - { - foreach (T element in smaller) - { - if (larger.Contains(element)) - { - return true; - } - } - - return false; - } - - private static bool LargerFrozenSetOverlapsSmallerCompatibleReadOnlySetImpl(IReadOnlySet smaller, FrozenSet larger) + private static bool SmallerSetOverlapsLargerCompatibleSetImpl(ISet smaller, ISet larger) { foreach (T element in smaller) { @@ -207,6 +194,17 @@ private bool ComparersAreCompatible(IReadOnlySet other) => _ => false }; + private bool HasFastLookupAndComparersAreCompatible(ISet other) => + other switch + { + HashSet hs => _thisSet.Comparer.Equals(hs.Comparer), + SortedSet sortedSet => _thisSet.Comparer.Equals(sortedSet.Comparer), + ImmutableHashSet ihs => _thisSet.Comparer.Equals(ihs.KeyComparer), + ImmutableSortedSet iss => _thisSet.Comparer.Equals(iss.KeyComparer), + FrozenSet fs => _thisSet.Comparer.Equals(fs.Comparer), + _ => false + }; + /// /// Determines counts that can be used to determine equality, subset, and superset. /// From 2f825598b028b6e9e54aba836d1e97bbc5b99f9e Mon Sep 17 00:00:00 2001 From: Advitalitum Date: Sat, 3 Oct 2026 19:53:52 +0300 Subject: [PATCH 07/14] Fix comment --- .../src/System/Collections/Frozen/FrozenSetInternalBase.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs index 40be7acf100adb..262ed9438175c3 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs @@ -130,7 +130,7 @@ private protected override bool OverlapsCore(IEnumerable other) { Debug.Assert(_thisSet.Count != 0, "EmptyFrozenSet should have been used."); - // Fast path for Overlaps when other is IReadOnlySet with fast lookup and same equality comparer + // Fast path for Overlaps when other is ISet with fast lookup and same equality comparer if (other is ISet otherAsSet && HasFastLookupAndComparersAreCompatible(otherAsSet)) { return otherAsSet.Count > Count From 2071bf0814589f7e11bface26aff31578a999ce8 Mon Sep 17 00:00:00 2001 From: Advitalitum Date: Sun, 4 Oct 2026 03:30:25 +0300 Subject: [PATCH 08/14] Remove SortedSet and ImmutableSortedSet from HasFastLookupAndComparersAreCompatible. --- .../src/System/Collections/Frozen/FrozenSetInternalBase.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs index 262ed9438175c3..18801a78892c41 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs @@ -198,9 +198,7 @@ private bool HasFastLookupAndComparersAreCompatible(ISet other) => other switch { HashSet hs => _thisSet.Comparer.Equals(hs.Comparer), - SortedSet sortedSet => _thisSet.Comparer.Equals(sortedSet.Comparer), ImmutableHashSet ihs => _thisSet.Comparer.Equals(ihs.KeyComparer), - ImmutableSortedSet iss => _thisSet.Comparer.Equals(iss.KeyComparer), FrozenSet fs => _thisSet.Comparer.Equals(fs.Comparer), _ => false }; From 86dc88abf96a1a78487ae8358a8b4a0a4ba95b02 Mon Sep 17 00:00:00 2001 From: Advitalitum Date: Sun, 4 Oct 2026 13:39:50 +0300 Subject: [PATCH 09/14] Remove ImmutableHashSet HasFastLookupAndComparersAreCompatible, it's O(log(n) --- .../src/System/Collections/Frozen/FrozenSetInternalBase.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs index 18801a78892c41..2ed1e32aad11cb 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs @@ -198,7 +198,6 @@ private bool HasFastLookupAndComparersAreCompatible(ISet other) => other switch { HashSet hs => _thisSet.Comparer.Equals(hs.Comparer), - ImmutableHashSet ihs => _thisSet.Comparer.Equals(ihs.KeyComparer), FrozenSet fs => _thisSet.Comparer.Equals(fs.Comparer), _ => false }; From d5f13753454063e3c95e104770b931cab4df6e45 Mon Sep 17 00:00:00 2001 From: Advitalitum Date: Sun, 4 Oct 2026 19:41:42 +0300 Subject: [PATCH 10/14] Try not allocate enumerator --- .../Frozen/FrozenSetInternalBase.cs | 35 ++++++++++--------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs index 2ed1e32aad11cb..846166998f0d6b 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs @@ -130,15 +130,24 @@ private protected override bool OverlapsCore(IEnumerable other) { Debug.Assert(_thisSet.Count != 0, "EmptyFrozenSet should have been used."); - // Fast path for Overlaps when other is ISet with fast lookup and same equality comparer - if (other is ISet otherAsSet && HasFastLookupAndComparersAreCompatible(otherAsSet)) + + switch (other) { - return otherAsSet.Count > Count - ? SmallerSetOverlapsLargerCompatibleSetImpl(this, otherAsSet) - : SmallerSetOverlapsLargerCompatibleSetImpl(otherAsSet, this); + // Fast path for Overlaps when other is HashSet and same equality comparer + case HashSet hs when _thisSet.Comparer.Equals(hs.Comparer): + return hs.Count > Count + ? SmallerSetOverlapsLargerCompatibleSetImpl(this, hs) + : SmallerSetOverlapsLargerCompatibleSetImpl(hs, this); + + // Fast path for Overlaps when other is FrozenSet and same equality comparer + case FrozenSet fs when _thisSet.Comparer.Equals(fs.Comparer): + return fs.Count > Count + ? SmallerSetOverlapsLargerCompatibleSetImpl(this, fs) + : SmallerSetOverlapsLargerCompatibleSetImpl(fs, this); + + default: + return OverlapsEnumerableImpl(other); } - - return OverlapsEnumerableImpl(other); } private bool OverlapsEnumerableImpl(IEnumerable other) @@ -154,7 +163,9 @@ private bool OverlapsEnumerableImpl(IEnumerable other) return false; } - private static bool SmallerSetOverlapsLargerCompatibleSetImpl(ISet smaller, ISet larger) + private static bool SmallerSetOverlapsLargerCompatibleSetImpl(TSmallerSet smaller, TLargerSet larger) + where TSmallerSet : ISet + where TLargerSet : ISet { foreach (T element in smaller) { @@ -194,14 +205,6 @@ private bool ComparersAreCompatible(IReadOnlySet other) => _ => false }; - private bool HasFastLookupAndComparersAreCompatible(ISet other) => - other switch - { - HashSet hs => _thisSet.Comparer.Equals(hs.Comparer), - FrozenSet fs => _thisSet.Comparer.Equals(fs.Comparer), - _ => false - }; - /// /// Determines counts that can be used to determine equality, subset, and superset. /// From 84736c870cada0c80426f29ed9dd8fc5e1afc3ef Mon Sep 17 00:00:00 2001 From: Advitalitum Date: Sun, 4 Oct 2026 20:49:24 +0300 Subject: [PATCH 11/14] Use switch expression and aditional method --- .../Frozen/FrozenSetInternalBase.cs | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs index 846166998f0d6b..1207ef950b847a 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs @@ -130,24 +130,16 @@ private protected override bool OverlapsCore(IEnumerable other) { Debug.Assert(_thisSet.Count != 0, "EmptyFrozenSet should have been used."); - - switch (other) + return other switch { // Fast path for Overlaps when other is HashSet and same equality comparer - case HashSet hs when _thisSet.Comparer.Equals(hs.Comparer): - return hs.Count > Count - ? SmallerSetOverlapsLargerCompatibleSetImpl(this, hs) - : SmallerSetOverlapsLargerCompatibleSetImpl(hs, this); + HashSet hs when _thisSet.Comparer.Equals(hs.Comparer) => OverlapsCompatibleSetImpl(hs), // Fast path for Overlaps when other is FrozenSet and same equality comparer - case FrozenSet fs when _thisSet.Comparer.Equals(fs.Comparer): - return fs.Count > Count - ? SmallerSetOverlapsLargerCompatibleSetImpl(this, fs) - : SmallerSetOverlapsLargerCompatibleSetImpl(fs, this); + FrozenSet fs when _thisSet.Comparer.Equals(fs.Comparer) => OverlapsCompatibleSetImpl(fs), - default: - return OverlapsEnumerableImpl(other); - } + _ => OverlapsEnumerableImpl(other) + }; } private bool OverlapsEnumerableImpl(IEnumerable other) @@ -163,6 +155,14 @@ private bool OverlapsEnumerableImpl(IEnumerable other) return false; } + private bool OverlapsCompatibleSetImpl(TOtherSet other) + where TOtherSet : ISet + { + return other.Count > Count + ? SmallerSetOverlapsLargerCompatibleSetImpl(this, other) + : SmallerSetOverlapsLargerCompatibleSetImpl(other, this); + } + private static bool SmallerSetOverlapsLargerCompatibleSetImpl(TSmallerSet smaller, TLargerSet larger) where TSmallerSet : ISet where TLargerSet : ISet From 2986b810ee0862e8efa59ed49c263c80cbcf4d3f Mon Sep 17 00:00:00 2001 From: Advitalitum Date: Sun, 4 Oct 2026 22:20:08 +0300 Subject: [PATCH 12/14] Make it dirty --- .../Frozen/FrozenSetInternalBase.cs | 40 ++++++++++++++++--- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs index 1207ef950b847a..5ae2d44dd52ba4 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs @@ -155,17 +155,47 @@ private bool OverlapsEnumerableImpl(IEnumerable other) return false; } - private bool OverlapsCompatibleSetImpl(TOtherSet other) - where TOtherSet : ISet + private bool OverlapsCompatibleSetImpl(HashSet other) { return other.Count > Count ? SmallerSetOverlapsLargerCompatibleSetImpl(this, other) : SmallerSetOverlapsLargerCompatibleSetImpl(other, this); } - private static bool SmallerSetOverlapsLargerCompatibleSetImpl(TSmallerSet smaller, TLargerSet larger) - where TSmallerSet : ISet - where TLargerSet : ISet + private bool OverlapsCompatibleSetImpl(FrozenSet other) + { + return other.Count > Count + ? SmallerSetOverlapsLargerCompatibleSetImpl(this, other) + : SmallerSetOverlapsLargerCompatibleSetImpl(other, this); + } + + private static bool SmallerSetOverlapsLargerCompatibleSetImpl(FrozenSet smaller, FrozenSet larger) + { + foreach (T element in smaller) + { + if (larger.Contains(element)) + { + return true; + } + } + + return false; + } + + private static bool SmallerSetOverlapsLargerCompatibleSetImpl(FrozenSet smaller, HashSet larger) + { + foreach (T element in smaller) + { + if (larger.Contains(element)) + { + return true; + } + } + + return false; + } + + private static bool SmallerSetOverlapsLargerCompatibleSetImpl(HashSet smaller, FrozenSet larger) { foreach (T element in smaller) { From 5f8cd968bff2220474201fdf87248519f1ebb3e6 Mon Sep 17 00:00:00 2001 From: Advitalitum Date: Mon, 5 Oct 2026 01:34:49 +0300 Subject: [PATCH 13/14] Make less overloads --- .../Collections/Frozen/FrozenSetInternalBase.cs | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs index 5ae2d44dd52ba4..9304c2f096f6d4 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs @@ -169,7 +169,7 @@ private bool OverlapsCompatibleSetImpl(FrozenSet other) : SmallerSetOverlapsLargerCompatibleSetImpl(other, this); } - private static bool SmallerSetOverlapsLargerCompatibleSetImpl(FrozenSet smaller, FrozenSet larger) + private static bool SmallerSetOverlapsLargerCompatibleSetImpl(FrozenSet smaller, ISet larger) { foreach (T element in smaller) { @@ -182,20 +182,7 @@ private static bool SmallerSetOverlapsLargerCompatibleSetImpl(FrozenSet small return false; } - private static bool SmallerSetOverlapsLargerCompatibleSetImpl(FrozenSet smaller, HashSet larger) - { - foreach (T element in smaller) - { - if (larger.Contains(element)) - { - return true; - } - } - - return false; - } - - private static bool SmallerSetOverlapsLargerCompatibleSetImpl(HashSet smaller, FrozenSet larger) + private static bool SmallerSetOverlapsLargerCompatibleSetImpl(HashSet smaller, ISet larger) { foreach (T element in smaller) { From e1da6c0d390b8275c4e4158133fd79eede50dc74 Mon Sep 17 00:00:00 2001 From: Advitalitum Date: Mon, 5 Oct 2026 02:13:55 +0300 Subject: [PATCH 14/14] Use FrozenSetInternalBase instead of FrozenSet --- .../src/System/Collections/Frozen/FrozenSetInternalBase.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs index 9304c2f096f6d4..c6dd2612f7b404 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSetInternalBase.cs @@ -182,11 +182,11 @@ private static bool SmallerSetOverlapsLargerCompatibleSetImpl(FrozenSet small return false; } - private static bool SmallerSetOverlapsLargerCompatibleSetImpl(HashSet smaller, ISet larger) + private static bool SmallerSetOverlapsLargerCompatibleSetImpl(HashSet smaller, FrozenSetInternalBase larger) { foreach (T element in smaller) { - if (larger.Contains(element)) + if (larger._thisSet.FindItemIndex(element) >= 0) { return true; }