Skip to content

Commit

Permalink
Use non-memory overload to prevent internal reallocation
Browse files Browse the repository at this point in the history
  • Loading branch information
ScrubN committed Dec 14, 2023
1 parent d814a76 commit 4cf838a
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions TwitchDownloaderCore/Extensions/StreamExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,22 @@ public static async Task ProgressCopyToAsync(this Stream source, Stream destinat
}

var rentedBuffer = ArrayPool<byte>.Shared.Rent(STREAM_DEFAULT_BUFFER_LENGTH);
var buffer = rentedBuffer.AsMemory(0, STREAM_DEFAULT_BUFFER_LENGTH);

long totalBytesRead = 0;
try
{
int bytesRead;
while ((bytesRead = await source.ReadAsync(buffer, cancellationToken).ConfigureAwait(false)) != 0)
while ((bytesRead = await source.ReadAsync(rentedBuffer, 0, rentedBuffer.Length, cancellationToken).ConfigureAwait(false)) != 0)
{
await destination.WriteAsync(buffer[..bytesRead], cancellationToken).ConfigureAwait(false);
await destination.WriteAsync(rentedBuffer, 0, bytesRead, cancellationToken).ConfigureAwait(false);

totalBytesRead += bytesRead;
progress.Report(new StreamCopyProgress(sourceLength.Value, totalBytesRead));
}
}
finally
{
ArrayPool<byte>.Shared.Return(rentedBuffer);
ArrayPool<byte>.Shared.Return(rentedBuffer, true);
}
}
}
Expand Down

0 comments on commit 4cf838a

Please sign in to comment.