Skip to content

fix: avoid buffer resize cost in shuffle reader during InMemoryPayload merge#546

Open
zhangxffff wants to merge 6 commits intobytedance:mainfrom
zhangxffff:fix/shuffle_reader_slow
Open

fix: avoid buffer resize cost in shuffle reader during InMemoryPayload merge#546
zhangxffff wants to merge 6 commits intobytedance:mainfrom
zhangxffff:fix/shuffle_reader_slow

Conversation

@zhangxffff
Copy link
Copy Markdown
Collaborator

@zhangxffff zhangxffff commented May 8, 2026

What problem does this PR solve?

Issue Number: close #545

Type of Change

  • 🐛 Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality)
  • 🚀 Performance improvement (optimization)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)
  • 🔨 Refactoring (no logic changes)
  • 🔧 Build/CI or Infrastructure changes
  • 📝 Documentation only

Description

BoltColumnarBatchDeserializer::next() previously merged each inbound BlockPayload into a running merged_ InMemoryPayload immediately on arrival. With N small payloads consolidating into one output batch, this triggered N−1 calls to InMemoryPayload::merge(), and each call reallocated the destination buffer to exactly the new size — Arrow's PoolBuffer::Reserve does not grow capacity geometrically. The result was O(N) Arrow reallocations and O(N²) bytes copied per output batch.

This PR accumulate inbound payloads size, prealloc enough size and merge when the batch is ready.

Benchmark case: 4 columns with 1024 length StringView, each payload has 4 StringView. Read cost reduce from 14.68s to 131.26ms.

Performance Impact

  • No Impact: This change does not affect the critical path (e.g., build system, doc, error handling).

  • Positive Impact: I have run benchmarks.

    Click to view Benchmark Results
  • Negative Impact: Explained below (e.g., trade-off for correctness).

Release Note

Please describe the changes in this PR

Release Note:

Release Note:
fix shuffle reader performance issue caused by memory reallocate

Checklist (For Author)

  • I have added/updated unit tests (ctest).
  • I have verified the code with local build (Release/Debug).
  • I have run clang-format / linters.
  • (Optional) I have run Sanitizers (ASAN/TSAN) locally for complex C++ changes.
  • No need to test or manual test.

Breaking Changes

  • No

  • Yes (Description: ...)

    Click to view Breaking Changes
    Breaking Changes:
    - Description of the breaking change.
    - Possible solutions or workarounds.
    - Any other relevant information.
    

@zhangxffff zhangxffff changed the title Fix/shuffle reader slow fix: BoltColumnarBatchDeserializer: avoid buffer resize cost during InMemoryPayload merge small payloads May 8, 2026
@zhangxffff zhangxffff changed the title fix: BoltColumnarBatchDeserializer: avoid buffer resize cost during InMemoryPayload merge small payloads fix: avoid buffer resize cost in shuffle reader during InMemoryPayload merge May 8, 2026
@zhangxffff zhangxffff requested a review from guhaiyan0221 May 9, 2026 06:42
@zhangxffff zhangxffff force-pushed the fix/shuffle_reader_slow branch from 71926d4 to eca5241 Compare May 11, 2026 08:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Shuffle reader: O(N²) memcpy when merging small payloads into a batch

2 participants