[PR #897] Implement ReadByte for BufferedSubStream #1324

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

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

State: closed
Merged: Yes


I noticed absurd memory allocations when decompressing 7-zip files and tracked it down to BufferedSubStream not overriding ReadByte.
From the documentation: The default implementations of ReadByte() and WriteByte(Byte) create a new single-element byte array, and then call your implementations of Read(Byte[], Int32, Int32) and Write(Byte[], Int32, Int32). When you derive from Stream, we recommend that you override these methods to access your internal buffer, if you have one, for substantially better performance.

Testing with a ~300MB (compressed size) 7-zip file, I noticed an improvement in decompression speed from ~32 seconds to ~27 seconds and a reduction in memory allocations from ~7GB to ~100MB.

**Original Pull Request:** https://github.com/adamhathcock/sharpcompress/pull/897 **State:** closed **Merged:** Yes --- I noticed absurd memory allocations when decompressing 7-zip files and tracked it down to `BufferedSubStream` not overriding `ReadByte`. From the [documentation](https://learn.microsoft.com/en-us/dotnet/api/system.io.stream?view=net-9.0): `The default implementations of ReadByte() and WriteByte(Byte) create a new single-element byte array, and then call your implementations of Read(Byte[], Int32, Int32) and Write(Byte[], Int32, Int32). When you derive from Stream, we recommend that you override these methods to access your internal buffer, if you have one, for substantially better performance.` Testing with a ~300MB (compressed size) 7-zip file, I noticed an improvement in decompression speed from ~32 seconds to ~27 seconds and a reduction in memory allocations from ~7GB to ~100MB.
claunia added the pull-request label 2026-01-29 22:19:58 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#1324