← Back to Pull requests

perf: reuse LZ4 frame encoder state across shuffle blocks #7606

Open
unknown opened this pull request with 288 lines diff across 3 files
View on GitHub
unknown unknown commented on Nov 11, 2026 19:24 UTC
Author

Which issue does this PR close?

Part of #6916.

Rationale for this change

compress_ipc_stream builds a new lz4_flex FrameEncoder for every shuffle block. That is a zeroed 16 KiB hash table, a 64 KiB input buffer and a 72,109 byte output buffer per block, plus a copy of the IPC stream into the input buffer. Keeping one lz4_flex FrameEncoder<Vec<u8>> and copying each frame out also reuses that state, but it measured no faster than main on 8192-row blocks.

What changes are included in this PR?

  • New lz4_frame.rs: a small LZ4 frame writer on top of lz4_flex::block::compress_into_with_table that keeps its buffers and hash table across blocks. It writes standard LZ4 frames with independent 64 KiB blocks, the same layout remote shuffle already used.

  • ShuffleCodecContext holds the encoder. release_zstd is renamed to release_workspaces and drops both.

  • Local LZ4 always uses 64 KiB blocks now. Before, the block size followed the schema message, so very wide schemas got 256 KiB blocks.
  • The block format doesn’t change, and all readers are native, so old and new writers and readers can be mixed.

    How are these changes tested?

  • New unit tests: single-block frames are byte-identical to lz4_flex’s FrameEncoder, and multi-block frames round trip through FrameDecoder for several sizes and write patterns. Another test covers releasing the encoder on the remote path.

  • The reference lz4 CLI (1.9.4) decodes frames taken from real shuffle blocks.

  • cargo test -p datafusion-comet-shuffle passes, and clippy is clean.
  • Benchmarks (shuffle_writer, Threadripper PRO 5975WX, 5 alternating rounds, median):

    | Benchmark | main | this PR | change |
    |---|---:|---:|---:|
    | write encoded, 8192 rows, LZ4 | 279.5 µs | 213.2 µs | -23.7% |
    | ipc_context mixed/8192/LZ4/reused | 282.3 µs | 214.4 µs | -24.1% |
    | ipc_context flat/8192/LZ4/reused | 226.1 µs | 191.0 µs | -15.5% |
    | end to end, 2000 partitions, LZ4 (cases from #7199) | 12.39 ms | 11.52 ms | -7.0% |
    | end to end, 8000 partitions, LZ4 (cases from #7199) | 23.69 ms | 22.37 ms | -5.6% |

    At 200 partitions, and for 128-row blocks with a fresh context (what remote shuffle does), the change is within noise. Allocations per 128-row LZ4 block drop from 171,325 bytes to 17,296, the same as no compression.

    Diff: 288 lines (+216 / -72) across 3 files Reviewer: sunchao Review duration: 27802m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.40) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.40 · 288 lines diff across 3 files
    0.40
    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
    Shallow Review Depth shallow_review_depth
    0.344
    Diff Size diff_size
    0.339
    Real PR Conversation Timeline
    1 comments
    "Review active on #7606"
    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: 39.8% (MEDIUM) · Higher = larger blast radius
    39.8%
    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: 21.2% · Safe to merge
    21.2%