Avoid NotSupportedException overhead in SharpCompressStream for non-seekable streams#1084
Merged
adamhathcock merged 5 commits intomasterfrom Dec 23, 2025
Merged
Conversation
Co-authored-by: adamhathcock <527620+adamhathcock@users.noreply.github.com>
Co-authored-by: adamhathcock <527620+adamhathcock@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Handle System.NotSupportedException more efficiently
Avoid NotSupportedException overhead in SharpCompressStream for non-seekable streams
Dec 23, 2025
adamhathcock
approved these changes
Dec 23, 2025
Owner
|
@copilot remove the try/catch...just check CanSeek |
Co-authored-by: adamhathcock <527620+adamhathcock@users.noreply.github.com>
Contributor
Author
Done in commit 6991900. Removed the try/catch blocks and simplified to just check CanSeek using ternary operators: _baseInitialPos = stream.CanSeek ? stream.Position : 0;
_internalPosition = Stream.CanSeek ? Stream.Position : 0;All tests pass (258 tests including XZ-specific tests). |
adamhathcock
approved these changes
Dec 23, 2025
adamhathcock
approved these changes
Dec 23, 2025
Contributor
There was a problem hiding this comment.
Pull request overview
This PR optimizes SharpCompressStream performance by replacing exception-based handling with proactive CanSeek checks when accessing stream positions. The change eliminates the overhead of throwing and catching NotSupportedException for non-seekable streams like ReadOnlyStream used in XZ file operations.
Key Changes:
- Replaced try/catch blocks with
CanSeekternary checks in two locations - Added clarifying comments explaining the rationale for the check
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
adamhathcock
approved these changes
Dec 23, 2025
This was referenced Jan 5, 2026
This was referenced Jan 5, 2026
This was referenced Jan 12, 2026
This was referenced Jan 19, 2026
This was referenced Jan 26, 2026
This was referenced Feb 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
SharpCompressStreamwas attempting to accessstream.Positionwithout checkingCanSeek, causingNotSupportedExceptionto be thrown and caught for every non-seekable stream (e.g., XZ'sReadOnlyStream). This exception overhead occurred in the constructor and buffer allocation, impacting performance for formats using non-seekable streams.Changes
stream.CanSeekbefore accessingPositionin two locations:_baseInitialPosBufferSizesetter initialization of_internalPositionImpact
Eliminates exception overhead for non-seekable streams while maintaining identical behavior for seekable streams. No API changes.
<issue_title>Handled System.NotSupportedException but can be avoided?</issue_title>
><issue_description>Open xz file like this:
>
> using var fileStream = new FileStream(updateFilename, FileMode.Open, FileAccess.Read);
> using var reader = ReaderFactory.Open(fileStream, new SharpCompress.Readers.ReaderOptions()
> {
> LeaveStreamOpen = true,
> });
>
>
>
SharpCompressStreamis wrapped aroundReadOnlyStreamandReadOnlyStreamdoesnt' support Position get. InSharpCompressStreamexception is handled:>
> try
> {
> _baseInitialPos = stream.Position;
> }
> catch
> {
> _baseInitialPos = 0;
> }
>
> But this is still exception thrown for each entry I think which causes performance issues. Can it be avoided? Check for
CanSeek?</issue_description>>
> ## Comments on the Issue (you are @copilot in this section)
>
>
>
>
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.