Repository navigation
perf(arrow-ipc): reuse lz4 frame encoder buffers across IPC writes - #12084
Conversation
47e6d6f to
29cc3dd
Compare
Adds single-threaded and multi-threaded StreamWriter/StreamEncoder benchmark cases using LZ4_FRAME compression, to establish a baseline for upcoming lz4 encoder optimizations.
29cc3dd to
add5d56
Compare
Caches the lz4_flex FrameEncoder (including its hash table and block buffers) in IpcWriteContext and reuses it across compress_lz4 calls, instead of allocating a fresh encoder per buffer. The block size is pinned to 256KB so a reused encoder's block size is not fixed by the first buffer written. Mirrors the existing zstd context reuse.
add5d56 to
6bc9994
Compare
|
will merge up and run the benchmarks when the benchmarks are merged |
…he#12083) Adds some new benches for helping qualify a future change in apache#12084. The new benchmarks are some `StreamWriter`/`StreamEncoder` cases using `CompressionType::LZ4_FRAME`, including single-threaded and multi-threaded (contended) variants.
|
run benchmark ipc_writer |
|
🤖 Benchmark starting (GKE) | trigger Target: ipc_writer Sharding factor: 1 (1 workers). Comparing ipc-lz4-reuse-frame-buffers (cb0c640) to 3ac3836 (merge-base) diff Run configurationrun benchmark ipc_writer
env:
BENCH_FILTER: StreamEncoder/encode_10/lz4/
shards: 1
Results will be posted when all workers finish. File an issue against this benchmark runner |
|
run benchmark ipc_writer |
|
🤖 Benchmark starting (GKE) | trigger Target: ipc_writer Sharding factor: 1 (1 workers). Comparing ipc-lz4-reuse-frame-buffers (cb0c640) to 3ac3836 (merge-base) diff Run configurationrun benchmark ipc_writer
env:
BENCH_FILTER: StreamEncoder/encode_10/lz4/
shards: 1
Results will be posted when all workers finish. File an issue against this benchmark runner |
|
🤖 Benchmark completed (GKE) | trigger Target: ipc_writer Comparing ipc-lz4-reuse-frame-buffers (cb0c640) to 3ac3836 (merge-base) diff Run configurationrun benchmark ipc_writer
env:
BENCH_FILTER: StreamEncoder/encode_10/lz4/
shards: 1
DetailsPer-runner informationipc_writer — shard 1/1Node: gk3-benchmark-cluster-nap-1ghb8pp5-d2cf91a9-x967 Instance: c4a-highmem-16 (12 vCPU / 65 GiB) uname: BENCH_COMMAND: cargo bench --features=arrow,async,test_common,experimental,object_store --bench ipc_writerCPU Details (lscpu)Resource Usagebase (merge-base)
branch
File an issue against this benchmark runner |
|
🤖 Benchmark completed (GKE) | trigger Target: ipc_writer Comparing ipc-lz4-reuse-frame-buffers (cb0c640) to 3ac3836 (merge-base) diff Run configurationrun benchmark ipc_writer
env:
BENCH_FILTER: StreamEncoder/encode_10/lz4/
shards: 1
DetailsPer-runner informationipc_writer — shard 1/1Node: gk3-benchmark-cluster-nap-1ghb8pp5-d2cf91a9-6t9b Instance: c4a-highmem-16 (12 vCPU / 65 GiB) uname: BENCH_COMMAND: cargo bench --features=arrow,async,test_common,experimental,object_store --bench ipc_writerCPU Details (lscpu)Resource Usagebase (merge-base)
branch
File an issue against this benchmark runner |
|
run benchmark ipc_writer |
|
🤖 Benchmark starting (GKE) | trigger Target: ipc_writer Sharding factor: 1 (1 workers). Comparing ipc-lz4-reuse-frame-buffers (cb0c640) to 3ac3836 (merge-base) diff Run configurationrun benchmark ipc_writer
env:
BENCH_FILTER: StreamEncoder/encode_10/lz4
resources:
cpu: '4'
shards: 1
Results will be posted when all workers finish. File an issue against this benchmark runner |
|
run benchmark ipc_writer |
|
🤖 Benchmark starting (GKE) | trigger Target: ipc_writer Sharding factor: 1 (1 workers). Comparing ipc-lz4-reuse-frame-buffers (cb0c640) to 3ac3836 (merge-base) diff Run configurationrun benchmark ipc_writer
env:
BENCH_FILTER: StreamEncoder/encode_10/lz4/ro
resources:
cpu: '8'
shards: 1
Results will be posted when all workers finish. File an issue against this benchmark runner |
|
🤖 Benchmark completed (GKE) | trigger Target: ipc_writer Comparing ipc-lz4-reuse-frame-buffers (cb0c640) to 3ac3836 (merge-base) diff Run configurationrun benchmark ipc_writer
env:
BENCH_FILTER: StreamEncoder/encode_10/lz4/ro
resources:
cpu: '8'
shards: 1
DetailsPer-runner informationipc_writer — shard 1/1Node: gk3-benchmark-cluster-nap-gqsla0c0-ff8771d2-s6l4 Instance: c4a-highmem-16 (8 vCPU / 65 GiB) uname: BENCH_COMMAND: cargo bench --features=arrow,async,test_common,experimental,object_store --bench ipc_writerCPU Details (lscpu)Resource Usagebase (merge-base)
branch
File an issue against this benchmark runner |
|
🤖 Benchmark completed (GKE) | trigger Target: ipc_writer Comparing ipc-lz4-reuse-frame-buffers (cb0c640) to 3ac3836 (merge-base) diff Run configurationrun benchmark ipc_writer
env:
BENCH_FILTER: StreamEncoder/encode_10/lz4
resources:
cpu: '4'
shards: 1
DetailsPer-runner informationipc_writer — shard 1/1Node: gk3-benchmark-cluster-nap-hr9recpb-6c82a1a3-s6dz Instance: c4a-highmem-16 (4 vCPU / 65 GiB) uname: BENCH_COMMAND: cargo bench --features=arrow,async,test_common,experimental,object_store --bench ipc_writerCPU Details (lscpu)Resource Usagebase (merge-base)
branch
File an issue against this benchmark runner |
|
run benchmark ipc_writer |
|
🤖 Benchmark starting (GKE) | trigger Target: ipc_writer Sharding factor: 1 (1 workers). Comparing ipc-lz4-reuse-frame-buffers (cb0c640) to 3ac3836 (merge-base) diff Run configurationrun benchmark ipc_writer
env:
BENCH_FILTER: StreamEncoder/encode_10/lz4/ro
resources:
cpu: '2'
shards: 1
Results will be posted when all workers finish. File an issue against this benchmark runner |
|
run benchmark ipc_writer |
|
🤖 Benchmark starting (GKE) | trigger Target: ipc_writer Sharding factor: 1 (1 workers). Comparing ipc-lz4-reuse-frame-buffers (cb0c640) to 3ac3836 (merge-base) diff Run configurationrun benchmark ipc_writer
env:
BENCH_FILTER: StreamEncoder/encode_10/lz4/ro
resources:
cpu: '1'
shards: 1
Results will be posted when all workers finish. File an issue against this benchmark runner |
|
just a bit curious 🤔 |
|
🤖 Benchmark completed (GKE) | trigger Target: ipc_writer Comparing ipc-lz4-reuse-frame-buffers (cb0c640) to 3ac3836 (merge-base) diff Run configurationrun benchmark ipc_writer
env:
BENCH_FILTER: StreamEncoder/encode_10/lz4/ro
resources:
cpu: '2'
shards: 1
DetailsPer-runner informationipc_writer — shard 1/1Node: gk3-benchmark-cluster-nap-1sbiwga9-ed3aa907-2bkt Instance: c4a-highmem-16 (2 vCPU / 65 GiB) uname: BENCH_COMMAND: cargo bench --features=arrow,async,test_common,experimental,object_store --bench ipc_writerCPU Details (lscpu)Resource Usagebase (merge-base)
branch
File an issue against this benchmark runner |
|
🤖 Benchmark completed (GKE) | trigger Target: ipc_writer Comparing ipc-lz4-reuse-frame-buffers (cb0c640) to 3ac3836 (merge-base) diff Run configurationrun benchmark ipc_writer
env:
BENCH_FILTER: StreamEncoder/encode_10/lz4/ro
resources:
cpu: '1'
shards: 1
DetailsPer-runner informationipc_writer — shard 1/1Node: gk3-benchmark-cluster-nap-1emaqzpa-d9be8adf-mmmj Instance: c4a-highmem-16 (1 vCPU / 65 GiB) uname: BENCH_COMMAND: cargo bench --features=arrow,async,test_common,experimental,object_store --bench ipc_writerCPU Details (lscpu)Resource Usagebase (merge-base)
branch
File an issue against this benchmark runner |
Rich-T-kid
left a comment
There was a problem hiding this comment.
benchmark results look very strong!
this PR looks good, left a few suggestions
|
|
||
| #[test] | ||
| #[cfg(feature = "lz4")] | ||
| fn test_lz4_compression_reuses_context() { |
There was a problem hiding this comment.
This verifies correctness across repeated frames, but it would also pass if every call created a fresh encoder. Is there a stable way to assert that the same cached compressor is retained across calls? maybe we could assert the capacity?
we could also keep this test and rename it to test_lz4_compression_multiple_frames and create a separate test to assert the allocation
There was a problem hiding this comment.
🤔 FrameEncoder does not expose an API to inspect its internal buffer, so I don't think we can assert over the capacity. I could start comparing pointer addresses of the FrameEncoder, but I fear that'd make a brittle test in case someone changes something in the future.
What comes to mind is:
- Rename the test as you suggested, and rely on it for ensuring correctness rather than reuse
- Rely on the benchmark added in the previous PR for ensuring reuse (no reuse would show worst performance)
There was a problem hiding this comment.
yea I just took a look at their docs and I cant find anything we could use to assert this. any alternative does seem quite hacky. I think relying on the benchmarks should be fine.
|
thanks @gabotechs |
Allow callers to choose 64 KiB / 256 KiB / 1 MiB / 4 MiB lz4 blocks via IpcWriteOptions, keeping 256 KiB as the default pinned by apache#12084. 8 MiB is excluded because the frame header's 3-bit block size code cannot represent it without setting the reserved bit, which the decoder rejects. Generated-by: MiMo via OpenCode
Allow callers to choose 64 KiB / 256 KiB / 1 MiB / 4 MiB lz4 blocks via IpcWriteOptions, keeping 256 KiB as the default pinned by apache#12084. 8 MiB is excluded because the frame header's 3-bit block size code cannot represent it without setting the reserved bit, which the decoder rejects. Generated-by: MiMo via OpenCode
# Which issue does this PR close? - Closes apache#12105 # Rationale for this change apache#12084 pinned the reusable LZ4 frame encoder to a 256 KiB block size so a reused encoder’s block size is not decided by the first buffer it writes. Callers who want to trade retained encoder buffers against compression ratio currently have no way to choose a different size. This change lets callers configure the block size while keeping 256 KiB as the default, so existing output remains unchanged. # What changes are included in this PR? - Adds `IpcWriteOptions::try_with_lz4_block_size(Option<usize>)`, following the existing `try_with_compression_level` pattern with a private field and validating builder. - Supports 64 KiB, 256 KiB, 1 MiB, and 4 MiB, which are the block sizes the LZ4 frame header can represent. The option requires metadata V5 or newer and returns an error when used with ZSTD. - Updates `IpcWriteContext` to store the selected size and discard its cached encoder when that size changes. The setting is synced through the shared record batch and dictionary batch encoding path. - Preserves the selected size when `Lz4Compressor` rebuilds after a mid-frame error. - Does not support 8 MiB: `lz4_flex` sets a reserved frame-header bit for that size, which its decoder rejects. # Are these changes tested? Tests cover the supported frame-header size codes and roundtrips, byte-identical output for the default 256 KiB setting, and switching block sizes while reusing an `IpcWriteContext`. They also cover unsupported sizes, metadata V4, ZSTD in both builder-call orders, and end-to-end roundtrips with nulls and a dictionary column. # Are there any significant user-facing changes? Yes. This adds the public `IpcWriteOptions::try_with_lz4_block_size` method so callers can select an LZ4 frame block size. The addition is non-breaking; when the option is unset, the 256 KiB default and encoded output are unchanged. # AI usage disclosure Codex Desktop and OpenCode assisted with implementation, test updates, and reviewer-driven revisions. I guided the changes based on the issue and reviewer feedback. The branch was checked with local tests, formatting, and lint.
…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]>
Something that showed up during some profiling of a downstream project that depends on
arrow-rs, hopefully the benchmarks in #12083 can be used for helping qualify this change (review that PR first)lz4_flexFrameEncoder(hash table + block buffers) inIpcWriteContextand reuses it acrosscompress_lz4calls instead of allocating a fresh encoder per buffer, mirroring the existing zstd context reuse.Benchmark results
Measured on a 16-core macOS machine, comparing the base of this stack (#12083, no encoder reuse) against this PR:
cargo bench -p arrow-ipc --bench ipc_writer --features lz4,zstd -- lz4StreamWriter/write_10/lz4