← Back to Pull requests

perf: remove per-partition tail copy and flush from the native shuffle writer #7234

Closed
unknown closed this pull request without merging on Oct 30, 2026 04:34 UTC
View on GitHub
unknown unknown commented on Oct 26, 2026 12:33 UTC
Author

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 BufBatchWriter per spill or finish cycle, so anything that writer does per instance is multiplied by partitions x cycles.

1. Every sub-batch_size chunk was materialized twice. PartitionedBatchIterator already emits maximal batch_size chunks plus one tail per partition. Full chunks bypass the BatchCoalescer, but the tail is copy_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 lseek per partition. finish_partition called stream_position() on the BufWriter<File>, which flushes the buffer, so every partition left as its own write(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?

  • BufBatchWriter gains a passthrough mode and BufBatchWriter::new_for_partition, which the multi-partition writers (LocalPartitionWriter and PartitionedSpill) use. The single-partition writer still coalesces, since its inputs can genuinely be small. The multi-partition writers no longer need a batch_size, so that plumbing is removed from them.

  • Passthrough does not compact view arrays (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 to Utf8). The round-robin planner's existing view-type check is unchanged.

  • BufBatchWriter::drain hands buffered bytes to the underlying writer without flushing it; flush is drain plus a flush.

  • LocalPartitionWriter tracks the output offset as a running byte total and flushes the output once in finish_all, where the total is checked against the real file position and a mismatch fails the task.

  • The contributor guide's key-class table now says only the single-partition writer coalesces.
  • 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_block pins the passthrough block boundaries and row order.

  • offsets_match_data_file_with_spilled_and_in_memory_batches writes 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.

  • The existing datafusion-comet-shuffle tests (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 no native/shuffle changes 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.

    Diff: 435 lines (+326 / -109) across 5 files Reviewer: rich7420 Review duration: 5281m
    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
    0.45
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 45.2% (MEDIUM) · Higher = larger blast radius
    45.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 17.9% · Safe to merge
    17.9%