You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Api Proposal] Add List<T> AsSpan to CollectionsMarshal #27061
changed the title [-][Api Proposal] Add List<T>.AsSpan to MemoryMarshal [/-][+][Api Proposal] Add List<T>.AsSpan to CollectionsMarshal[/+]on Aug 4, 2018
curious, do you happen to have a motivating scenario?
List<T> is a very convenient datastructure for creating an array of an unknown size. However, to use it as an array you need to copy the entire contents again with .ToArray().
AsSpan/AsMemory would give the "array" correctly bounded (size of List rather than size of backing array); without the copy.
Would it make sense to propose a "ListBuilder"/"SpanBuilder" type then? It could have roughly the same surface area as List, along with an AsSpan/AsMemory method. Once those are called, mutation is no longer allowed.
@benaadams We understand that this a logical extension of the current implementation of the type. Could you list some compelling scenarios? We can always come up with potential scenarios.
Creating a Memory/Span (Array-like) of an unknown upfront size. List<T> is currently the main builder type; but getting an array from it .ToArray() causes a secondary allocation.
I missed this when I had a list and wanted to do something (been a while... can't remember what the exact situation was) which could be vectorised (since you need to take pointer of the underlying array). It didn't have to be exposed as a Span... but just my thought.
changed the title [-][Api Proposal] Add List<T>.AsSpan to CollectionsMarshal[/-][+][Api Proposal] Add List<T> AsSpan to CollectionsMarshal[/+]on Aug 31, 2019
namespaceSystem.Runtime.InteropServices{publicpartialstaticclassCollectionsMarshal{publicstaticSpan<T>AsSpan(List<T>list);publicstaticReadOnlySpan<T>AsReadOnlySpan(List<T>list);// We don't want this one:// public Memory<T> AsMemory(List<T> list);}}
Reacted by Tanner Gooding, Joe4evr, antiufo, Adam Sitnik and NetMage
The “approved shape” wasn’t completely implemented with dotnet/coreclr#26867. Should we update the “approved shape” above so it is in sync with the code?
Re-flagging as api-ready-for-review to address the AsReadOnlySpan change and also do a quick review on the nullable annotations change (see dotnet/coreclr#26903).
namespaceSystem.Runtime.InteropServices{publicpartialstaticclassCollectionsMarshal{// Updated with nullable annotationpublicstaticSpan<T>AsSpan(List<T>?list);// Since the AsSpan(...) overload is no longer changing the internal version,// we don't need this one anymore// public static ReadOnlySpan<T> AsReadOnlySpan(List<T> list);// We don't want this one:// public Memory<T> AsMemory(List<T> list);}}
Activity
[-]Add List<T>.AsSpan to MemoryMarshal [/-][+][Api Proposal] Add List<T>.AsSpan to MemoryMarshal [/+]jkotas commentedon Aug 4, 2018
I do not think this should be on MemoryMarshal. This should be on a new one, like CollectionsMarshal.
This should not be an extension method. No other methods on
*Marshalare extension methods to make you do more typing.The PowerCollections in corefxlab would be a good place to experiment with this: dotnet/corefxlab#2406
[-][Api Proposal] Add List<T>.AsSpan to MemoryMarshal [/-][+][Api Proposal] Add List<T>.AsSpan to CollectionsMarshal[/+]benaadams commentedon Aug 4, 2018
Updated
Would then need to go via private reflection?
danmoseley commentedon Aug 15, 2018
@jkotas is reflection what you have in mind?
@benaadams curious, do you happen to have a motivating scenario?
benaadams commentedon Aug 15, 2018
List<T>is a very convenient datastructure for creating an array of an unknown size. However, to use it as an array you need to copy the entire contents again with.ToArray().AsSpan/AsMemorywould give the "array" correctly bounded (size of List rather than size of backing array); without the copy.jkotas commentedon Aug 15, 2018
I had in mind all the different list and array builders in "Common". They are all variants of List with different performance characteristics.
benaadams commentedon Aug 15, 2018
All
internalthough 😢morganbr commentedon Aug 15, 2018
Would it make sense to propose a "ListBuilder"/"SpanBuilder" type then? It could have roughly the same surface area as List, along with an
AsSpan/AsMemorymethod. Once those are called, mutation is no longer allowed.danmoseley commentedon Aug 15, 2018
@ahsonkhan
danmoseley commentedon May 30, 2019
@safern this does not seem to me essential in order to ship 3.0. Please correct me if I'm wrong.
Gnbrkm41 commentedon Aug 2, 2019
any updates, just wondering? Sounds like a great API 😃
terrajobst commentedon Aug 27, 2019
@benaadams We understand that this a logical extension of the current implementation of the type. Could you list some compelling scenarios? We can always come up with potential scenarios.
benaadams commentedon Aug 27, 2019
Creating a Memory/Span (Array-like) of an unknown upfront size.
List<T>is currently the main builder type; but getting an array from it.ToArray()causes a secondary allocation.The alternative would be to expose an alternative builder type https://github.com/dotnet/corefx/issues/31597#issuecomment-413059736
TylerBrinkley commentedon Aug 27, 2019
FYI, it's currently being discussed here.
Gnbrkm41 commentedon Aug 27, 2019
I missed this when I had a list and wanted to do something (been a while... can't remember what the exact situation was) which could be vectorised (since you need to take pointer of the underlying array). It didn't have to be exposed as a Span... but just my thought.
[-][Api Proposal] Add List<T>.AsSpan to CollectionsMarshal[/-][+][Api Proposal] Add List<T> AsSpan to CollectionsMarshal[/+]terrajobst commentedon Sep 24, 2019
Video
Approved shape:
ahsonkhan commentedon Sep 25, 2019
Re-opening until API is exposed from the ref assembly.
eerhardt commentedon Sep 25, 2019
The “approved shape” wasn’t completely implemented with dotnet/coreclr#26867. Should we update the “approved shape” above so it is in sync with the code?
ahsonkhan commentedon Sep 26, 2019
Re-flagging as api-ready-for-review to address the AsReadOnlySpan change and also do a quick review on the nullable annotations change (see dotnet/coreclr#26903).
terrajobst commentedon Oct 1, 2019
Video
We decided to not increment the version, which is why we removed the
AsReadOnlySpan()overload.