Skip to content

fix: assemble split IPC messages in StreamDecoder at their declared size - #11648

Merged
Jefffrey merged 1 commit into
apache:mainfrom
ranflarion:stream-decoder-bounded-reserve
Oct 18, 2026
Merged

Jefffrey merged 1 commit into
apache:mainfrom
ranflarion:stream-decoder-bounded-reserve

Conversation

@ranflarion

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

StreamDecoder assembles a message that arrives split across input buffers in a scratch MutableBuffer that starts empty and doubles as the pieces are copied in, and the decoded batch is zero-copy over that buffer, so it keeps between 1x and 2x its body alive. A reader that feeds fixed-size chunks hits this on nearly every message large enough to span one: DataFusion 55 reads spill files back in 128 KiB chunks, and a spilling sort's read-back batches held 2,969 MiB for 2,198 MiB of data. StreamReader avoids it since #10522 by reading a body into min(body_length, MAX_PREALLOC_BYTES) up front.

What changes are included in this PR?

When the first bytes of a message flatbuffer or body are copied into the empty scratch buffer, the decoder now reserves the declared length, capped at MAX_PREALLOC_BYTES the same way read_body_bounded caps it. A message up to 64 MiB is therefore allocated once at its size, while an implausible declared length still reserves at most 64 MiB and leaves the decoder waiting for bytes that never arrive. Larger messages keep growing geometrically past the cap, as they do in StreamReader. The path that slices a message straight out of a single input buffer is unchanged.

Are these changes tested?

Yes, two tests in arrow-ipc/src/reader/stream.rs. test_split_body_is_buffered_at_its_declared_size feeds a 99,272 byte stream one byte at a time and checks the decoded batch holds no more than the stream: without the change it holds 131,072 bytes. test_implausible_body_length_reserves_a_bounded_buffer declares body lengths of i64::MAX and 1 << 50 and checks the decoder keeps waiting for the body: reserving the declared length without the cap fails it with MemoryError("invalid allocation layout for requested capacity").

In the DataFusion sort from the issue, built against arrow 59.3 with this change, peak native allocation went from 201.8 MiB to 161.0 MiB.

The arrow-ipc suite passes (138 library tests, including the two above), along with cargo fmt --all -- --check and cargo clippy -p arrow-ipc --all-targets --all-features -- -D warnings.

Are there any user-facing changes?

No API changes. Batches decoded from split messages hold less memory. No breaking changes.

@github-actions github-actions Bot added the arrow-ipc arrow-ipc crate label Oct 17, 2026
@Jefffrey Jefffrey added the bug Squash those bugs label Oct 18, 2026
@Jefffrey
Jefffrey added this pull request to the merge queue Oct 18, 2026
@Jefffrey

Copy link
Copy Markdown
Contributor

thanks @ranflarion

Merged via the queue into apache:main with commit ccc426c Oct 18, 2026
35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arrow-ipc arrow-ipc crate bug Squash those bugs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

IPC StreamDecoder keeps up to twice a split message's size in the decoded batch

2 participants