From: Ben Adams Date: Wed, 3 Jan 2018 16:40:03 +0000 (+0000) Subject: Use Span to drop byte[1] allocations (#15680) X-Git-Tag: accepted/tizen/base/20180629.140029~233 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=097e68658c5249eaefff33bd92b044e9ba22c819;p=platform%2Fupstream%2Fcoreclr.git Use Span to drop byte[1] allocations (#15680) --- diff --git a/src/mscorlib/shared/System/IO/PinnedBufferMemoryStream.cs b/src/mscorlib/shared/System/IO/PinnedBufferMemoryStream.cs index 2bd1ef6..dfcc05d 100644 --- a/src/mscorlib/shared/System/IO/PinnedBufferMemoryStream.cs +++ b/src/mscorlib/shared/System/IO/PinnedBufferMemoryStream.cs @@ -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)array)) Initialize(ptr, len, len, FileAccess.Read); } diff --git a/src/mscorlib/src/System/StubHelpers.cs b/src/mscorlib/src/System/StubHelpers.cs index bf19c7f..fb19604 100644 --- a/src/mscorlib/src/System/StubHelpers.cs +++ b/src/mscorlib/src/System/StubHelpers.cs @@ -46,7 +46,7 @@ namespace System.StubHelpers static internal char ConvertToManaged(byte nativeChar) { - byte[] bytes = new byte[1] { nativeChar }; + Span bytes = new Span(ref nativeChar, 1); string str = Encoding.Default.GetString(bytes); return str[0]; }