Use Span to drop byte[1] allocations (#15680)
authorBen Adams <thundercat@illyriad.co.uk>
Wed, 3 Jan 2018 16:40:03 +0000 (16:40 +0000)
committerJan Kotas <jkotas@microsoft.com>
Wed, 3 Jan 2018 16:40:03 +0000 (08:40 -0800)
src/mscorlib/shared/System/IO/PinnedBufferMemoryStream.cs
src/mscorlib/src/System/StubHelpers.cs

index 2bd1ef6..dfcc05d 100644 (file)
@@ -29,19 +29,12 @@ namespace System.IO
         {
             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<byte>)array))
                 Initialize(ptr, len, len, FileAccess.Read);
         }
 
index bf19c7f..fb19604 100644 (file)
@@ -46,7 +46,7 @@ namespace System.StubHelpers
 
         static internal char ConvertToManaged(byte nativeChar)
         {
-            byte[] bytes = new byte[1] { nativeChar };
+            Span<byte> bytes = new Span<byte>(ref nativeChar, 1);
             string str = Encoding.Default.GetString(bytes);
             return str[0];
         }