Open polijp opened 2 years ago
Tagging subscribers to this area: @dotnet/area-system-collections See info in area-owners.md if you want to be subscribed.
There is a risk when the collection is changed, either we have to refresh the length of the ReadOnlySpan that points on the keys (or the values), either we decide it keeps the length of the collection when it was created.
It's not possible to change the length of a span instance after construction (just like arrays). Also, the same risk of contents changing already exists with AsSpan<T>(List<T>)
, you simply have to know what you're doing.
[API Proposal]:
missing the title?
To guarantee the sorted aspect, one will gets a ReadOnlySpan.
The span of values wouldn't have to be read-only because the values won't affect sorting.
Note this wouldn't just be adding a new API... it would require moving SortedList<>
from System.Collections.dll to System.Private.CoreLib.dll.
If moving SortedList<> is a big problem, can't we use [InternalsVisibleTo] and change the fields to internal?
can't we use [InternalsVisibleTo] and change the fields to internal
No, even if we were ok using InternalsVisibleTo (which we avoid), corelib can't reference collections.dll, it's the other way around.
Also, once we stop caring about BinaryFormatter and thus the impact of changes on serialization compat, we could very likely be interested in changing how data is stored in SortedList, but a method like this adds back such constraints.
I think such a method would need to yield very significant benefits to consider adding it, given what it costs in flexibility.
we could very likely be interested in changing how data is stored in SortedList, but a method like this adds back such constraints.
I guess it was ok for CollectionsMarshal.AsSpan
for list which we would never change internally to some other data structure, but for dictionaries/sortinglist it may happen, e.g. some tree-based
Background and motivation
.net 6 introduced CollectionsMarshall to access a List elements as a Span or to access to a value of a dictionary with a ref struct. These features are really useful in mathematical computing that needs high performances.
The idea here consists in expanding CollectionsMarshall with SortedList that maintains two internal arrays (one for the keys, one for the values). To guarantee the sorted aspect, one will gets a ReadOnlySpan. It will allow to access the keys and/or the values and browse them quicker than with the Keys and Values Properties.
API Proposal
API Usage
Alternative Designs
No response
Risks
There is a risk when the collection is changed, either we have to refresh the length of the ReadOnlySpan that points on the keys (or the values), either we decide it keeps the length of the collection when it was created.