diff --git a/src/libraries/System.Linq/src/System/Linq/Distinct.SpeedOpt.cs b/src/libraries/System.Linq/src/System/Linq/Distinct.SpeedOpt.cs index a3dbb6458969cf..4bb5f0373e2036 100644 --- a/src/libraries/System.Linq/src/System/Linq/Distinct.SpeedOpt.cs +++ b/src/libraries/System.Linq/src/System/Linq/Distinct.SpeedOpt.cs @@ -9,7 +9,7 @@ public static partial class Enumerable { private sealed partial class DistinctIterator { - public override TSource[] ToArray() => HashSetToArray(new HashSet(_source, _comparer)); + public override TSource[] ToArray() => ICollectionToArray(new HashSet(_source, _comparer)); public override List ToList() => new List(new HashSet(_source, _comparer)); diff --git a/src/libraries/System.Linq/src/System/Linq/ToCollection.cs b/src/libraries/System.Linq/src/System/Linq/ToCollection.cs index afbf1d4e158a9b..05e18b2382c8c6 100644 --- a/src/libraries/System.Linq/src/System/Linq/ToCollection.cs +++ b/src/libraries/System.Linq/src/System/Linq/ToCollection.cs @@ -11,11 +11,6 @@ public static partial class Enumerable { public static TSource[] ToArray(this IEnumerable source) { - if (source is null) - { - ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); - } - #if !OPTIMIZE_FOR_SIZE if (source is Iterator iterator) { @@ -25,15 +20,7 @@ public static TSource[] ToArray(this IEnumerable source) if (source is ICollection collection) { - int count = collection.Count; - if (count != 0) - { - var result = new TSource[count]; - collection.CopyTo(result, 0); - return result; - } - - return []; + return ICollectionToArray(collection); } return EnumerableToArray(source); @@ -41,6 +28,11 @@ public static TSource[] ToArray(this IEnumerable source) [MethodImpl(MethodImplOptions.NoInlining)] // avoid large stack allocation impacting other paths static TSource[] EnumerableToArray(IEnumerable source) { + if (source is null) + { + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); + } + SegmentedArrayBuilder.ScratchBuffer scratch = default; SegmentedArrayBuilder builder = new(scratch); @@ -52,6 +44,19 @@ static TSource[] EnumerableToArray(IEnumerable source) } } + private static TSource[] ICollectionToArray(ICollection collection) + { + int count = collection.Count; + if (count != 0) + { + var result = new TSource[count]; + collection.CopyTo(result, 0); + return result; + } + + return []; + } + public static List ToList(this IEnumerable source) { if (source is null) @@ -259,12 +264,5 @@ public static HashSet ToHashSet(this IEnumerable sour /// Default initial capacity to use when creating sets for internal temporary storage. /// This is based on the implicit size used in previous implementations, which used a custom Set type. private const int DefaultInternalSetCapacity = 7; - - private static TSource[] HashSetToArray(HashSet set) - { - var result = new TSource[set.Count]; - set.CopyTo(result); - return result; - } } } diff --git a/src/libraries/System.Linq/src/System/Linq/Union.SpeedOpt.cs b/src/libraries/System.Linq/src/System/Linq/Union.SpeedOpt.cs index e907c63562ddb3..9f50bf0c39a331 100644 --- a/src/libraries/System.Linq/src/System/Linq/Union.SpeedOpt.cs +++ b/src/libraries/System.Linq/src/System/Linq/Union.SpeedOpt.cs @@ -24,7 +24,7 @@ private HashSet FillSet() } } - public override TSource[] ToArray() => HashSetToArray(FillSet()); + public override TSource[] ToArray() => ICollectionToArray(FillSet()); public override List ToList() => new List(FillSet());