Repository navigation
perf(arrow-ipc): decode lz4 frames of independent blocks without a streaming decoder - #12423
Open
ariel-miculas wants to merge 5 commits into
Open
ariel-miculas wants to merge 5 commits into
ariel-miculas wants to merge 5 commits into
Conversation
…size `ipc_reader` covers lz4 at one shape: four columns of 8192 rows, at whatever maximum block size the writer's default declares. Two things that decide what lz4 decompression costs are invisible there. The first is how many buffers a message holds. A message carries one compressed buffer per field node plus its validity and offsets, each its own frame, so the per-buffer cost is most of what a wide relation pays and a four-column shape barely shows it. 200 columns of 4096 rows keeps every buffer small; 20 columns of 131072 rows makes each values buffer half a megabyte. The second is the maximum block size the frame's header declares, which is what a frame costs a streaming decoder to open whatever the buffer behind it holds. The lz4 cases set it explicitly with `try_with_lz4_block_size` rather than leaving it to the writer's default, and run at both 64 KiB and 4 MiB -- 4 MiB being what Arrow C++ and Comet declare. Leaving it implicit would also tie the benchmark to a writer default that has already changed once. Each shape runs at zstd too, as a control: zstd is untouched by work on the lz4 decoder, so a zstd row that moves is the machine, not the change. Co-Authored-By: Claude Opus 5 <[email protected]>
…ing decoder
`decompress_lz4` opens an `lz4_flex::frame::FrameDecoder` per buffer, and none
of that decoder's streaming machinery is needed for the frames IPC carries.
`FrameDecoder` sizes its source and destination buffers from the maximum block
size the frame's header *declares*, and zero-fills the destination up to that
maximum before each block, whatever the block holds. The declared maximum has
nothing to do with how much data is behind it: a writer may declare 4 MiB blocks
for a buffer of any size, and Arrow C++ and Comet do. So decoding a few KiB can
cost megabytes of memset, and an IPC message decodes one buffer after another --
one per field node plus its validity and offsets, each its own frame.
Pooling one decoder in `DecompressionContext`, as the zstd arm already does for
its decompressor, would remove the repeated construction but not the zero-fill,
which happens once per block rather than once per decoder. A decoder cannot in
fact be carried between frames at all: `FrameDecoder` asserts its destination
capacity equals the declared maximum exactly, which a decoder that has seen a
linked frame no longer satisfies, and that assertion fires in debug builds as
soon as frames of differing block modes go through one decoder.
The blocks can instead be decompressed one after another straight into the
output with `lz4_flex::block::decompress_into`: no scratch buffer, nothing
zeroed beyond the output, and no dependence on what the header declares. A frame
outside that shape -- linked blocks, a block or content checksum, a dictionary,
a block size the format does not define -- still goes to the streaming decoder,
with a decoder of its own as before. Independent blocks and no checksums is what
every Arrow writer produces.
Like the streaming path, this never sizes the output from the advertised length
alone. That length comes from the IPC prefix and is untrusted, so the output
starts at no more than `MAX_PREALLOC_BYTES` and grows only as the blocks fill
it, bounded per block by the frame's declared maximum and by what the advertised
length leaves. A frame holding more than it advertised is reported as such
rather than decoded.
Measured through `arrow-ipc/benches/ipc_reader.rs`, reading 10 batches end to
end, so decompression is only part of each figure:
no_validation/read_10/lz4 2.14 ms -> 1.95 ms -8.4%
wide_200x4096/lz4_64kib_blocks 7.44 ms -> 4.15 ms -44.3%
wide_200x4096/lz4_4096kib_blocks 68.81 ms -> 4.16 ms -93.9%
wide_20x131072/lz4_64kib_blocks 15.30 ms -> 11.53 ms -24.1%
wide_20x131072/lz4_4096kib_blocks 21.38 ms -> 11.55 ms -45.4%
Both zstd rows are unchanged (p = 0.15 and 0.13), and the two block sizes now
converge: what the header declares no longer decides what the read costs.
Allocations counted with the system allocator wrapped in a `GlobalAlloc` that
tallies every one, over the same reads -- a census, not a sample. Bytes and peak
in MiB; the zstd rows do not move and are left out:
shape blocks allocations bytes allocated peak live
4x8192 64 KiB 526 -> 366 12.47 -> 2.47 0.37 -> 0.25
4 MiB 526 -> 366 642.47 -> 2.47 8.25 -> 0.25
200x4096 64 KiB 24449 -> 16449 562.94 -> 62.94 6.42 -> 6.30
4 MiB 24449 -> 16449 32062.94 -> 62.94 14.30 -> 6.30
20x131072 64 KiB 2483 -> 1683 246.84 -> 196.84 19.81 -> 19.68
4 MiB 2483 -> 1683 3396.82 -> 196.82 27.68 -> 19.68
Every delta accounts for itself exactly. The counts fall by 160, 8000 and 800
against 80, 4000 and 400 compressed buffers: two allocations per buffer, the
decoder's `src` and `dst`, and what remains matches the zstd arm, which already
pools its decompressor. The bytes fall by twice the declared block size per
buffer -- 80, 4000 and 400 buffers times 128 KiB, then times 8 MiB -- to the
byte in all six rows. On 200 columns at 4 MiB blocks that is 31.3 GiB allocated
to read a 30 MiB stream, and the figures no longer vary with the declared size
at all.
Measured as well through a Comet in-memory cache read, where each column's
buffers are compressed one at a time and the frames declare 4 MiB blocks. 5M
rows by 6 columns, full projection: 1105 ms to 259 ms. On a 300-column relation,
where a cached batch holds hundreds of buffers, 7519 ms to about the time the
same workload takes when the writer declares 64 KiB blocks instead.
New tests cover both paths against every frame shape: independent and linked
blocks, with and without a content checksum, over compressible and
incompressible payloads, plus truncated frames.
This is apache#11129, which was closed by apache#12084 -- that
PR pooled the frame *encoder* in `IpcWriteContext` and left the decoder as it
was.
Co-Authored-By: Claude Opus 5 <[email protected]>
Contributor
Author
|
I didn't include the counting allocator code, I'm pasting it below |
ariel-miculas
force-pushed
the
improve-lz4-decoder-performance-11129
branch
from
November 23, 2026 16:45
08dca2d to
d85ea4c
Compare
mzabaluev
reviewed
Nov 23, 2026
| at, | ||
| decompressed_size, | ||
| block_max, | ||
| UNCOMPRESSED_BLOCK, |
Contributor
There was a problem hiding this comment.
Does this const value need to be passed as an argument to the private function defined in the same module?
Contributor
Author
There was a problem hiding this comment.
indeed, no need to pass it as a parameter
Comment on lines
+545
to
+548
| // `room` ran out at the advertised size rather than at the block maximum, so the | ||
| // frame holds more than the IPC prefix said it would. | ||
| if room == decompressed_size - written { | ||
| exceeds() |
Contributor
There was a problem hiding this comment.
Should the error be inspected before deciding this is the cause?
The error mapping decided that a block had overrun the advertised size by looking only at which limit capped the destination slice. That test is necessary but not sufficient: on the last block of a well-formed frame the destination is capped by the advertised size as well, so a genuinely corrupt final block reported "exceeds advertised size" and dropped the real diagnostic. Require the error to be `DecompressError::OutputTooSmall` as well, and let the four corruption variants through as themselves. The stored-block path conflated the same two cases, decided there by the block length rather than the error. A block wider than the frame's own declared maximum is a corrupt frame, not an over-long one, and now says so. Co-Authored-By: Claude Opus 5 <[email protected]>
13 tasks
This branch has not been deployed
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.
Which issue does this PR close?
FrameDecoderper buffer #11129Rationale for this change
decompress_lz4built anlz4_flex::frame::FrameDecoderper buffer, and none ofthat decoder's streaming machinery is needed for the frames IPC actually carries.
FrameDecodersizes its source and destination buffers from the maximum blocksize the frame's header declares, and zero-fills the destination up to that
maximum before each block, whatever the block holds. The format lets a writer
declare 4 MiB blocks for a buffer of any size --
lz4_flex's own encoder does sofor anything over 256 KiB -- so decoding a few KiB can cost megabytes of memset,
and an IPC message decodes one buffer after another: one per field node plus its
validity and offsets.
Pooling one decoder in
DecompressionContext, as the zstd arm already does forits decompressor, removes the repeated construction but not the zero-fill, which
happens once per block rather than once per decoder. A decoder cannot in fact be
carried between frames at all:
FrameDecoderasserts its destination capacityequals the declared maximum exactly, which a decoder that has seen a linked frame
no longer satisfies, and that assertion fires in debug builds as soon as frames
of differing block modes go through one decoder.
The uncompressed length is already known from the IPC prefix, so the blocks can
instead be decompressed one after another straight into the output with
lz4_flex::block::decompress_into: no scratch buffer, nothing zeroed beyond theoutput, and no dependence on what the header declares. A frame outside that shape
-- linked blocks, a block or content checksum, a dictionary -- still goes to the
streaming decoder, with a decoder of its own as before. Independent blocks and no
checksums is what every Arrow writer produces.
What changes are included in this PR?
New benchmarks to cover lz4 decompression and the performance fix.
Are these changes tested?
New tests cover both paths against every frame shape: independent and linked
blocks, with and without a content checksum, over compressible and incompressible
payloads, plus truncated frames.
Measured through
arrow-ipc/benches/ipc_reader.rs, reading 10 batches end to end(decompression is only part of each figure):
Counted with the system allocator wrapped in a
GlobalAllocthat tallies everyallocation, over the same reads of 10 batches -- so these are a census, not a
sample, and the uncompressed and zstd columns are controls that do not move:
Every delta accounts for itself exactly. The counts fall by 160, 8000 and 800
against 80, 4000 and 400 compressed buffers: two allocations per buffer, the
decoder's
srcanddst, and what remains matches the zstd arm, which alreadypools its decompressor. The bytes fall by 10.00, 500.00 and 1625.00 MiB, which
is 80 and 4000 buffers times a 64 KiB
srcplus a 64 KiBdst, and -- wherethe buffers pass 256 KiB and the frames declare 4 MiB blocks -- 200 values
buffers times 8 MiB plus 200 validity buffers times 128 KiB. On that last shape
the decoder allocated 9.3 times the memory it was decoding.
Allocation count is not the whole cost:
dstis grown withVec::resize, whichzero-fills, so a 4 MiB block mode also memsets 4 MiB per buffer that the decode
then overwrites in full. The bytes column stands in for that memset, since the
same resize both allocates and zeroes.
Are there any significant user-facing changes?
No
AI usage disclosure
Generated with AI