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

Use Array.Empty<T> for LINQ's Enumerable.Empty<TResult> #2290

Merged
merged 1 commit into from Jul 9, 2015

Conversation

@justinvp
Copy link
Collaborator

justinvp commented Jul 9, 2015

No description provided.

@@ -1452,7 +1452,10 @@ private static IEnumerable<TResult> RepeatIterator<TResult>(TResult element, int

public static IEnumerable<TResult> Empty<TResult>()
{
return EmptyEnumerable<TResult>.Instance;
// This is a more memory-intensive empty enumerable that allocates a new enumerator each time.

This comment has been minimized.

@stephentoub

stephentoub Jul 9, 2015 Member

I don't believe this comment is accurate any more. Enumerating an empty array should not allocate a new enumerator: https://github.com/dotnet/coreclr/blob/master/src/mscorlib/src/System/Array.cs#L2717

This comment has been minimized.

@justinvp

justinvp Jul 9, 2015 Author Collaborator

Excellent! I'll remove the comment.

This comment has been minimized.

@justinvp

justinvp Jul 9, 2015 Author Collaborator

As an aside, correct me if I am wrong, but it looks like SZGenericArrayEnumerator<T>.Empty is being greedily allocated as soon as SZGenericArrayEnumerator<T> is used for the first time (first time the generic GetEnumerator is called). Which means lots of possible unnecessary empty enumerator allocations for each T. Wouldn't it be better if this was lazily allocated only when an empty enumerator was actually needed? Something like EmptySZGenericArrayEnumerator<T>.Instance. (I can open an issue/PR on CoreCLR to continue the discussion there, if warranted).

This comment has been minimized.

@stephentoub

stephentoub Jul 9, 2015 Member

Yeah, that's probably a good idea.

This comment has been minimized.

@justinvp

justinvp Jul 9, 2015 Author Collaborator

Never mind -- I tested it out and the runtime is doing the right thing.

@justinvp justinvp force-pushed the justinvp:linqempty branch from fe29b64 to 1dc7cf8 Jul 9, 2015
@stephentoub
Copy link
Member

stephentoub commented Jul 9, 2015

cc: @VSadov

@justinvp justinvp force-pushed the justinvp:linqempty branch from 1dc7cf8 to 8e51ee0 Jul 9, 2015
@VSadov
Copy link
Member

VSadov commented Jul 9, 2015

LGTM

stephentoub added a commit that referenced this pull request Jul 9, 2015
Use Array.Empty<T> for LINQ's Enumerable.Empty<TResult>
@stephentoub stephentoub merged commit b219b8d into dotnet:master Jul 9, 2015
1 check passed
1 check passed
default Build finished. No test results found.
Details
@justinvp justinvp deleted the justinvp:linqempty branch Oct 1, 2015
@karelz karelz modified the milestone: 1.0.0-rtm Dec 3, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

5 participants
You can’t perform that action at this time.