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

Conversation

stephentoub
Copy link
Member

This also makes Enumerable.ToArray more inlineable and removes a bit of duplicated code.

This also makes Enumerable.ToArray more inlineable and removes a bit of duplicated code.
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-linq
See info in area-owners.md if you want to be subscribed.

}

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.

@stephentoub stephentoub merged commit 785d62b into dotnet:main Mar 25, 2024
87 checks passed
@stephentoub stephentoub deleted the distincttoarray branch March 25, 2024 16:22
@github-actions github-actions bot locked and limited conversation to collaborators Apr 25, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants