[PR #1160] add check to see if we need to seek before hand #1597

Open
opened 2026-01-29 22:21:18 +00:00 by claunia · 0 comments
Owner

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

State: closed
Merged: Yes


Related to findings on https://github.com/adamhathcock/sharpcompress/issues/1105 by @julianxhokaxhiu

This pull request improves memory efficiency and stream handling in BufferedSubStream by using pooled buffers and optimizing seek operations. It also updates the test suite to better simulate forward-only stream scenarios.

Memory Management Improvements:

  • Switched _cache allocation in BufferedSubStream to use ArrayPool<byte>.Shared.Rent for buffer pooling, and ensured the buffer is returned to the pool in Dispose for reduced GC pressure. [1] [2]

Performance and Stream Handling Enhancements:

  • Updated RefillCache and RefillCacheAsync to avoid unnecessary seek operations by checking the current stream position before seeking, improving performance for sequential reads. [1] [2]

Testing Improvements:

  • Modified BufferReadAndSeekTest to wrap the memory stream with ForwardOnlyStream, better simulating streams that do not support seeking. [1] [2]
**Original Pull Request:** https://github.com/adamhathcock/sharpcompress/pull/1160 **State:** closed **Merged:** Yes --- Related to findings on https://github.com/adamhathcock/sharpcompress/issues/1105 by @julianxhokaxhiu This pull request improves memory efficiency and stream handling in `BufferedSubStream` by using pooled buffers and optimizing seek operations. It also updates the test suite to better simulate forward-only stream scenarios. **Memory Management Improvements:** * Switched `_cache` allocation in `BufferedSubStream` to use `ArrayPool<byte>.Shared.Rent` for buffer pooling, and ensured the buffer is returned to the pool in `Dispose` for reduced GC pressure. [[1]](diffhunk://#diff-e05b040e70e38b0d116d8d268e407131b5ba8b6614af841bc9188cd7f368fee3R2) [[2]](diffhunk://#diff-e05b040e70e38b0d116d8d268e407131b5ba8b6614af841bc9188cd7f368fee3L31-R41) **Performance and Stream Handling Enhancements:** * Updated `RefillCache` and `RefillCacheAsync` to avoid unnecessary seek operations by checking the current stream position before seeking, improving performance for sequential reads. [[1]](diffhunk://#diff-e05b040e70e38b0d116d8d268e407131b5ba8b6614af841bc9188cd7f368fee3R71-R78) [[2]](diffhunk://#diff-e05b040e70e38b0d116d8d268e407131b5ba8b6614af841bc9188cd7f368fee3R93-R98) **Testing Improvements:** * Modified `BufferReadAndSeekTest` to wrap the memory stream with `ForwardOnlyStream`, better simulating streams that do not support seeking. [[1]](diffhunk://#diff-5203913d939d601de98a07fe1ef8384114c7cbb9de17ccc5a93297b51c301e6eR8) [[2]](diffhunk://#diff-5203913d939d601de98a07fe1ef8384114c7cbb9de17ccc5a93297b51c301e6eL67-R75)
claunia added the pull-request label 2026-01-29 22:21:18 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#1597