Skip to content

Use sync.Pool for mergeIterator batchesBuf - #7765

Open
PaurushGarg wants to merge 1 commit into
cortexproject:masterfrom
PaurushGarg:fix/shared-batches-buf-v2
Open

Use sync.Pool for mergeIterator batchesBuf#7765
PaurushGarg wants to merge 1 commit into
cortexproject:masterfrom
PaurushGarg:fix/shared-batches-buf-v2

Conversation

@PaurushGarg

@PaurushGarg PaurushGarg commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

What this PR does:
Each mergeIterator allocates its own batchesBuf at creation time. This adds to the heap profile for query engines that create all iterators upfront, especially with high-cardinality queries - while only one buffer is actively used at a time since series are processed sequentially.

This PR replaces per-iterator allocation with a sync.Pool. Each buildNextBatch call borrows a scratch buffer, merges into it, copies the result into c.batches, and returns the buffer via defer. Idle iterators hold no buffer.

Checklist

  • Tests updated
  • Documentation added
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX]
  • docs/configuration/v1-guarantees.md updated if this PR introduces experimental flags

@PaurushGarg
PaurushGarg force-pushed the fix/shared-batches-buf-v2 branch 3 times, most recently from 9706212 to 925fa11 Compare August 17, 2026 19:35
@pull-request-size pull-request-size Bot added size/M and removed size/L labels Aug 17, 2026
@PaurushGarg
PaurushGarg force-pushed the fix/shared-batches-buf-v2 branch 3 times, most recently from 3b7ba40 to 2fdb8d2 Compare August 17, 2026 20:28
@PaurushGarg
PaurushGarg marked this pull request as ready for review August 17, 2026 20:37
@PaurushGarg PaurushGarg changed the title share batchesBuf across merge iterators via MergeWorkspace Use sync.Pool for mergeIterator batchesBuf to reduce memory usage Aug 17, 2026
@PaurushGarg PaurushGarg changed the title Use sync.Pool for mergeIterator batchesBuf to reduce memory usage Use sync.Pool for mergeIterator batchesBuf Aug 17, 2026
Replace per-iterator batchesBuf with a sync.Pool. Each buildNextBatch
call borrows a scratch buffer from the pool, merges into it, copies the
result into c.batches, and returns the buffer immediately. Idle
iterators hold no buffer at all.

This reduces heap usage from O(series) to O(concurrent_evaluations) —
saving ~1200 bytes per series (~11 GB at 9M series). The pool approach
is engine-agnostic: safe for both sequential and parallel series
processing within a single Select call.

Signed-off-by: Paurush Garg <paurushg@amazon.com>
@PaurushGarg
PaurushGarg force-pushed the fix/shared-batches-buf-v2 branch from 2fdb8d2 to 32e9cb7 Compare August 17, 2026 20:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant