[PR #981] Fix ~7x performance regression in ArchiveFactory.Open() by deferring stream buffering #1401

Closed
opened 2026-01-29 22:20:22 +00:00 by claunia · 0 comments
Owner

Original Pull Request: https://github.com/adamhathcock/sharpcompress/pull/981

State: closed
Merged: No


Opening archives with ArchiveFactory.Open() became ~7x slower after buffering was added to stream wrapping. The buffering was applied before archive format detection, causing repeated buffer invalidations during the IsArchive() probe sequence as each factory seeks through the stream.

Changes

  • Defer stream wrapping: Move SharpCompressStream buffering to occur after FindFactory() completes format detection
  • Factory detection now operates on unwrapped streams, eliminating buffer thrashing from seek operations
  • Archive reading still benefits from buffering as the wrapped stream is passed to factory.Open()
// Before: buffer applied before detection (slow)
stream = new SharpCompressStream(stream, bufferSize: readerOptions.BufferSize);
return FindFactory<IArchiveFactory>(stream).Open(stream, readerOptions);

// After: buffer applied after detection (fast)
var factory = FindFactory<IArchiveFactory>(stream);
stream = new SharpCompressStream(stream, bufferSize: readerOptions.BufferSize);
return factory.Open(stream, readerOptions);

All 480 tests pass.

Original prompt

This section details on the original issue you should resolve

<issue_title>Decompression performance 7 times slower after updating from 0.31 to 0.32 +</issue_title>
<issue_description>Opening archives with ArchiveFactory.Open() seems to have a significant performance lose (around 7 times slower) after updating from 0.31 to 0.32 + versions.

Anyone noticed any similar behavior? </issue_description>

Comments on the Issue (you are @copilot in this section)

@adamhathcock Any details of what format used?

I want to stick some profiling around the codebase soon</comment_new>


Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

**Original Pull Request:** https://github.com/adamhathcock/sharpcompress/pull/981 **State:** closed **Merged:** No --- Opening archives with `ArchiveFactory.Open()` became ~7x slower after buffering was added to stream wrapping. The buffering was applied before archive format detection, causing repeated buffer invalidations during the `IsArchive()` probe sequence as each factory seeks through the stream. ## Changes - **Defer stream wrapping**: Move `SharpCompressStream` buffering to occur after `FindFactory()` completes format detection - Factory detection now operates on unwrapped streams, eliminating buffer thrashing from seek operations - Archive reading still benefits from buffering as the wrapped stream is passed to `factory.Open()` ```csharp // Before: buffer applied before detection (slow) stream = new SharpCompressStream(stream, bufferSize: readerOptions.BufferSize); return FindFactory<IArchiveFactory>(stream).Open(stream, readerOptions); // After: buffer applied after detection (fast) var factory = FindFactory<IArchiveFactory>(stream); stream = new SharpCompressStream(stream, bufferSize: readerOptions.BufferSize); return factory.Open(stream, readerOptions); ``` All 480 tests pass. <!-- START COPILOT CODING AGENT SUFFIX --> <details> <summary>Original prompt</summary> > > ---- > > *This section details on the original issue you should resolve* > > <issue_title>Decompression performance 7 times slower after updating from 0.31 to 0.32 +</issue_title> > <issue_description>Opening archives with ArchiveFactory.Open() seems to have a significant performance lose (around 7 times slower) after updating from 0.31 to 0.32 + versions. > > Anyone noticed any similar behavior? </issue_description> > > ## Comments on the Issue (you are @copilot in this section) > > <comments> > <comment_new><author>@adamhathcock</author><body> > Any details of what format used? > > I want to stick some profiling around the codebase soon</body></comment_new> > </comments> > </details> - Fixes adamhathcock/sharpcompress#879 <!-- START COPILOT CODING AGENT TIPS --> --- ✨ Let Copilot coding agent [set things up for you](https://github.com/adamhathcock/sharpcompress/issues/new?title=✨+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot) — coding agent works faster and does higher quality work when set up for your repo.
claunia added the pull-request label 2026-01-29 22:20:22 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#1401