Align StreamReader with Ruby's IO conventions - #151
Open
sribalakumar wants to merge 1 commit into
Open
Conversation
sribalakumar
force-pushed
the
fix-stream-reader-io-contract
branch
2 times, most recently
from
August 9, 2026 13:32
787c4e4 to
c260dd7
Compare
sribalakumar
force-pushed
the
fix-stream-reader-io-contract
branch
from
August 9, 2026 14:03
c260dd7 to
173ac75
Compare
StreamReader#read passed its length argument straight to the underlying IO, so it read that many *compressed* bytes and returned however many decompressed bytes fell out. The size of the return value tracked the compression ratio rather than the caller's request: a small frame could return far more than asked for, while a larger one returned an empty String because zstd was still filling an internal block. That makes the reader unusable for consumers that need a specific number of bytes, such as Gem::Package::TarReader. Buffer decompressed output so length means decompressed bytes, and serve reads from that buffer. Refills use decompress_with_pos rather than decompress: it writes at most ZSTD_DStreamOutSize bytes per call and reports how much input it consumed, so a high compression ratio cannot balloon the buffer. Also return nil at EOF instead of raising StandardError, matching IO#read. Rescuing StandardError to detect EOF would otherwise swallow genuine decompression failures, which the extension raises as RuntimeError. Add eof?, support read with no length and an outbuf argument, and fix close, which called finish on StreamingDecompress (a method it does not define) and wrote to a read-only IO. Correct the spelling of the experimental marker on both StreamReader and StreamWriter. The marker stays in place: this does not promote either class to a stable API.
sribalakumar
force-pushed
the
fix-stream-reader-io-contract
branch
from
August 9, 2026 14:06
173ac75 to
82f8165
Compare
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.
Fixes #149.
Opening this as a concrete proposal — happy to close it if you'd prefer the non-breaking route I mentioned in the issue (leave
StreamReaderas-is, add a separate IO-conformant class). No hard feelings either way.What changed
StreamReader#readpassedlengthstraight through to the underlying IO, so it read that many compressed bytes and returned however many decompressed bytes fell out. The return size tracked the compression ratio instead of the caller's request.This buffers decompressed output and serves reads from that buffer, so
lengthmeans decompressed bytes.read(512), 44-byte frameread(512)×4, 20 KB frame[0, 0, 0, 0][512, 512, 512, 512]StandardErrornilcloseNoMethodErrorAlso adds
eof?,readwith no length, and anoutbufargument.On
decompress_with_posRefills use
decompress_with_posrather thandecompress.decompressloops until all input is consumed, so output per call is unbounded — with a high compression ratio a 64 KB read can expand to hundreds of MB in the buffer.decompress_with_poswrites at mostZSTD_DStreamOutSizebytes per call and reports how much input it consumed, which keeps the buffer bounded regardless of ratio. That matters when the compressed input isn't trusted.Measured on a 14.3 MB archive (64× ratio), peak buffer:
decompress4.6 MB →decompress_with_pos0.57 MB.Breaking change
read's semantics change, so this is breaking for anyone depending on the current behaviour. The existing spec's expectations change accordingly:readalso becomes arity-optional to matchIO#read.Tests
Full suite passes — 88 examples, 0 failures (was 69). New coverage: exact-length reads across block boundaries, ratio independence,
nilat EOF,readwith no args,read(0), negative length,outbuf(including the EOF case),eof?,close, customchunk_size, round-trip integrity for both compressible and incompressible input, and an end-to-end test drivingGem::Package::TarReaderthrough the reader.