Use the internal write buffer more#2
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
There was a problem hiding this comment.
Pull request overview
This PR optimizes Compress::LZ4::Writer#write to batch multiple LZ4F_compressUpdate outputs into the existing @buffer and only write to the underlying @output when the remaining buffer capacity is insufficient (and once at the end). This reduces the number of @output.write calls when a single write call processes multiple MaxSrcSize chunks.
Changes:
- Accumulate compressed output in
@bufferacross iterations of thewriteloop using an in-methodbuffer_posoffset. - Flush
@bufferto@outputonly when the next update’s worst-case bound won’t fit in the remaining space, and once at the end ofwrite.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
The buffer was sized to exactly one worst-case block (compress_bound(MaxSrcSize)), so the per-iteration fit check always flushed after a single block and the internal buffering was a no-op. Size the buffer to hold several blocks (>= 256 KB) and use the precomputed @block_bound constant for the fit check, which also drops a redundant compress_bound FFI call per block. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Fix: buffer was never actually batching writesWhile reviewing, I found the buffering optimization was effectively a no-op.
if required_buffer_size > @buffer.size - buffer_posreduced to Changes (
|
Let liblz4 add to the internal buffer until it's full. Decreases the number of writes done to the output IO.