Files
sharpcompress/tests/SharpCompress.Performance/Benchmarks/SevenZipBenchmarks.cs
Adam Hathcock 14d77b671c Release pooled 7z decoder buffers across async and partial reads
Dispose cached folder decoders during async archive cleanup and scope temporary encoded-header decoders in both read paths. Release LZMA range-decoder input buffers on partial reads and failures while preserving borrowed source ownership.

Correct the synchronous extraction-reader benchmark and expand lifetime and checksum coverage. Document profiling showing async Archive allocations falling from 8.95 MiB to 0.70 MiB per extraction, with metadata-only allocations reduced by 70%.

Validation: 2,989 .NET 10 Release tests passed; all library targets, the performance project, and net48 test compilation succeeded. Formatting and whitespace checks passed.
2026-10-19 14:43:51 +01:00

107 lines
3.8 KiB
C#

using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using SharpCompress.Archives.SevenZip;
namespace SharpCompress.Performance.Benchmarks;
[MemoryDiagnoser]
public class SevenZipBenchmarks : ArchiveBenchmarkBase
{
private byte[] _lzmaBytes = null!;
private byte[] _lzma2Bytes = null!;
[GlobalSetup]
public void Setup()
{
_lzmaBytes = File.ReadAllBytes(GetArchivePath("7Zip.LZMA.7z"));
_lzma2Bytes = File.ReadAllBytes(GetArchivePath("7Zip.LZMA2.7z"));
}
[Benchmark(Description = "7Zip LZMA: Extract all entries")]
public void SevenZipLzmaExtract()
{
using var stream = new MemoryStream(_lzmaBytes);
using var archive = SevenZipArchive.OpenArchive(stream);
foreach (var entry in archive.Entries.Where(e => !e.IsDirectory))
{
using var entryStream = entry.OpenEntryStream();
entryStream.CopyTo(Stream.Null);
}
}
[Benchmark(Description = "7Zip LZMA: Extract all entries (Async)")]
public async Task SevenZipLzmaExtractAsync()
{
using var stream = new MemoryStream(_lzmaBytes);
await using var archive = await SevenZipArchive
.OpenAsyncArchive(stream)
.ConfigureAwait(false);
await foreach (var entry in archive.EntriesAsync.Where(e => !e.IsDirectory))
{
await using var entryStream = await entry.OpenEntryStreamAsync().ConfigureAwait(false);
await entryStream.CopyToAsync(Stream.Null).ConfigureAwait(false);
}
}
[Benchmark(Description = "7Zip LZMA2: Extract all entries")]
public void SevenZipLzma2Extract()
{
using var stream = new MemoryStream(_lzma2Bytes);
using var archive = SevenZipArchive.OpenArchive(stream);
foreach (var entry in archive.Entries.Where(e => !e.IsDirectory))
{
using var entryStream = entry.OpenEntryStream();
entryStream.CopyTo(Stream.Null);
}
}
[Benchmark(Description = "7Zip LZMA2: Extract all entries (Async)")]
public async Task SevenZipLzma2ExtractAsync()
{
using var stream = new MemoryStream(_lzma2Bytes);
await using var archive = await SevenZipArchive
.OpenAsyncArchive(stream)
.ConfigureAwait(false);
await foreach (var entry in archive.EntriesAsync.Where(e => !e.IsDirectory))
{
await using var entryStream = await entry.OpenEntryStreamAsync().ConfigureAwait(false);
await entryStream.CopyToAsync(Stream.Null).ConfigureAwait(false);
}
}
[Benchmark(Description = "7Zip LZMA2 Reader: Extract all entries")]
public void SevenZipLzma2Extract_Reader()
{
using var stream = new MemoryStream(_lzma2Bytes);
using var archive = SevenZipArchive.OpenArchive(stream);
using var reader = archive.ExtractAllEntries();
while (reader.MoveToNextEntry())
{
if (reader.Entry.IsDirectory)
{
continue;
}
using var entryStream = reader.OpenEntryStream();
entryStream.CopyTo(Stream.Null);
}
}
[Benchmark(Description = "7Zip LZMA2 Reader: Extract all entries (Async)")]
public async Task SevenZipLzma2ExtractAsync_Reader()
{
using var stream = new MemoryStream(_lzma2Bytes);
await using var archive = await SevenZipArchive
.OpenAsyncArchive(stream)
.ConfigureAwait(false);
await using var reader = await archive.ExtractAllEntriesAsync();
while (await reader.MoveToNextEntryAsync().ConfigureAwait(false))
{
await using var entryStream = await reader.OpenEntryStreamAsync().ConfigureAwait(false);
await entryStream.CopyToAsync(Stream.Null).ConfigureAwait(false);
}
}
}