-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Add ArrayBuilder<T> #37852
Comments
Can you explain the ways in which this API serves as a better array builder than |
@svick There are several cases in which using
|
I'm not sure I like the idea of having all of these different APIs on a "builder" type that's not meant to serve as an exchange type. It seems the primary benefit of this over T[] arr = new T[capacity];
for (int i = 0; i < arr.Length; i++)
{
arr[i] = GetOrCreateElementToAdd();
}
/* use arr */ Or, if you know the max capacity but aren't quite sure you can fill it: T[] arr = new T[maxCapacity];
int i = 0;
while (needToAddAnItem)
{
arr[i++] = GetOrCreateElementToAdd();
}
Array.Resize(ref arr, i);
/* use arr */ What am I missing? |
Why wouldn't it serve as an exchange type? You could pass it into anything that knows how to You could certainly cut down the API by not implementing
There's a secondary benefit of getting rid of There seemed to be a fairly good consensus during the review of that API that people want a growable array kind of type.
You left out the scenario where you underestimate the capacity rather than overestimate it. You can still get some benefit out in that scenario if what you want out is a |
Yes, I'm aware of the problems of the Given this state of the world, I could see a simpler solution for your scenario: class CollectionsMarshal
{
public static ArraySegment<T> GetArraySegmentAndReset(List<T> list);
} The implementation gives you back the |
I've heard that the API review committee does not like to reconsider its decisions. It's not strictly true that it doesn't reconsider them, though. I'm giving some external push-back and proposing a safe API that solves the motivating use case. Unless there's some other reason that Of course, |
Thinking about this a bit more, I believe But I think |
That's #27062. |
@GrabYourPitchforks I remember hearing about "something something ... new members on I think I like both the original dedicated Edit: Is it OK for the new APIs to use |
Wouldn't edit Memory hides that your getting an array unless you ask it to then copy. So yeh ArraySegment is probably better. |
Background and Motivation
Today people use
List<T>
as an array builder, but it isn't a very good one. For .NET 5, there is an API (#27061) being added to expose the innards ofList<T>
in order to make it a slightly better array builder. It would be better to have an actual array builder!Proposed API
Alternative Designs
System.Collections.Generic.ArrayBuilder<T>
actually already exists today as aninternal struct
, but its current form is probably not what you would want from a public API surface. This API doesn't propose to replace usages of the existingArrayBuilder<T>
, which is a mutablestruct
and elides checks in favor of performance. The internalArrayBuilder<T>
shouldn't take the good name and namespace, so it would be renamed.System.Collections.Immutable.ImmutableArray<T>.Builder
could be copied almost wholesale.See Also
Postscript
If this could be done before .NET 5 ships, I would suggest that
System.Runtime.InteropServices.CollectionsMarshal.AsSpan<T>(List<T>? list)
be removed. The motivating use case was "List is a very convenient datastructure for creating an array of an unknown size."¹ and this would solve that use case.cc: @GrabYourPitchforks
The text was updated successfully, but these errors were encountered: