[PR #1131] More test fixes and some perf changes #1566

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

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

State: closed
Merged: Yes


This pull request introduces several improvements and refactorings to asynchronous ZIP archive handling and Deflate decompression. The main changes include refactoring buffer management for memory efficiency, updating async reading APIs for consistency, and improving resource cleanup. These updates enhance performance, reduce allocations, and make the codebase easier to maintain.

Async ZIP reading and buffer management improvements

  • Refactored SeekableZipHeaderFactory to use ArrayPool<byte> for buffer allocation in async header seeking, reducing memory allocations and improving performance. Also, updated the header search logic to use spans for efficient data access. [1] [2]
  • Updated async ZIP header reading methods to use explicit buffer arrays and offsets instead of returning new arrays, improving memory management and consistency across DirectoryEndHeader, DirectoryEntryHeader, LocalEntryHeader, and Zip64DirectoryEndHeader. [1] [2] [3] [4]
  • Added a SkipAsync method to AsyncBinaryReader and replaced unnecessary buffer reads with skips in ZIP header parsing, improving efficiency. [1] [2]

Deflate decompression memory optimization

  • Changed the Deflate decompression window from a raw byte[] to an IMemoryOwner<byte> from MemoryPool<byte>, ensuring pooled buffer usage and proper disposal for reduced GC pressure and memory leaks. All array copy operations were updated to use spans. [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]

Resource cleanup and async disposal

  • Implemented DisposeAsync in RarArchive to support proper async resource cleanup, including disposal of unpacker resources.
  • Ensured the Deflate window buffer is disposed correctly in InflateBlocks.Free() to prevent memory leaks when decompressing data.

These changes collectively improve performance, resource management, and maintainability of the archive and compression codebase.

**Original Pull Request:** https://github.com/adamhathcock/sharpcompress/pull/1131 **State:** closed **Merged:** Yes --- This pull request introduces several improvements and refactorings to asynchronous ZIP archive handling and Deflate decompression. The main changes include refactoring buffer management for memory efficiency, updating async reading APIs for consistency, and improving resource cleanup. These updates enhance performance, reduce allocations, and make the codebase easier to maintain. ### Async ZIP reading and buffer management improvements * Refactored `SeekableZipHeaderFactory` to use `ArrayPool<byte>` for buffer allocation in async header seeking, reducing memory allocations and improving performance. Also, updated the header search logic to use spans for efficient data access. [[1]](diffhunk://#diff-e4bf6d19a79bff81ad297d9d193c48548686e39806ed8e0f928a6afd99f7f23aL250-R202) [[2]](diffhunk://#diff-e4bf6d19a79bff81ad297d9d193c48548686e39806ed8e0f928a6afd99f7f23aR211-R215) * Updated async ZIP header reading methods to use explicit buffer arrays and offsets instead of returning new arrays, improving memory management and consistency across `DirectoryEndHeader`, `DirectoryEntryHeader`, `LocalEntryHeader`, and `Zip64DirectoryEndHeader`. [[1]](diffhunk://#diff-c07e955791385b6bc8e78f729f4bf89c594ffb7a2a8e10e39a095b5a7e6405dbL32-R33) [[2]](diffhunk://#diff-115c96a4f555b3918f7c4e399e83ca7d8a9eede91e237aa989246b6bbec61761L56-R61) [[3]](diffhunk://#diff-f37cd5cf680b244796a45c6316c30e473b6c39ca38b5924b5c9aab0fe201e00eL40-R43) [[4]](diffhunk://#diff-0c1915b061ec322d6ba9aa9e751546b3a1e9505847c0b711ccbc03334252f0b8L41-R45) * Added a `SkipAsync` method to `AsyncBinaryReader` and replaced unnecessary buffer reads with skips in ZIP header parsing, improving efficiency. [[1]](diffhunk://#diff-fb995dad22a6c5e6b7fbcd992f623685cd43e387568a8b2ead2581b11ba708a5L54-R66) [[2]](diffhunk://#diff-8f0c2dac486475c7cff3782cf74b4fcd7f8f62962203af2fb7b89143d42eefb7L82-R82) ### Deflate decompression memory optimization * Changed the Deflate decompression window from a raw `byte[]` to an `IMemoryOwner<byte>` from `MemoryPool<byte>`, ensuring pooled buffer usage and proper disposal for reduced GC pressure and memory leaks. All array copy operations were updated to use spans. [[1]](diffhunk://#diff-8f4178f88d6ead96112102c2843a01cd5e609a6beae3debf67796e074de896acL119-R127) [[2]](diffhunk://#diff-8f4178f88d6ead96112102c2843a01cd5e609a6beae3debf67796e074de896acL343-R344) [[3]](diffhunk://#diff-8f4178f88d6ead96112102c2843a01cd5e609a6beae3debf67796e074de896acR719-R726) [[4]](diffhunk://#diff-8f4178f88d6ead96112102c2843a01cd5e609a6beae3debf67796e074de896acL1216-R1223) [[5]](diffhunk://#diff-8f4178f88d6ead96112102c2843a01cd5e609a6beae3debf67796e074de896acL1262-R1269) [[6]](diffhunk://#diff-8f4178f88d6ead96112102c2843a01cd5e609a6beae3debf67796e074de896acL1399-R1406) [[7]](diffhunk://#diff-8f4178f88d6ead96112102c2843a01cd5e609a6beae3debf67796e074de896acL1464-R1478) [[8]](diffhunk://#diff-8f4178f88d6ead96112102c2843a01cd5e609a6beae3debf67796e074de896acL1493-R1507) [[9]](diffhunk://#diff-8f4178f88d6ead96112102c2843a01cd5e609a6beae3debf67796e074de896acL1512-R1527) [[10]](diffhunk://#diff-8f4178f88d6ead96112102c2843a01cd5e609a6beae3debf67796e074de896acL1563-R1573) ### Resource cleanup and async disposal * Implemented `DisposeAsync` in `RarArchive` to support proper async resource cleanup, including disposal of unpacker resources. * Ensured the Deflate window buffer is disposed correctly in `InflateBlocks.Free()` to prevent memory leaks when decompressing data. These changes collectively improve performance, resource management, and maintainability of the archive and compression codebase.
claunia added the pull-request label 2026-01-29 22:21:10 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#1566