Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid 0-length array allocation for empty Distinct/Union.ToArray #100218

Merged
merged 1 commit into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static partial class Enumerable
{
private sealed partial class DistinctIterator<TSource>
{
public override TSource[] ToArray() => HashSetToArray(new HashSet<TSource>(_source, _comparer));
public override TSource[] ToArray() => ICollectionToArray(new HashSet<TSource>(_source, _comparer));

public override List<TSource> ToList() => new List<TSource>(new HashSet<TSource>(_source, _comparer));

Expand Down
40 changes: 19 additions & 21 deletions src/libraries/System.Linq/src/System/Linq/ToCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ public static partial class Enumerable
{
public static TSource[] ToArray<TSource>(this IEnumerable<TSource> source)
{
if (source is null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source);
}

#if !OPTIMIZE_FOR_SIZE
if (source is Iterator<TSource> iterator)
{
Expand All @@ -25,22 +20,19 @@ public static TSource[] ToArray<TSource>(this IEnumerable<TSource> source)

if (source is ICollection<TSource> 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);

[MethodImpl(MethodImplOptions.NoInlining)] // avoid large stack allocation impacting other paths
static TSource[] EnumerableToArray(IEnumerable<TSource> source)
{
if (source is null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: nesting validation inside the local method will result in its compiler-generated name to show up in the stack trace. I would prefer it if validation was done separately on each public method even if that results in duplication.

{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source);
}

SegmentedArrayBuilder<TSource>.ScratchBuffer scratch = default;
SegmentedArrayBuilder<TSource> builder = new(scratch);

Expand All @@ -52,6 +44,19 @@ static TSource[] EnumerableToArray(IEnumerable<TSource> source)
}
}

private static TSource[] ICollectionToArray<TSource>(ICollection<TSource> collection)
{
int count = collection.Count;
if (count != 0)
{
var result = new TSource[count];
collection.CopyTo(result, 0);
return result;
}

return [];
}

public static List<TSource> ToList<TSource>(this IEnumerable<TSource> source)
{
if (source is null)
Expand Down Expand Up @@ -259,12 +264,5 @@ public static HashSet<TSource> ToHashSet<TSource>(this IEnumerable<TSource> sour
/// <summary>Default initial capacity to use when creating sets for internal temporary storage.</summary>
/// <remarks>This is based on the implicit size used in previous implementations, which used a custom Set type.</remarks>
private const int DefaultInternalSetCapacity = 7;

private static TSource[] HashSetToArray<TSource>(HashSet<TSource> set)
{
var result = new TSource[set.Count];
set.CopyTo(result);
return result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private HashSet<TSource> FillSet()
}
}

public override TSource[] ToArray() => HashSetToArray(FillSet());
public override TSource[] ToArray() => ICollectionToArray(FillSet());

public override List<TSource> ToList() => new List<TSource>(FillSet());

Expand Down
Loading