Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Use _emptyArray in List<T>.ToArray - #2758

Merged
stephentoub merged 1 commit into
dotnet:masterfrom
stephentoub:list_toarray
Jan 21, 2016
Merged

Use _emptyArray in List<T>.ToArray#2758
stephentoub merged 1 commit into
dotnet:masterfrom
stephentoub:list_toarray

Conversation

@stephentoub

Copy link
Copy Markdown
Member

I've only added this to List<T> in mscorlib as it's the only one of the collections with a ToArray that's exposed out (I've not touched legacy collections like ArrayList). The other changes for the other collections are in corefx in dotnet/corefx#5562.

cc: @jkotas, @ellismg
https://github.com/dotnet/corefx/issues/2363

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Did you consider adding a method like the following:

T[] Allocate<T>(int size) 
{
#if FEATURE_CORCLR
  if (size == 0) { return Array.Empty<T>(); }
#endif
  return new T[size];
}

This could then become a drop in replacement for new T[size] in places where Array.Empty<T> was acceptable.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Interesting idea. Though in a case like this (and I expect others) you'd end up doing something like:

T[] array = Allocate(_size);
Array.Copy(_items, 0, array, 0, _size);
return array;

and you'd be doing a bit of extra work unnecessarily with the Array.Copy call.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Agree there would be some extra work here in copy. Feel like more Array.Empty<T> overall would get more use if we have a clean, simple way to have allocations return it automatically.

@stephentoub stephentoub changed the title Use Array.Empty<T> in List<T>.ToArray Use _emptyArray in List<T>.ToArray Jan 21, 2016
@justinvp

Copy link
Copy Markdown

Is dotnet/corefx#5562 going to be updated to return _emptyArray from List<T>.ToArray() instead of Array.Empty<T>() as well?

@stephentoub

Copy link
Copy Markdown
Member Author

Yes, I just haven't had a chance yet.

@jkotas

jkotas commented Jan 21, 2016

Copy link
Copy Markdown
Member

LGTM

stephentoub added a commit that referenced this pull request Jan 21, 2016
Use _emptyArray in List<T>.ToArray
@stephentoub
stephentoub merged commit 9a43c44 into dotnet:master Jan 21, 2016
@stephentoub
stephentoub deleted the list_toarray branch January 21, 2016 12:52
@justinvp

Copy link
Copy Markdown

In dotnet/corefx#2363, @terrajobst says:

We bring this change to .NET Framework, but we’ll quirk it. Only customers targeting the latest version will get the new behavior.

I see it is within #if FEATURE_CORECLR here. Will there be another PR that adds the behavior to .NET Framework under an AppContext switch?

@stephentoub

Copy link
Copy Markdown
Member Author

Will there be another PR that adds the behavior to .NET Framework under an AppContext switch?

This isn't the code for the full framework. I expect when it's ported back, a switch will be used.

@justinvp

Copy link
Copy Markdown

Ah, I was under the impression the code was shared -- didn't realize a port step would be necessary. Thanks!

@stephentoub

Copy link
Copy Markdown
Member Author

Unfortunately. 😦 At least with the current state of things. It could change.

ellismg added a commit that referenced this pull request Jan 21, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants