Use Array.Empty<T> for LINQ's Enumerable.Empty<TResult>#2290
Conversation
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Excellent! I'll remove the comment.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
Yeah, that's probably a good idea.
There was a problem hiding this comment.
Never mind -- I tested it out and the runtime is doing the right thing.
|
cc: @VSadov |
|
LGTM |
Use Array.Empty<T> for LINQ's Enumerable.Empty<TResult>
Use Array.Empty<T> for LINQ's Enumerable.Empty<TResult> Commit migrated from dotnet/corefx@b219b8d
No description provided.