Use pooled memory streams for temporary production buffers

This commit is contained in:
Adam Hathcock
2026-10-16 15:44:44 +01:00
parent 2369f10b63
commit 7fc5c0e565
7 changed files with 17 additions and 13 deletions

View File

@@ -25,7 +25,7 @@ public partial class ArcLzwStream
if (!_processed)
{
using var limited = new ReadOnlySubStream(_stream, _compressedSize);
using var data = new MemoryStream();
using var data = new PooledMemoryStream();
await limited.CopyToAsync(data, 81920, cancellationToken).ConfigureAwait(false);
FinishDecompression(data);
}

View File

@@ -191,7 +191,7 @@ public partial class ArcLzwStream : Stream
if (!_processed)
{
using var limited = new ReadOnlySubStream(_stream, _compressedSize);
using var data = new MemoryStream();
using var data = new PooledMemoryStream();
limited.CopyTo(data);
FinishDecompression(data);
}
@@ -201,7 +201,7 @@ public partial class ArcLzwStream : Stream
return result;
}
private void FinishDecompression(MemoryStream data)
private void FinishDecompression(PooledMemoryStream data)
{
if (data.Length != _compressedSize)
{

View File

@@ -202,7 +202,8 @@ public class PpmdStream : Stream, IAsyncDisposable
_isDisposed = true;
if (_compress)
{
await _model.EncodeBlockAsync(_stream, new MemoryStream(), true).ConfigureAwait(false);
using var input = new PooledMemoryStream();
await _model.EncodeBlockAsync(_stream, input, true).ConfigureAwait(false);
}
_modelH?.Dispose();
_modelH = null;

View File

@@ -2,6 +2,7 @@ using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using SharpCompress.IO;
namespace SharpCompress.Compressors.Shrink;
@@ -28,7 +29,7 @@ internal partial class ShrinkStream : Stream
// Read actual compressed data from the stream rather than pre-allocating based on the
// declared compressed size, which may be crafted to cause an OutOfMemoryException.
// The stream is already bounded by ReadOnlySubStream in ZipFilePart.
using var srcMs = new MemoryStream();
using var srcMs = new PooledMemoryStream();
await _inStream.CopyToAsync(srcMs, 81920, cancellationToken).ConfigureAwait(false);
var src = srcMs.ToArray();
// Decompress synchronously (CPU-bound operation).

View File

@@ -1,6 +1,7 @@
using System;
using System.IO;
using SharpCompress.Common;
using SharpCompress.IO;
namespace SharpCompress.Compressors.Shrink;
@@ -52,7 +53,7 @@ internal partial class ShrinkStream : Stream
// Read actual compressed data from the stream rather than pre-allocating based on the
// declared compressed size, which may be crafted to cause an OutOfMemoryException.
// The stream is already bounded by ReadOnlySubStream in ZipFilePart.
using var srcMs = new MemoryStream();
using var srcMs = new PooledMemoryStream();
_inStream.CopyTo(srcMs);
var src = srcMs.ToArray();
Decode(src);

View File

@@ -53,7 +53,7 @@ public partial class SevenZipWriter
var filesInfo = new SevenZipFilesInfoWriter { Entries = entries.ToArray() };
// Write header to a temporary stream first
using var headerStream = new MemoryStream();
using var headerStream = new PooledMemoryStream();
ArchiveHeaderWriter.WriteRawHeader(headerStream, mainStreamsInfo, filesInfo);
// Optionally compress the header
@@ -68,7 +68,7 @@ public partial class SevenZipWriter
}
private async ValueTask WriteCompressedHeaderAsync(
MemoryStream rawHeaderStream,
PooledMemoryStream rawHeaderStream,
long endOfPackedData
)
{
@@ -95,7 +95,7 @@ public partial class SevenZipWriter
};
// Write encoded header to a second temporary stream
using var encodedHeaderStream = new MemoryStream();
using var encodedHeaderStream = new PooledMemoryStream();
ArchiveHeaderWriter.WriteEncodedHeader(encodedHeaderStream, headerStreamsInfo);
// Write the encoded header to the output
@@ -123,7 +123,7 @@ public partial class SevenZipWriter
}
private async ValueTask WriteRawHeaderToOutputAsync(
MemoryStream rawHeaderStream,
PooledMemoryStream rawHeaderStream,
long endOfPackedData
)
{

View File

@@ -5,6 +5,7 @@ using System.Threading.Tasks;
using SharpCompress.Common;
using SharpCompress.Common.Zip;
using SharpCompress.Common.Zip.Headers;
using SharpCompress.IO;
namespace SharpCompress.Writers.Zip;
@@ -24,7 +25,7 @@ public partial class ZipWriter
// Buffer the entire central directory + end record into memory, then write async.
// This avoids synchronous writes to the underlying stream during finalization.
using var ms = new MemoryStream();
using var ms = new PooledMemoryStream();
ulong size = 0;
foreach (var entry in entries)
{
@@ -149,9 +150,9 @@ public partial class ZipWriter
CancellationToken cancellationToken
)
{
// Build the header synchronously into a MemoryStream, then async-copy to OutputStream.
// Build the header synchronously into a pooled stream, then async-copy to OutputStream.
// This avoids any synchronous writes to the potentially async-only output stream.
using var ms = new MemoryStream();
using var ms = new PooledMemoryStream();
var outputStream = OutputStream.NotNull();
var result = WriteHeader(
ms,