Skip to content

perf(index): bound merge_segments prefetch to one scan's byte budget - #9761

Merged
westonpace merged 4 commits into
lance-format:mainfrom
rerun-io:upstream/merge-segments-io-budget
Oct 13, 2026
Merged

westonpace merged 4 commits into
lance-format:mainfrom
rerun-io:upstream/merge-segments-io-budget

Conversation

@amunra

@amunra amunra commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Problem

BTreeIndex::merge_segments builds every source's stream before consuming any of them:

for (segment, old_data_filter) in segments.iter().zip(old_data_filters) {
    let stream = segment.data_stream().await?;
    ...
    inputs.push(Arc::new(OneShotExec::new(stream)));
}

data_stream reads a segment's index file through page_stream, which calls
IndexReader::read_record_batch_stream. Since #9463, read_record_batch_stream reads the
whole file under a single decode plan rather than issuing one read per btree page.

A single decode plan schedules every one of its reads up front, before the consumer polls
the stream at all. The bytes that plan holds in flight are therefore capped by the
scheduler's byte budget, not by how far readahead lets the stream run ahead. Each segment
owns an IndexStore, each IndexStore owns a ScanScheduler, and each ScanScheduler
carries a byte budget of its own.

Merging N segments therefore holds N full budgets at once. SchedulerConfig::max_bandwidth
is 32 MiB per I/O thread, so at the cloud default of 64 threads that is 2 GiB per source,
none of it visible to the DataFusion memory pool.

Change

  • IndexStore::with_io_buffer_size(bytes) returns a store whose reads are bounded by
    bytes of outstanding data. LanceIndexStore implements it by building a scheduler with
    the smaller budget over the same object store. Unlike with_io_priority, the scheduler
    cannot be shared: the budget belongs to the scheduler.
  • SchedulerConfig::max_bandwidth_bytes(io_parallelism) exposes the budget
    max_bandwidth would hand a store, so the merge divides the real number rather than a
    second copy of the formula.
  • merge_segments counts the sources it will actually read (those not skipped by
    filter_keeps_nothing), divides one scan's budget between them, and rescopes each
    segment's store before building its stream — which is where prefetch starts. The source
    count and the I/O parallelism both come from those sources alone: segments[0] may itself
    be skipped, and segments under different base paths carry different parallelism.
  • The per-source share has a floor of one storage page (32 MiB), so a source fetches a whole
    page at a time rather than trickling. That is a throughput floor, not a liveness one: the
    scheduler admits a pending request whenever nothing of lower priority is in flight, so a
    source makes progress at any budget.

The aggregate is therefore max(one scan's budget, num_sources * 32 MiB). The division
conserves one scan's budget only while num_sources <= io_parallelism — 64 sources on
cloud storage, but 8 on local. Past that the floor binds and the total grows with the
source count, so a caller wanting a fixed total has to merge in several passes. That is far
better than the N full budgets the current code holds, but it is not a constant bound.

Compatibility

with_io_buffer_size is required, not defaulted, so an out-of-tree IndexStore will
not compile until it adds one. A defaulted no-op would let a caller ask for a bound,
silently not get one, and have no way to tell; with_io_priority beside it is required for
the same reason. A store that prefetches nothing may still return itself — it just has to
say so.

Every other IndexStore in the tree is a test double over a real store, so each one
forwards and the question does not arise for them.

No file format change, and no existing signature changes.

Testing

cargo test -p lance-index -p lance-index-core -p lance-io --lib passes (2011 / 9 / 323);
cargo clippy --all --tests --benches -- -D warnings is clean.

  • Plumbing — three trained segments merge with the middle one's filter keeping nothing.
    A recording IndexStore asserts the two read sources are each rescoped exactly once, to
    half of one scan's budget, and that the skipped source is never rescoped at all. Verified
    the test fails both ways: removing the rescoping call leaves the recorder empty, and
    counting the skipped source divides three ways (89478485) instead of two (134217728).
  • Arithmetic — one source gets what a single scan would have had; 2/4/8/16/32/64 sources
    still total one scan's budget between them; the floor binds from the 9th source at local
    I/O parallelism; and degenerate inputs yield the floor rather than 0, which the scheduler
    reads as "no backpressure" rather than "the smallest budget". Verified the test fails when
    the division is removed: two sources then total 4 GiB where one scan would have held 2 GiB.

PR history

This began as rerun-io#63 on our release-10.0.0 line, offered upstream as #9289:
whole-index btree passes were reading one page per request, and that PR replaced them with a
single decode plan per scan.

While #9289 was open, main arrived at the same place independently — #9463 streams btree
pages sequentially through read_record_batch_stream, #9310 coalesces page loads into one
read_ranges, and #9462 assembles search results once rather than per page. Between them
they cover what #9289 was for, so it is closed.

One piece did not come across. Reading a whole index file under a single decode plan moves
the bound on in-flight bytes from readahead to the scheduler's byte budget, and that budget
is per store. merge_segments opens one store per source and builds every source's stream
before consuming any, so it now holds one full budget per source — on main today, with no
rescoping anywhere. That is the OOM this PR prevents, and it is all this PR does; the
streaming work it was originally bundled with is already in main.

amunra and others added 3 commits October 7, 2026 16:11
Every source stream in `merge_segments` is built before any is consumed, and
`data_stream` now schedules a whole-file read eagerly. Each segment carries its
own store, so a merge held one full byte budget per source at once -- 32MiB per
IO thread each, 2GiB per source at the cloud default -- none of it visible to the
DataFusion memory pool.

`IndexStore::with_io_buffer_size` (default: return the store unchanged) returns a
store whose reads are bounded by a given number of outstanding bytes.
`merge_segments` divides one scan's budget between the sources it actually reads,
with a floor of one storage page each so every source can still make progress.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The default returned the store unchanged, so a caller asking for a byte
bound could silently not get one with no way to tell. Matches
`with_io_priority` beside it, which has no default either.

The eight other implementations in the tree are all test doubles wrapping
a real store; each forwards to its inner store as it already does for
`with_io_priority`. The one with no backing scheduler returns itself,
stated explicitly at the site that knows it is correct.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
- `MERGE_BYTES_PER_IO_THREAD` was documented as a liveness floor ("so a
  source can always make progress"). It is not one: the scheduler admits a
  pending request whenever nothing of lower priority is in flight, so a
  source progresses at any budget. Renamed to `MERGE_MIN_BYTES_PER_SOURCE`
  and documented as the throughput floor it is. The old name also conflated
  the floor with the per-I/O-thread multiplier, which are now separate.

- `merge_io_buffer_size` recomputed `SchedulerConfig::max_bandwidth`'s
  formula from a private copy of the constant, so the two could drift
  silently. Added `SchedulerConfig::max_bandwidth_bytes(io_parallelism)` and
  divided that instead.

- The budget was sized from `segments[0]`, which may be one of the sources
  the merge skips, and segments under different base paths carry different
  I/O parallelism. Size it from the sources actually read, taking the widest.

- Documented that the aggregate is `max(one scan, num_sources * floor)`, so
  conservation holds only up to `io_parallelism` sources — 8 on local
  storage, not some exotic count.

- Documented on the trait that `with_io_buffer_size` must build a scheduler
  rather than share one, so it is per-scope not per-request, and that the
  result must not be cached: `is_same_storage_binding` compares storage, not
  budgets.

- Added the missing plumbing test. A recording `IndexStore` asserts each
  read source is rescoped exactly once at its share, and that a skipped
  source is never rescoped. Both halves verified by flipping the code under
  test.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@github-actions github-actions Bot added A-index Vector index, linalg, tokenizer A-encoding Encoding, IO, file reader/writer performance labels Oct 7, 2026

@lance-gatekeeper lance-gatekeeper Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Gate recommendation: approve.

Applying the smaller budgets before stream construction addresses the eager-prefetch multiplication while preserving merge behavior.

Please mark this PR with the breaking-change label.

@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. and removed K-approved Latest Gatekeeper recommendation permits acceptance. labels Oct 7, 2026

@westonpace westonpace left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one minor comment nit. Ping me when it's ready to merge.

Comment thread rust/lance-index-core/src/scalar.rs Outdated
Comment on lines +355 to +356
/// Return a store whose reads are bounded by `bytes` of outstanding data, for callers
/// that read several stores at once and need the total bounded rather than each one.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "why" here is a little misleading / confusing. I think the problem, from the perspective of this trait, is that the store's default budget might be too generous, and a caller will want to restrict it. There might be cases where we want to do that even when we aren't reading from several stores at once. Can we clean the wording up a little.

@lance-gatekeeper lance-gatekeeper Bot removed the K-approved Latest Gatekeeper recommendation permits acceptance. label Oct 13, 2026
@amunra
amunra force-pushed the upstream/merge-segments-io-budget branch from a6d977f to 6828239 Compare October 13, 2026 15:12
The doc on `with_io_buffer_size` gave the several-stores case as the reason to
call it. That is one caller, not the reason: the budget bounds a read's memory,
and a caller narrows it whenever the store's own is more than its scope can
afford, whether it reads one store or several.

Also say the cap is soft, matching `MERGE_MIN_BYTES_PER_SOURCE`: the scheduler
admits a pending read whenever nothing of lower priority is in flight, budget or
no budget, so a caller that needs a hard ceiling does not get one here.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@amunra
amunra force-pushed the upstream/merge-segments-io-budget branch from e4c898e to 5d9320e Compare October 13, 2026 15:20
@westonpace

Copy link
Copy Markdown
Member

Windows failure is flaky test. JSONB failure is a legitimate but unrelated failure and I will fix in a separate issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-encoding Encoding, IO, file reader/writer A-index Vector index, linalg, tokenizer performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants