[Mono-dev] Should we replace MemoryStream?

Robert Jordan robertj at gmx.net
Tue Nov 10 12:42:50 EST 2009


Avery Pennarun wrote:
> On Tue, Nov 10, 2009 at 11:24 AM, Robert Jordan <robertj at gmx.net>
> wrote:
>> Right, but MemoryStream is pretty prevalent and one of its frequent
>>  usage pattern is:
>> 
>> var ms = new MemoryStream () or MemoryStream(somepredictedsize); //
>> fill ms with some stream APIs ms.Close (); var bytes = ms.GetBuffer
>> (); // pass `bytes' to byte[] APIs (e.g. unmanaged world)
> 
> But my argument is that your line
> 
> // fill ms with some stream APIs
> 
> might or might not result in the array being reallocated even in the 
> *naive* implementation.  Each reallocation will cause a copy of the 
> entire buffer every time.
> 
> Conversely, a chunked implementation would reallocate-and-copy the 
> data at most once, when you call GetBuffer().  So it is strictly 
> equal-or-better than the naive implementation, in terms of 
> reallocations and copies.

An algorithm based on a MemoryStream implemented with chunks will
perform better in average. I fully agree with that.

The problem is that one method (GetBuffer) *will be* unexpected
slower, and another one, much harder to fix: it is allowed to change
the buffer even before the stream has been closed. This means that
after every GetBuffer call, the implementation must behave differently
because it must somehow deal with a changed underlying buffer.

Robert



More information about the Mono-devel-list mailing list