Skip to content

Align StreamReader with Ruby's IO conventions - #151

Open
sribalakumar wants to merge 1 commit into
SpringMT:mainfrom
sribalakumar:fix-stream-reader-io-contract
Open

Align StreamReader with Ruby's IO conventions#151
sribalakumar wants to merge 1 commit into
SpringMT:mainfrom
sribalakumar:fix-stream-reader-io-contract

Conversation

@sribalakumar

Copy link
Copy Markdown

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 StreamReader as-is, add a separate IO-conformant class). No hard feelings either way.

What changed

StreamReader#read passed length straight 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 length means decompressed bytes.

before after
read(512), 44-byte frame 240000 bytes 512
read(512) ×4, 20 KB frame [0, 0, 0, 0] [512, 512, 512, 512]
at EOF raises StandardError nil
close NoMethodError closes the IO

Also adds eof?, read with no length, and an outbuf argument.

On decompress_with_pos

Refills use decompress_with_pos rather than decompress. decompress loops 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_pos writes at most ZSTD_DStreamOutSize bytes 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: decompress 4.6 MB → decompress_with_pos 0.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:

# before
expect(reader.read(10)).to eq('a')
expect(reader.read(10)).to eq('bcdef')
expect(reader.read(10)).to eq('')

# after
expect(reader.read(10)).to eq('abcdef')
expect(reader.read(10)).to be_nil

read also becomes arity-optional to match IO#read.

Tests

Full suite passes — 88 examples, 0 failures (was 69). New coverage: exact-length reads across block boundaries, ratio independence, nil at EOF, read with no args, read(0), negative length, outbuf (including the EOF case), eof?, close, custom chunk_size, round-trip integrity for both compressible and incompressible input, and an end-to-end test driving Gem::Package::TarReader through the reader.

@sribalakumar
sribalakumar force-pushed the fix-stream-reader-io-contract branch 2 times, most recently from 787c4e4 to c260dd7 Compare August 9, 2026 13:32
@sribalakumar
sribalakumar force-pushed the fix-stream-reader-io-contract branch from c260dd7 to 173ac75 Compare August 9, 2026 14:03
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
sribalakumar force-pushed the fix-stream-reader-io-contract branch from 173ac75 to 82f8165 Compare August 9, 2026 14:06
@sribalakumar sribalakumar changed the title Make StreamReader#read follow the IO contract Align StreamReader with Ruby's IO conventions Aug 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

StreamReader doesn't follow Ruby's IO conventions

1 participant