Change delegation in {Unmanaged}MemoryStream.Read/WriteAsync(Memory)
authorStephen Toub <stoub@microsoft.com>
Thu, 14 Sep 2017 15:35:23 +0000 (08:35 -0700)
committerStephen Toub <stoub@microsoft.com>
Thu, 14 Sep 2017 15:35:23 +0000 (08:35 -0700)
commitaaacca7ed5b8582eccfc259f81e1eb25d95d81be
treeb96894f7629cb1d6b817cd4d9f14b360b6b2e1d4
parent81adc9de83fd4f16d54b0d2f8b4ad739bd675482
Change delegation in {Unmanaged}MemoryStream.Read/WriteAsync(Memory)

Read/WriteAsync(Memory) on MemoryStream and UnmanagedMemoryStream need to delegate to one of the existing virtual methods, in case an existing stream has overridden the virtuals in order to change or augment the behavior (e.g. checking on each write to ensure the length doesn't exceed some amount).  Currently these delegate to the synchronous Read/Write(Span) methods.  The problem with that is, for exactly the case where there is a derived class, Read/Write(Span) themselves need to delegate to Read/Write(byte[]), which means they use ArrayPool and copy.  But with a {ReadOnly}Memory, we may already have access to the underlying array, in which case we're going from an array to a span and back to different rented array along with an unnecessary copy.  To address that, this commit changes the delegation to prefer Read/Write(byte[],...) if possible, falling back to Read/Write(Span) only if we couldn't get an array from the Memory.
src/mscorlib/shared/System/IO/UnmanagedMemoryStream.cs
src/mscorlib/src/System/IO/MemoryStream.cs