Replace heap-allocated temporary byte arrays with ArrayPool<byte>.Shared rentals in CipherStream and Streams to reduce GC pressure on hot I/O paths#664
Open
paulomorgado wants to merge 1 commit intobcgit:masterfrom
Conversation
Use ArrayPool<byte> for temporary buffers in CipherStream and Streams Replace heap-allocated temporary byte arrays with ArrayPool<byte>.Shared rentals to reduce GC pressure on hot paths. All rented buffers are returned with clearArray: true to maintain existing security-clearing behavior. CipherStream: - Write(byte[], int, int): ArrayPool under NETCOREAPP2_0_OR_GREATER - WriteAsync(byte[], int, int): ArrayPool with async ownership transfer - Write(ReadOnlySpan<byte>): stackalloc/ArrayPool instead of stackalloc/new - WriteAsync(ReadOnlyMemory<byte>): ArrayPool with async ownership transfer - Dispose: stackalloc/ArrayPool instead of stackalloc/new Streams: - CopyTo: stackalloc/ArrayPool instead of stackalloc/new - CopyToAsync: ArrayPool instead of new byte[] - ReadAsyncCompletion: ArrayPool, deferred into async method body - WriteAsyncCompletion: ArrayPool, deferred into async method body
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Describe your changes
Replace heap-allocated temporary byte arrays with
ArrayPool<byte>.Sharedrentals inCipherStreamandStreamsto reduce GC pressure on hot I/O paths.Both
CipherStreamandStreamsare used heavily in cryptographic I/O pipelines where temporary buffers are allocated on every read/write call. These short-lived allocations put unnecessary pressure on the garbage collector.ArrayPool<byte>.Sharedavoids this by reusing buffers from a shared pool.CipherStream.csWrite(byte[], int, int)new byte[outputSize]ArrayPool.Rent/ReturnunderNETCOREAPP2_0_OR_GREATERWriteAsync(byte[], int, int)new byte[outputSize]ArrayPool.Rentwith async ownership transfer viaWriteAsyncArrayPoolCompletionWrite(ReadOnlySpan<byte>)stackalloc/new byte[]stackalloc/ArrayPool.RentWriteAsync(ReadOnlyMemory<byte>)new byte[outputSize]ArrayPool.Rentwith async ownership transfer viaWriteAsyncCompletionDispose(bool)stackalloc/new byte[]stackalloc/ArrayPool.RentStreams.csCopyTostackalloc/new byte[]stackalloc/ArrayPool.RentCopyToAsyncnew byte[bufferSize]ArrayPool.Rent/ReturnReadAsyncCompletionnew byte[buffer.Length]ArrayPool.Rent, deferred into async method bodyWriteAsyncCompletionbuffer.ToArray()ArrayPool.Rent, deferred into async method bodyDesign notes
finallyblock. The synchronousfinallyonly returns the buffer if no async handoff occurred (checked vialength == 0).stackalloc/ArrayPoolpattern: For synchronous Span-based methods, small buffers still usestackallocfor zero-allocation fast paths. Only the large-buffer fallback usesArrayPool(previouslynew byte[]).clearArray: true: AllArrayPool.Returncalls useclearArray: trueto zero sensitive cryptographic data before the buffer is returned to the pool, matching the previousArray.Clear/Span.Fill(0x00)behavior.#if NETCOREAPP2_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER, falling back to the originalnew byte[]allocation for older targets.How has this been tested?
Build verified across all target frameworks (
net461,netstandard2.0,net6.0) with 0 errors.Checklist before requesting a review
See also Contributing Guidelines.