From 05642cbdc6dbce6983fdf32b3329f3db1e296024 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 3 Jan 2026 17:12:43 +0000 Subject: [PATCH] Use ArrayPool for temporary buffers in BinaryReaderExtensions Co-authored-by: adamhathcock <527620+adamhathcock@users.noreply.github.com> --- .../Polyfills/BinaryReaderExtensions.cs | 36 +++++++++++++------ 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/src/SharpCompress/Polyfills/BinaryReaderExtensions.cs b/src/SharpCompress/Polyfills/BinaryReaderExtensions.cs index d34771cf..dbf17c25 100644 --- a/src/SharpCompress/Polyfills/BinaryReaderExtensions.cs +++ b/src/SharpCompress/Polyfills/BinaryReaderExtensions.cs @@ -11,11 +11,18 @@ public static class BinaryReaderExtensions { public async Task ReadByteAsync(CancellationToken cancellationToken = default) { - var buffer = new byte[1]; - await reader - .BaseStream.ReadExactAsync(buffer, 0, 1, cancellationToken) - .ConfigureAwait(false); - return buffer[0]; + var buffer = ArrayPool.Shared.Rent(1); + try + { + await reader + .BaseStream.ReadExactAsync(buffer, 0, 1, cancellationToken) + .ConfigureAwait(false); + return buffer[0]; + } + finally + { + ArrayPool.Shared.Return(buffer); + } } public async Task ReadBytesAsync( @@ -23,11 +30,20 @@ public static class BinaryReaderExtensions CancellationToken cancellationToken = default ) { - var bytes = new byte[count]; - await reader - .BaseStream.ReadExactAsync(bytes, 0, count, cancellationToken) - .ConfigureAwait(false); - return bytes; + var buffer = ArrayPool.Shared.Rent(count); + try + { + await reader + .BaseStream.ReadExactAsync(buffer, 0, count, cancellationToken) + .ConfigureAwait(false); + var bytes = new byte[count]; + System.Array.Copy(buffer, 0, bytes, 0, count); + return bytes; + } + finally + { + ArrayPool.Shared.Return(buffer); + } } } }