perf: coalesce native shuffle batches before broadcast serialization #7258
All Vouch review confidence checks passed
Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 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.388
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #7258"
general
weight: 0.35
sunchao
Which issue does this PR close?
Closes #7257.
Rationale for this change
A broadcast build fed by native shuffle can receive thousands of tiny batches. Each batch crosses Arrow FFI and opens a separate serialized stream.
The shared serializer improvements from #7310 are now on main. The measurements below compare batching off and on with that serializer in both arms.
What changes are included in this PR?
Add experimental, default-off
spark.comet.exec.broadcastExchange.producerCoalesce.enabled. Direct local Comet native shuffle inputs group small decoded INT, BIGINT, BOOLEAN, STRING and BINARY batches before Arrow export and serialization. AQE partition ranges, row order and shared shuffle dependencies are preserved. Large, unsupported and singleton inputs pass through; execution stays native.Groups are bounded by rows, retained/copy estimates and input count. Conservative bitmap padding remains in the copy estimate, including for aliased Boolean columns. Reader buffers remain outside execution-pool accounting; the contributor memory guide documents their limits and lifetime. Driver broadcast assembly is unchanged.
Review cleanup shares direct-buffer validation, test frames and stream fixtures, centralizes row validation, removes constructor-only catches and overlapping/non-observing tests, and shortens documentation. Task completion and runtime-error cleanup remain. Shared benchmark mode selection lives in the inert base companion to avoid initializing another Spark session.
The mode remains default-off. The fresh wide-input control regresses, and complete-query timings are order-sensitive; broader workload benefits and concurrent-task memory measurements are still needed before default enablement.
How are these changes tested?
On
4a4185fdab, the native build used the submitted Cargo.lock. All 82 applicable Spark 3.5 native shuffle, decoder, and block reader tests passed, including AQE on/off. Six Spark 4-only tests were skipped.With a 9 MiB frame followed by small fetched blocks, the batching reader retained 128 KiB of JVM direct scratch at the next block, matching the ordinary reader. The previous batching reader retained 18 MiB at that point. The regression test checks that the larger buffer is reused within a block and that small rows from adjacent blocks still coalesce in order. A separate control verified that an invalid read-buffer setting fails before fetch construction.
The following results were collected on
48e1024bfcbefore the rebase onto0f90a7d070.[3,3,8,2]→[6,8,2]output order, nullable supported types with AQE on/off, shared dependencies and cleanup.git diff --check.48e1024bfc(38 successful checks, 17 skipped), including the Spark 4.1 SQL suites; local Clippy was not rerun.Performance measurements before the rebase and buffer fix
Measured
48e1024bfcplus #7310's serializer (2660acf816) on four pinned AMD EPYC Milan CPUs, JDK 17.0.21, Spark 3.5.9 and a 4 GiB JVM heap. The #7310 comparison overrides only compiledUtilsclasses; benchmark classes, native library, inputs and settings are otherwise identical. Class origins and native-load traces were verified, and source/artifact hashes stayed unchanged across all eight successful JVM runs.Each serializer ran in fresh JVMs in OFF/ON/OFF and ON/OFF/ON order, with warmup and at least five iterations/two measured seconds per arm. Values below are logged elapsed milliseconds divided by iteration count; ranges are ranges of arm means, not confidence intervals.
The producer benchmark runs native decode → Arrow FFI → broadcast serialization over 8,192 rows in prepared, uncompressed in-memory frames. Transport and driver assembly are excluded. Ordered values/nulls, row counts, streams and bytes are checked outside timing. The repeated 6 KiB string is a compression/byte-budget control, not representative JSON.
| Input rows per frame | #7310 serializer, batching OFF (ms) | #7310 serializer, batching ON (ms) | Streams OFF → ON |
|---|---:|---:|---:|
| 1, INT + BIGINT | 164.3–187.1 | 15.83–17.17 | 8,192 → 8 |
| 8, INT + BIGINT | 22.21–23.34 | 2.257–2.331 | 1,024 → 1 |
| 8,192, full-batch control | 0.211–0.214 | 0.215–0.217 | 1 → 1 |
| 64, plus a 6 KiB STRING | 31.98–32.08 | 35.89–60.79 | 128 → 7 |
Tiny-batch producer time improves by roughly 10x against #7310. Full-batch overhead is about 0.004–0.006 ms. The wide-string control is slower with batching in both orders, with substantial order variation; no wide-row speedup or general performance gain is claimed. One-row serialized output falls from 2,295,384 to 64,784 bytes. Decoder-only means for that input were 52.15 ms OFF / 15.16 ms ON, including native decoding, JNI/FFI, iteration and cleanup rather than isolating FFI alone.
CometBroadcastLocalShuffleBenchmarkmeasures a complete query: 65,536 build rows in 128 tasks → 64 local native shuffle partitions → broadcast hash join with 1,048,576 probe rows → count/sum collection. Every iteration regenerates the inputs and includes shuffle, broadcast assembly/distribution, join and aggregation, without cached input/stages. All four query runs returned[1048576,88351366032], verified native plans through aggregation, and reduced broadcast input streams from 8,192 to 64.With #7310 applied:
| Order | First arm (ms) | Middle arm (ms) | Last arm (ms) |
|---|---:|---:|---:|
| OFF / ON / OFF | 856.4 | 630.4 | 694.4 |
| ON / OFF / ON | 769.6 | 764.2 | 535.4 |
The OFF range is 694.4–856.4 ms and ON is 535.4–769.6 ms. These overlap; the initial reverse-order ON arm is effectively neutral versus OFF. The faster later ON arms do not establish a uniform 25–30% query improvement.
The query uses explicit broadcast, AQE off,
local[4], and the benchmark framework's on-heap mode with an unbounded native pool. These are controlled local measurements, not TPC-H, AQE conversion, general workload or production-memory evidence. Both benchmark classes accept--on-first; the producer also accepts--decode-only.The benchmark native library used the measured revision in an isolated snapshot with an alternate Cargo.lock because the registry lacked a submitted AWS dependency version at the time. Arrow 59.3.1 and DataFusion 55.2.0 are unchanged, but 43 package versions differ; the submitted Cargo.lock is untouched. These local results do not validate that lockfile. The release library uses LTO off and 16 codegen units.