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
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
|
We actually went to some trouble to remove all usage of ArrayPool.Shared, because a buffer you get from the pool is not guaranteed to have been released by all other callers (for the simple reason that one can call Return and yet retain the array). I was actually a bit shocked to realise that Stream silently exposes one's data in this way. Possibly an internal (to BC), private (so that we can relatively guarantee release) pool could work, but even a per-library instance like this (possibly shared across separate user contexts) would be a bit concerning; I would probably prefer to see explicit security contexts of some kind, within (each of) which pooling would then be considered locally safe. Happy to hear other opinions. |
|
When that happens, it will cause issues for a lot of things in .NET. Including BC, because you have no control on the usage of Using a dedicated |
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.