From 5395b14eb4b924419b3d4732582a22ac72571d9f Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Wed, 15 Apr 2020 16:52:22 -0400 Subject: [PATCH] Remove unnecessary MemoryStream allocation from ByteArrayContent (#35005) Constructing a ByteArrayContent (or anything that derives from it, like StringContent) or a ReadOnlyMemoryContent proactively allocates a MemoryStream that it stores as the _bufferedContent on the base HttpContent. But the 99.99% use case for ByteArrayContent is as a request payload, and this _bufferedContent isn't used for that. The only time this would be used is if a developer created a ByteArrayContent and then explicitly got the stream from it, e.g. via GetStreamAsync, in which case we're no worse off by doing the creation lazily. This PR just deletes the unnecessary code / allocation. --- .../System.Net.Http/src/System/Net/Http/ByteArrayContent.cs | 5 ----- .../System.Net.Http/src/System/Net/Http/HttpContent.cs | 5 ----- .../src/System/Net/Http/ReadOnlyMemoryContent.cs | 11 +---------- 3 files changed, 1 insertion(+), 20 deletions(-) diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/ByteArrayContent.cs b/src/libraries/System.Net.Http/src/System/Net/Http/ByteArrayContent.cs index 9b1e9b0..e4d9cca 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/ByteArrayContent.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/ByteArrayContent.cs @@ -2,7 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System.Diagnostics; using System.IO; using System.Threading; using System.Threading.Tasks; @@ -25,8 +24,6 @@ namespace System.Net.Http _content = content; _offset = 0; _count = content.Length; - - SetBuffer(_content, _offset, _count); } public ByteArrayContent(byte[] content, int offset, int count) @@ -47,8 +44,6 @@ namespace System.Net.Http _content = content; _offset = offset; _count = count; - - SetBuffer(_content, _offset, _count); } protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context) => diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpContent.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpContent.cs index 2ba2272..9575952 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpContent.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpContent.cs @@ -131,11 +131,6 @@ namespace System.Net.Http get { return _bufferedContent != null; } } - internal void SetBuffer(byte[] buffer, int offset, int count) - { - _bufferedContent = new MemoryStream(buffer, offset, count, writable: false, publiclyVisible: true); - } - internal bool TryGetBuffer(out ArraySegment buffer) { if (_bufferedContent != null) diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/ReadOnlyMemoryContent.cs b/src/libraries/System.Net.Http/src/System/Net/Http/ReadOnlyMemoryContent.cs index b2140a9..d4b91c2 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/ReadOnlyMemoryContent.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/ReadOnlyMemoryContent.cs @@ -3,7 +3,6 @@ // See the LICENSE file in the project root for more information. using System.IO; -using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; @@ -13,16 +12,8 @@ namespace System.Net.Http { private readonly ReadOnlyMemory _content; - public ReadOnlyMemoryContent(ReadOnlyMemory content) - { + public ReadOnlyMemoryContent(ReadOnlyMemory content) => _content = content; - if (MemoryMarshal.TryGetArray(content, out ArraySegment array)) - { - // If we have an array, allow HttpClient to take optimized paths by just - // giving it the array content to use as its already buffered data. - SetBuffer(array.Array!, array.Offset, array.Count); - } - } protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context) => stream.WriteAsync(_content).AsTask(); -- 2.7.4