← Back to Pull requests

perf: keep an idle shared broadcast decode for the next wave of tasks #7655

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Nov 16, 2026 19:54 UTC
Author

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 SharedDecode show this on main, with the bhj mode of a hash-join task-time
benchmark: a 1M-row broadcast probed by 32 tasks on local[8], so 4 waves, lz4, 7 runs. Every run had 32 reads, 4
decodes 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?

  • When the last task holding the shared decoded copy of a broadcast completes, the executor keeps the copy, and a task

  • that starts later, such as one of the next wave of tasks of the stage, reads it instead of decoding the broadcast
    again.
  • The executor frees the kept copy when Spark removes the broadcast from it, which is how Spark keeps its own broadcast

  • values. CometBroadcastValue is now AutoCloseable. Since SPARK-35396 (Spark 3.2), Spark's memory store closes an
    AutoCloseable value when its block leaves memory: when the broadcast is unpersisted or destroyed, when the driver
    cleans it up, when the memory store evicts it, and when the executor stops. After close, the value frees each copy
    as soon as no task holds it, because Spark's cache of broadcast values can still hand it to a later task.
  • A new config, spark.comet.exec.broadcastExchange.shareDecoded.maxIdleSize, 32 MiB by default, caps the bytes of

  • the 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 main does. SharedDecode.IdleDecodes keeps the copies oldest first. It takes no other lock, and
    evicted copies are freed outside every lock.
  • An entry is freed only while it is still the one that the broadcast hands out, so an eviction that runs after another

  • path freed the entry frees nothing.
  • CometBroadcastExchangeExec reads shareDecoded.enabled and the cap on the driver, and passes them through

  • CometBatchRDD as an Option[SharedDecode.Retention], which is None when sharing is disabled. batches has no
    default for it.
  • CometTestBase sets the cap to 0. Some suites compare CometArrowAllocator's memory before and after a step, and

  • the kept copy of an earlier test's broadcast could be freed in between, whenever Spark cleans up that broadcast.
  • The config docs, the broadcast join section of the tuning guide, the memory tuning guide, and the memory management

  • 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. unpersist cannot be that message: it skips the driver's block
    manager, 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 stage
    whose 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
    main column 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 (as main) | 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) ran
    twice in each session, the second time while the copies that the first kept were still kept. A thread sampled
    CometArrowAllocator every millisecond and counted a decode by the allocator it creates. Median of 7 runs, with the
    arms 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
    CometArrowAllocator at 100.8 MB in the first query of every arm. In the second query the copies kept from the first
    add 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?

  • A new test in CometBroadcastValueSuite checks that a task that comes after the last holder completes reads the kept

  • copy 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.
  • A new test reads a real broadcast through CometBatchRDD, checks that the executor keeps its copy, drops the

  • broadcast's block through BlockManager.removeBlock, and checks that the memory store closed the value and freed
    the copy. Eviction, unpersist, destroy and the driver's cleanup all remove the block from the memory store this way.
  • The test of broadcast joins over nested columns now runs with a positive cap. After a hash join and a nested loop

  • join over eight tasks, it checks that the allocator still holds the copy, destroys the broadcasts, and checks that
    the copies are freed.
  • A new test of the cap, with its own IdleDecodes, checks that keeping a copy frees the one kept longest, that a copy

  • over 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.
  • The benchmark numbers above. Neither benchmark is in the tree yet. The task-time one will come as its own PR.
  • Diff: 650 lines (+487 / -163) across 8 files Reviewer: sunchao Review duration: 20572m
    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
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    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.388
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #7655"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% 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: 52% (MEDIUM) · Higher = larger blast radius
    52%
    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: 46.7% · Lower = rushed or shallow review
    46.7%
    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: 27.7% · Safe to merge
    27.7%