perf: remove per-partition tail copy and flush from the native shuffle writer #7234
All Vouch review confidence checks passed
Residual risk 0.18 = Change Risk (0.45) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.45 · 435 lines diff across 5 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.364
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #7234"
general
weight: 0.35
rich7420
Which issue does this PR close?
Closes #7071.
Rationale for this change
In a multi-partition native shuffle, every output partition gets its own short-lived
BufBatchWriterper spill or finish cycle, so anything that writer does per instance is multiplied bypartitions x cycles.1. Every sub-
batch_sizechunk was materialized twice.PartitionedBatchIteratoralready emits maximalbatch_sizechunks plus one tail per partition. Full chunks bypass theBatchCoalescer, but the tail iscopy_rows'd into in-progress builders and re-emitted as the same block. There is never a second batch for the tail to coalesce with, so the copy buys nothing. With many partitions almost every chunk is a tail.2. A flush and an
lseekper partition.finish_partitioncalledstream_position()on theBufWriter<File>, which flushes the buffer, so every partition left as its ownwrite(2)plus a seek and the 1 MiB output buffer never coalesced small partitions.This supersedes the draft #5908.
What changes are included in this PR?
BufBatchWritergains a passthrough mode andBufBatchWriter::new_for_partition, which the multi-partition writers (LocalPartitionWriterandPartitionedSpill) use. The single-partition writer still coalesces, since its inputs can genuinely be small. The multi-partition writers no longer need abatch_size, so that plumbing is removed from them.Utf8View,BinaryView). The coalescer would only have helped the tail of a top-level view column, because full chunks already bypass it, and Comet does not shuffle view types today (the planner casts them back toUtf8). The round-robin planner's existing view-type check is unchanged.BufBatchWriter::drainhands buffered bytes to the underlying writer without flushing it;flushisdrainplus a flush.LocalPartitionWritertracks the output offset as a running byte total and flushes the output once infinish_all, where the total is checked against the real file position and a mismatch fails the task.Block boundaries and the file format are unchanged: the multi-partition path wrote one block per chunk before too, just after an extra copy.
How are these changes tested?
passthrough_writes_each_batch_as_its_own_blockpins the passthrough block boundaries and row order.offsets_match_data_file_with_spilled_and_in_memory_batcheswrites partitions with a spilled prefix and in-memory tail, then decodes every block inside each index range, so a wrong arithmetic offset fails to parse.datafusion-comet-shuffletests (192) and clippy pass.Benchmark,
shuffle_writer_high_partition(81,920 rows, 4-column schema, codec None, hash partitioning), this branch against apache/main (6fe73d9be8, which has nonative/shufflechanges since the branch point). Both built with the bench profile (lto=false,codegen-units=16) and run alternately, three rounds each; the table is the median of the three Criterion estimates:| Partitions | main | This PR | Change |
|---:|---:|---:|---:|
| 200 | 3.66 ms | 3.07 ms | -16% |
| 2000 | 13.32 ms | 8.06 ms | -39% |
| 8000 | 30.96 ms | 17.88 ms | -42% |
The machine was shared (load 6-11), and one 8000-partition run on the PR was an outlier at 35.9 ms; its other two runs were 15.6 and 17.9 ms, against 30.2 to 32.4 ms for main.