This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use System.Buffers in System.IO.FileSystem
- Change FileStream to use ```ArrayPool<byte>.Shared``` for its internal buffer - Add to Common a helper implementation of CopyToAsync that uses ```ArrayPool<byte>.Shared``` until such type as the base Stream (in mscorlib) can use a pooled buffer - Utilize that CopyToAsync helper in FileStream Having added that: - Utilize that CopyToAsync helper in DeflateStream/GZipStream, as System.IO.Compression already depends on System.Buffers.
- Loading branch information
1 parent
e5066d3
commit 0a4bfb5
Showing
12 changed files
with
175 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// 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.Buffers; | ||
using System.Diagnostics; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace System.IO | ||
{ | ||
/// <summary>Provides methods to help in the implementation of Stream-derived types.</summary> | ||
internal static class StreamHelpers | ||
{ | ||
/// <summary> | ||
/// Provides an implementation usable as an override of Stream.CopyToAsync but that uses the shared | ||
/// ArrayPool for the intermediate buffer rather than allocating a new buffer each time. | ||
/// </summary> | ||
/// <remarks> | ||
/// If/when the base CopyToAsync implementation is changed to use a pooled buffer, | ||
/// this will no longer be necessary. | ||
/// </remarks> | ||
public static Task ArrayPoolCopyToAsync(Stream source, Stream destination, int bufferSize, CancellationToken cancellationToken) | ||
{ | ||
Debug.Assert(source != null); | ||
|
||
if (destination == null) | ||
{ | ||
throw new ArgumentNullException("destination"); | ||
} | ||
if (bufferSize <= 0) | ||
{ | ||
throw new ArgumentOutOfRangeException("bufferSize", bufferSize, SR.ArgumentOutOfRange_NeedPosNum); | ||
} | ||
if (!source.CanRead) | ||
{ | ||
throw new NotSupportedException(SR.NotSupported_UnreadableStream); | ||
} | ||
if (!destination.CanWrite) | ||
{ | ||
throw new NotSupportedException(SR.NotSupported_UnwritableStream); | ||
} | ||
|
||
byte[] buffer = ArrayPool<byte>.Shared.Rent(bufferSize); // rented here to reduce size of async state machine boxed object | ||
return ArrayPoolCopyToAsyncInternal(source, destination, buffer, cancellationToken); | ||
} | ||
|
||
private static async Task ArrayPoolCopyToAsyncInternal(Stream source, Stream destination, byte[] buffer, CancellationToken cancellationToken) | ||
{ | ||
try | ||
{ | ||
int bytesRead; | ||
while ((bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false)) != 0) | ||
{ | ||
await destination.WriteAsync(buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false); | ||
} | ||
} | ||
finally | ||
{ | ||
ArrayPool<byte>.Shared.Return(buffer); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters