mirror of
https://github.com/BepInEx/dotnet-runtime.git
synced 2026-11-25 10:58:20 -10:00
Copying between streams represents one of the larger sources of memory allocations and copying in HttpClient. HttpClient has its own stream-to-stream copy implementation, but there are ways we can significantly improve it based on typical usage within HttpClient. This commit does the following: - Previously it would special case when both the source and destination stream were MemoryStreams, but even in that case it would allocate a temporary buffer and do a Read/Write loop to copy between them, and even then it's relatively rare to copy from a MemoryStream to a MemoryStream. Instead, if the source stream is a MemoryStream and we can get its buffer, we can do a single write to the destination (regardless of what kind of stream it is), avoiding the need for multiple read/writes and for the temporary buffer. If we can't get its buffer but its length is known to be less than or equal to the requested buffer size and it's positioned at the beginning, we can simply ToArray the source and still do a single write to the target. - If the destination is the special LimitMemoryStream, used for buffering output, and if that LimitMemoryStream was pre-sized based on the response having a Content-Length, then rather than allocating a temporary buffer and repeatedly copying from the source stream into that buffer and copying from the buffer into the memory stream, we can instead give the destination's buffer directly to be read into by the source, avoiding the temporary buffer and the extra copies. This is a very common case. - If the source does not need to be disposed after the copy, rather than doing our own copy loop, we can instead ask the source to use its CopyToAsync implementation to copy to the destination stream. Worst case this will be exactly what we were previously doing, but best case the source has its own CopyToAsync override that's more efficient. - When constructing a LimitMemoryStream, if the Content-Length isn't known, we were explicitly passing a 0 capacity to the base, which causes it to allocate a new zero-length array; I've added a ctor to avoid that. Also avoids some unnecessary array allocations and copies while getting data from buffered content. Commit migrated from https://github.com/dotnet/corefx/commit/d2502ce05032ae83c5675e2cd4b27ca2fb6706fa
81 lines
3.1 KiB
C#
81 lines
3.1 KiB
C#
// Licensed to the .NET Foundation under one or more agreements.
|
|
// 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.IO;
|
|
using System.Reflection;
|
|
using System.Threading.Tasks;
|
|
using Xunit;
|
|
|
|
namespace System.Net.Http.Tests
|
|
{
|
|
public class HttpContentTest
|
|
{
|
|
[Fact]
|
|
public void Dispose_BufferContentThenDisposeContent_BufferedStreamGetsDisposed()
|
|
{
|
|
MockContent content = new MockContent();
|
|
content.LoadIntoBufferAsync().Wait();
|
|
|
|
Type type = typeof(HttpContent);
|
|
TypeInfo typeInfo = type.GetTypeInfo();
|
|
FieldInfo bufferedContentField = typeof(HttpContent).GetField("_bufferedContent",
|
|
BindingFlags.Instance | BindingFlags.NonPublic);
|
|
Assert.NotNull(bufferedContentField);
|
|
|
|
MemoryStream bufferedContentStream = bufferedContentField.GetValue(content) as MemoryStream;
|
|
Assert.NotNull(bufferedContentStream);
|
|
|
|
content.Dispose();
|
|
|
|
// The following line will throw an ObjectDisposedException if the buffered-stream was correctly disposed.
|
|
Assert.Throws<ObjectDisposedException>(() => { string str = bufferedContentStream.Length.ToString(); });
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(1, 100, 99, 1)]
|
|
[InlineData(1, 100, 50, 99)]
|
|
[InlineData(1, 100, 98, 98)]
|
|
[InlineData(1, 100, 99, 99)]
|
|
[InlineData(1, 100, 99, 98)]
|
|
[InlineData(3, 50, 100, 149)]
|
|
[InlineData(3, 50, 149, 149)]
|
|
public async Task LoadIntoBufferAsync_ContentLengthSmallerThanActualData_ActualDataLargerThanMaxSize_ThrowsException(
|
|
int numberOfWrites, int sizeOfEachWrite, int reportedLength, int maxSize)
|
|
{
|
|
Assert.InRange(maxSize, 1, (numberOfWrites * sizeOfEachWrite) - 1);
|
|
|
|
LieAboutLengthContent c = new LieAboutLengthContent(numberOfWrites, sizeOfEachWrite, reportedLength);
|
|
Task t = c.LoadIntoBufferAsync(maxSize);
|
|
await Assert.ThrowsAsync<HttpRequestException>(() => t);
|
|
}
|
|
|
|
private sealed class LieAboutLengthContent : HttpContent
|
|
{
|
|
private readonly int _numberOfWrites, _sizeOfEachWrite, _reportedLength;
|
|
|
|
public LieAboutLengthContent(int numberOfWrites, int sizeOfEachWrite, int reportedLength)
|
|
{
|
|
_numberOfWrites = numberOfWrites;
|
|
_sizeOfEachWrite = sizeOfEachWrite;
|
|
_reportedLength = reportedLength;
|
|
}
|
|
|
|
protected override async Task SerializeToStreamAsync(Stream stream, TransportContext context)
|
|
{
|
|
byte[] bytes = new byte[_sizeOfEachWrite];
|
|
for (int i = 0; i < _numberOfWrites; i++)
|
|
{
|
|
await stream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
|
|
}
|
|
}
|
|
|
|
protected internal override bool TryComputeLength(out long length)
|
|
{
|
|
length = _reportedLength;
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
}
|