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

Fix which path is taken in Enumerable.Select for empty partition #88425

Merged
merged 2 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -8,5 +8,10 @@ namespace System.Linq
public static partial class Enumerable
{
public static IEnumerable<TResult> Empty<TResult>() => Array.Empty<TResult>();

private static IEnumerable<TResult>? GetEmptyIfEmpty<TSource, TResult>(IEnumerable<TSource> source) =>
ReferenceEquals(source, Array.Empty<TSource>()) ?
Array.Empty<TResult>() :
null;
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,10 @@ namespace System.Linq
public static partial class Enumerable
{
public static IEnumerable<TResult> Empty<TResult>() => EmptyPartition<TResult>.Instance;

private static IEnumerable<TResult>? GetEmptyIfEmpty<TSource, TResult>(IEnumerable<TSource> source) =>
source is EmptyPartition<TSource> ?
EmptyPartition<TResult>.Instance :
null;
}
}
4 changes: 1 addition & 3 deletions src/libraries/System.Linq/src/System/Linq/Select.SpeedOpt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ public static partial class Enumerable
static partial void CreateSelectIPartitionIterator<TResult, TSource>(
Func<TSource, TResult> selector, IPartition<TSource> partition, ref IEnumerable<TResult>? result)
{
result = partition is EmptyPartition<TSource> ?
EmptyPartition<TResult>.Instance :
new SelectIPartitionIterator<TSource, TResult>(partition, selector);
result = new SelectIPartitionIterator<TSource, TResult>(partition, selector);
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
}

private sealed partial class SelectEnumerableIterator<TSource, TResult> : IIListProvider<TResult>
Expand Down
5 changes: 5 additions & 0 deletions src/libraries/System.Linq/src/System/Linq/Select.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public static IEnumerable<TResult> Select<TSource, TResult>(
return iterator.Select(selector);
}

if (GetEmptyIfEmpty<TSource, TResult>(source) is IEnumerable<TResult> empty)
{
return empty;
}

if (source is IList<TSource> ilist)
{
if (source is TSource[] array)
Expand Down