microsoft/Microsoft.IO.RecyclableMemoryStream

Blocks list initial capacity

lbargaoanu opened this issue · 2 comments

Another small allocation :)
The initial capacity is one. Then, in the constructor, the capacity is increased to two (the default settings). This causes an allocation.
Perhaps leave the default (zero in .Net 5) and avoid the allocation? Or if one is hepful in other cases, start with zero just in the default case?

List<T> grows doubling the size of the array when it needs to grow.

When the length of the array is 0, the first growth is to 4.

With an initial length of 1, the list of blocks will grow to 2 before growing to the initial 4.

To prevent unneed allocations, the allocation of the List<byte[]> could happen on first use only. But that cost is only acceptable if it is expected that instances of the stream are created and "not used".

We don't let List use its own growth algorithm--it's always in terms of setting a specific capacity, and it always grows, so I think having an empty List to start makes sense. I think most streams would get used, so I wouldn't want to add overhead of checking for a null list everywhere.