perf: keep an idle shared broadcast decode for the next wave of tasks #7655
All Vouch review confidence checks passed
Residual risk 0.28 = Change Risk (0.52) × (1 − Review Confidence (0.47)).
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.02 · 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.344
Real PR Conversation Timeline
1 comments
"Review active on #7655"
general
weight: 0.35
sunchao
Which issue does this PR close?
Closes #7487.
Part of #6013.
Rationale for this change
#7016 lets the tasks of an executor that read a Comet broadcast at the same time share one decoded copy of it, and
frees the copy when the last of them completes. The tasks of a wave finish at about the same time, so the copy is gone
before the next wave starts, and every wave decodes the broadcast again. Every task of a wave also waits for that decode.
Counters added temporarily to
SharedDecodeshow this onmain, with thebhjmode of a hash-join task-timebenchmark: a 1M-row broadcast probed by 32 tasks on
local[8], so 4 waves, lz4, 7 runs. Every run had 32 reads, 4decodes and 4 frees. 28 tasks waited about 14 ms each, which is the time of the decode they waited for. Each new decode
started 0.6 to 1.3 ms after the copy before it was freed.
What changes are included in this PR?
that starts later, such as one of the next wave of tasks of the stage, reads it instead of decoding the broadcast
again.
values.
CometBroadcastValueis nowAutoCloseable. Since SPARK-35396 (Spark 3.2), Spark's memory store closes anAutoCloseablevalue when its block leaves memory: when the broadcast is unpersisted or destroyed, when the drivercleans it up, when the memory store evicts it, and when the executor stops. After
close, the value frees each copyas soon as no task holds it, because Spark's cache of broadcast values can still hand it to a later task.
spark.comet.exec.broadcastExchange.shareDecoded.maxIdleSize, 32 MiB by default, caps the bytes ofthe kept copies of all broadcasts on an executor. A copy larger than the cap is freed as soon as no task holds it,
and keeping a copy frees the copies kept longest until the rest fit. A cap of 0 frees every copy as soon as no task
holds it, as
maindoes.SharedDecode.IdleDecodeskeeps the copies oldest first. It takes no other lock, andevicted copies are freed outside every lock.
path freed the entry frees nothing.
CometBroadcastExchangeExecreadsshareDecoded.enabledand the cap on the driver, and passes them throughCometBatchRDDas anOption[SharedDecode.Retention], which isNonewhen sharing is disabled.batcheshas nodefault for it.
CometTestBasesets the cap to 0. Some suites compareCometArrowAllocator's memory before and after a step, andthe kept copy of an earlier test's broadcast could be freed in between, whenever Spark cleans up that broadcast.
guide describe the kept copy, which is JVM-side Arrow memory that neither Spark nor Comet's pools account for.
Keeping the copy until Spark removes the broadcast matches Spark, but Spark counts its broadcast values in storage
memory, and nothing counts this copy. The driver cleans up a broadcast only after it garbage-collects it, which can be
long after the query, so in a long-running application the cap is what bounds the kept copies. A value that never fits
in the executor's memory store is never closed, and only the cap bounds its copy. Freeing the copy when the query ends
would need a message from the driver to every executor.
unpersistcannot be that message: it skips the driver's blockmanager, which is the only one in local mode, and it drops the executors' copies of the broadcast as well.
Each query builds its own broadcasts, so a copy kept after its query is rarely read again, and a cap larger than the
broadcasts of the stage that is running mostly keeps such copies longer. That is why the default is 32 MiB, which is
8% of Spark's 384 MiB overhead floor. A broadcast that decodes to more than the cap decodes as on
main, and a stagewhose broadcasts together decode to more than the cap keeps only as many as fit.
The task times below come from an earlier revision of this PR, which freed a kept copy 1 s after its last task. Its
maincolumn ran with that timeout set to 0, which frees a copy as soon as no task holds it, as a cap of 0 does now.Within a run the waves are milliseconds apart, so this revision keeps the same copy, and I expect the same task times,
but I have not re-run this benchmark. Mean time per probe task in the same benchmark, median of 7 runs with the arms
interleaved:
| codec | Spark | Comet, this PR | Comet,
maxIdleSize=0(asmain) | decodes per run || ------------- | -----: | -------------: | ---------------------------------: | --------------: |
| lz4 | 7.1 ms | 9.0 ms | 18.8 ms | 4 to 1 |
| zstd, level 5 | 7.9 ms | 12.5 ms | 31.8 ms | 4 to 1 |
The first wave still waits for its decode. A cheaper decode would shrink that wait (#2216, #3762).
The kept copies of broadcasts read in consecutive stages stack, and the copies of one query are still kept when the
next starts. On this revision, a query with four broadcast hash joins in consecutive stages (a 4M-row fact repartitioned
into 32 tasks before each join, four 1.5M-row dims of 18.4 MB decoded each,
local[8], AQE off, Spark 4.1, lz4) rantwice in each session, the second time while the copies that the first kept were still kept. A thread sampled
CometArrowAllocatorevery millisecond and counted a decode by the allocator it creates. Median of 7 runs, with thearms interleaved and a new session for each run:
| cap | decodes per query | kept after the first query | peak copies, second query | peak allocator, second query |
| --------------------------- | ----------------: | -------------------------: | ------------------------: | ---------------------------: |
| 0 (as
main) | 7 | 0 | 18.4 MB (1) | 100.8 MB || 32 MiB (the default) | 4 | 18.4 MB (1) | 36.8 MB (2) | 119.2 MB |
| 64 MiB | 4 | 55.1 MB (3) | 73.5 MB (4) | 155.9 MB |
| 128 MiB (the earlier value) | 4 | 73.5 MB (4) | 147.0 MB (8) | 174.3 MB |
Every cap above 0 decodes each broadcast once, because each stage reads only its own broadcast, so a cap that holds
one copy keeps the whole saving here. JVM-side Arrow memory other than the shared copies puts the peak of
CometArrowAllocatorat 100.8 MB in the first query of every arm. In the second query the copies kept from the firstadd to that peak: the copy that the running stage holds plus the kept ones before the cap evicts them, 18.4 MB at
32 MiB and 73.5 MB at 128 MiB. The median query times were between 1385 and 1486 ms in every arm, within the spread
between runs. I did not profile where this query spends its time.
How are these changes tested?
CometBroadcastValueSuitechecks that a task that comes after the last holder completes reads the keptcopy without decoding again, that closing the value while a task holds the copy frees it once that task completes,
and that a task that reads the closed value decodes again and frees its copy when it completes.
CometBatchRDD, checks that the executor keeps its copy, drops thebroadcast's block through
BlockManager.removeBlock, and checks that the memory store closed the value and freedthe copy. Eviction, unpersist, destroy and the driver's cleanup all remove the block from the memory store this way.
join over eight tasks, it checks that the allocator still holds the copy, destroys the broadcasts, and checks that
the copies are freed.
IdleDecodes, checks that keeping a copy frees the one kept longest, that a copyover the cap is freed at once, that a task taking a kept copy takes it out of the cap, and that closing the values
frees what is still kept.