From e5de095657e7ab5d885801e6419763ed7371b0f3 Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Wed, 3 Jan 2018 16:40:03 +0000 Subject: [PATCH] Use Span to drop byte[1] allocations (dotnet/coreclr#15680) Signed-off-by: dotnet-bot-corefx-mirror --- .../src/CoreLib/System/IO/PinnedBufferMemoryStream.cs | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/Common/src/CoreLib/System/IO/PinnedBufferMemoryStream.cs b/src/Common/src/CoreLib/System/IO/PinnedBufferMemoryStream.cs index 2bd1ef6b9537..dfcc05d066f6 100644 --- a/src/Common/src/CoreLib/System/IO/PinnedBufferMemoryStream.cs +++ b/src/Common/src/CoreLib/System/IO/PinnedBufferMemoryStream.cs @@ -29,19 +29,12 @@ internal PinnedBufferMemoryStream(byte[] array) { Debug.Assert(array != null, "Array can't be null"); - int len = array.Length; - // Handle 0 length byte arrays specially. - if (len == 0) - { - array = new byte[1]; - len = 0; - } - _array = array; _pinningHandle = GCHandle.Alloc(array, GCHandleType.Pinned); // Now the byte[] is pinned for the lifetime of this instance. // But I also need to get a pointer to that block of memory... - fixed (byte* ptr = &_array[0]) + int len = array.Length; + fixed (byte* ptr = &MemoryMarshal.GetReference((Span)array)) Initialize(ptr, len, len, FileAccess.Read); }