Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
817bd2a
First pass of implementation
adamhathcock Apr 4, 2026
a26198a
use pooled memory streams for better performance
adamhathcock Apr 4, 2026
2bf0075
cleaned up PooledMemoryStream.cs
adamhathcock Apr 4, 2026
db9bdb2
Update src/SharpCompress/IO/PooledMemoryStream.cs
adamhathcock Apr 5, 2026
6b9a3f8
Update src/SharpCompress/Compressors/PPMd/PpmdStream.cs
adamhathcock Apr 5, 2026
5e7644b
Update src/SharpCompress/IO/PooledMemoryStream.cs
adamhathcock Apr 5, 2026
c01dd5c
Update tests/SharpCompress.Test/Streams/PooledMemoryStreamTests.cs
adamhathcock Apr 5, 2026
8c4fed3
Remove try/catch from async overloads and fix CA1725 parameter naming…
Copilot Apr 5, 2026
c25fec3
fix PooledMemoryStream.cs
adamhathcock Apr 5, 2026
bfe7824
remove net5 from target frameworks to avoid issues
adamhathcock Apr 5, 2026
4f09c6d
Update tests/SharpCompress.Test/Streams/PooledMemoryStreamTests.cs
adamhathcock Apr 5, 2026
ec46584
Update src/SharpCompress/IO/PooledMemoryStream.cs
adamhathcock Apr 5, 2026
fb78598
implement GetBuffer with non-pooled array creation
adamhathcock Apr 5, 2026
d1e6173
remove contiguous buffer tracking and added overrenting array pool test
adamhathcock Apr 5, 2026
2487d89
Merge remote-tracking branch 'origin/adam/pooledmemorystream' into ad…
adamhathcock Apr 5, 2026
e29670e
Merge branch 'master' into adam/pooledmemorystream
adamhathcock Apr 19, 2026
b36689a
format
adamhathcock Apr 19, 2026
ad3c5da
update tests
adamhathcock Apr 19, 2026
891d126
Update src/SharpCompress/IO/PooledMemoryStream.cs
adamhathcock Apr 19, 2026
e0ccbbb
Update src/SharpCompress/IO/PooledMemoryStream.cs
adamhathcock Apr 19, 2026
49a7e15
Add EnsureNotClosed() to Flush and FlushAsync in PooledMemoryStream
Copilot Apr 19, 2026
f8485b0
Compute CRC without GetBuffer() in SevenZipWriter to avoid allocation
Copilot Apr 19, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/SharpCompress/Archives/Tar/TarArchive.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ var header in TarHeaderFactory.ReadHeaderAsync(

using (var entryStream = entry.OpenEntryStream())
{
using var memoryStream = new MemoryStream();
using var memoryStream = new PooledMemoryStream();
await entryStream.CopyToAsync(memoryStream).ConfigureAwait(false);
memoryStream.Position = 0;
var bytes = memoryStream.ToArray();
Expand Down
2 changes: 1 addition & 1 deletion src/SharpCompress/Archives/Tar/TarArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ var header in TarHeaderFactory.ReadHeader(

using (var entryStream = entry.OpenEntryStream())
{
using var memoryStream = new MemoryStream();
using var memoryStream = new PooledMemoryStream();
entryStream.CopyTo(memoryStream, Constants.BufferSize);
memoryStream.Position = 0;
var bytes = memoryStream.ToArray();
Expand Down
4 changes: 2 additions & 2 deletions src/SharpCompress/Common/Rar/CryptKey5.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ int keyLength
)
{
var passwordBytes = Encoding.UTF8.GetBytes(password);
#if LEGACY_DOTNET || NET5_0
#if LEGACY_DOTNET
using var hmac = new HMACSHA256(passwordBytes);
var block = hmac.ComputeHash(salt);
#else
Expand All @@ -50,7 +50,7 @@ int keyLength
{
for (var i = 1; i < loop[x]; i++)
{
#if LEGACY_DOTNET || NET5_0
#if LEGACY_DOTNET
block = hmac.ComputeHash(block);
#else
block = HMACSHA256.HashData(passwordBytes, block);
Expand Down
2 changes: 1 addition & 1 deletion src/SharpCompress/Common/SevenZip/ArchiveReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1182,7 +1182,7 @@ private void OpenFile()
}
else
{
_stream = new MemoryStream();
_stream = new PooledMemoryStream();
}
_rem = _db._files[index].Size;
}
Expand Down
3 changes: 2 additions & 1 deletion src/SharpCompress/Common/SevenZip/SevenZipFilesInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.IO;
using System.Text;
using SharpCompress.Compressors.LZMA.Utilities;
using SharpCompress.IO;

namespace SharpCompress.Common.SevenZip;

Expand Down Expand Up @@ -215,7 +216,7 @@ private static void WriteFileProperty(
Action<Stream> writeData
)
{
using var dataStream = new MemoryStream();
using var dataStream = new PooledMemoryStream();
writeData(dataStream);

stream.WriteByte((byte)propertyId);
Expand Down
2 changes: 1 addition & 1 deletion src/SharpCompress/Common/Tar/Headers/TarHeader.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private async ValueTask WriteUstarAsync(Stream output, CancellationToken cancell
int splitIndex = -1;
for (int i = 0; i < dirSeps.Count; i++)
{
#if NET5_0_OR_GREATER
#if NET6_0_OR_GREATER
int count = ArchiveEncoding
.GetEncoding()
.GetByteCount(fullName.AsSpan(0, dirSeps[i]));
Expand Down
2 changes: 1 addition & 1 deletion src/SharpCompress/Common/Tar/Headers/TarHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ internal void WriteUstar(Stream output)
int splitIndex = -1;
for (int i = 0; i < dirSeps.Count; i++)
{
#if NET5_0_OR_GREATER
#if NET6_0_OR_GREATER
int count = ArchiveEncoding
.GetEncoding()
.GetByteCount(fullName.AsSpan(0, dirSeps[i]));
Expand Down
5 changes: 3 additions & 2 deletions src/SharpCompress/Compressors/LZMA/Lzma2EncoderStream.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using SharpCompress.IO;

namespace SharpCompress.Compressors.LZMA;

Expand Down Expand Up @@ -158,7 +159,7 @@ private byte[] CompressBlock(ReadOnlySpan<byte> data)
}

using var inputMs = new MemoryStream(data.ToArray(), writable: false);
using var outputMs = new MemoryStream();
using var outputMs = new PooledMemoryStream();

encoder.Code(inputMs, outputMs, data.Length, -1, null);

Expand Down Expand Up @@ -190,7 +191,7 @@ private int FindConsumedBytes(byte[] compressedData, int uncompressedSize)
decoder.SetDecoderProperties(props);

using var input = new MemoryStream(compressedData);
using var output = new MemoryStream();
using var output = new PooledMemoryStream();
decoder.Code(input, output, compressedData.Length, uncompressedSize, null);
Comment on lines +194 to 195
Copy link

Copilot AI Apr 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FindConsumedBytes doesn’t use the decompressed output; it only needs input.Position after decoder.Code(...). Using PooledMemoryStream here adds pooling overhead and can rent/return buffers unnecessarily. Use Stream.Null (or another sink stream) for output instead.

Suggested change
using var output = new PooledMemoryStream();
decoder.Code(input, output, compressedData.Length, uncompressedSize, null);
decoder.Code(input, Stream.Null, compressedData.Length, uncompressedSize, null);

Copilot uses AI. Check for mistakes.

return (int)input.Position;
Expand Down
3 changes: 2 additions & 1 deletion src/SharpCompress/Compressors/PPMd/PpmdStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using SharpCompress.Compressors.LZMA.RangeCoder;
using SharpCompress.Compressors.PPMd.H;
using SharpCompress.Compressors.PPMd.I1;
using SharpCompress.IO;

namespace SharpCompress.Compressors.PPMd;

Expand Down Expand Up @@ -179,7 +180,7 @@ protected override void Dispose(bool disposing)
{
if (_compress)
{
_model.EncodeBlock(_stream, new MemoryStream(), true);
_model.EncodeBlock(_stream, Stream.Null, true);
}
}
base.Dispose(disposing);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading.Tasks;
using SharpCompress.Common;
using SharpCompress.Compressors.RLE90;
using SharpCompress.IO;

namespace SharpCompress.Compressors.Squeezed;

Expand Down Expand Up @@ -54,14 +55,14 @@ private async Task<Stream> BuildDecodedStreamAsync(CancellationToken cancellatio

if (bytesRead != 2)
{
return new MemoryStream(Array.Empty<byte>());
return new PooledMemoryStream();
}

int numnodes = numNodesBytes[0] | (numNodesBytes[1] << 8);

if (numnodes >= NUMVALS || numnodes == 0)
{
return new MemoryStream(Array.Empty<byte>());
return new PooledMemoryStream();
}

var dnode = new int[numnodes, 2];
Expand All @@ -82,7 +83,7 @@ private async Task<Stream> BuildDecodedStreamAsync(CancellationToken cancellatio
}

var bitReader = new BitReader(_stream);
var huffmanDecoded = new MemoryStream();
var huffmanDecoded = new PooledMemoryStream();
int i = 0;

while (true)
Expand Down
5 changes: 3 additions & 2 deletions src/SharpCompress/Compressors/Squeezed/SqueezedStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using SharpCompress.Common;
using SharpCompress.Compressors.RLE90;
using SharpCompress.IO;

namespace SharpCompress.Compressors.Squeezed;

Expand Down Expand Up @@ -67,7 +68,7 @@ private Stream BuildDecodedStream()

if (numnodes >= NUMVALS || numnodes == 0)
{
return new MemoryStream(Array.Empty<byte>());
return new PooledMemoryStream();
}

var dnode = new int[numnodes, 2];
Expand All @@ -78,7 +79,7 @@ private Stream BuildDecodedStream()
}

var bitReader = new BitReader(_stream);
var huffmanDecoded = new MemoryStream();
var huffmanDecoded = new PooledMemoryStream();
int i = 0;

while (true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#if NETCOREAPP3_0_OR_GREATER
using System.Runtime.Intrinsics.X86;
#endif
#if NET5_0_OR_GREATER
#if NET6_0_OR_GREATER
using System.Runtime.Intrinsics.Arm;
#endif

Expand Down Expand Up @@ -554,7 +554,7 @@ the dst buffer. In circumstances where the op "catches up" to where the
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void ZSTD_copy16(void* dst, void* src)
{
#if NET5_0_OR_GREATER
#if NET6_0_OR_GREATER
if (AdvSimd.IsSupported)
{
AdvSimd.Store((byte*)dst, AdvSimd.LoadVector128((byte*)src));
Expand Down
6 changes: 3 additions & 3 deletions src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdLazy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
#endif
#if NET5_0_OR_GREATER
#if NET6_0_OR_GREATER
using System.Runtime.Intrinsics.Arm;
#endif

Expand Down Expand Up @@ -1172,7 +1172,7 @@ private static uint ZSTD_row_matchMaskGroupWidth(uint rowEntries)
{
assert(rowEntries == 16 || rowEntries == 32 || rowEntries == 64);
assert(rowEntries <= 64);
#if NET5_0_OR_GREATER
#if NET6_0_OR_GREATER
if (AdvSimd.IsSupported && BitConverter.IsLittleEndian)
{
if (rowEntries == 16)
Expand Down Expand Up @@ -1272,7 +1272,7 @@ uint rowEntries
}
#endif

#if NET5_0_OR_GREATER
#if NET6_0_OR_GREATER
if (AdvSimd.IsSupported && BitConverter.IsLittleEndian)
{
if (rowEntries == 16)
Expand Down
Loading
Loading