Repository navigation
perf(index): bound merge_segments prefetch to one scan's byte budget - #9761
Merged
westonpace merged 4 commits intoOct 13, 2026
Merged
Conversation
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]>
westonpace
approved these changes
Oct 9, 2026
westonpace
left a comment
Member
There was a problem hiding this comment.
Just one minor comment nit. Ping me when it's ready to merge.
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. |
Member
There was a problem hiding this comment.
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.
amunra
force-pushed
the
upstream/merge-segments-io-budget
branch
from
October 13, 2026 15:12
a6d977f to
6828239
Compare
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
force-pushed
the
upstream/merge-segments-io-budget
branch
from
October 13, 2026 15:20
e4c898e to
5d9320e
Compare
Member
|
Windows failure is flaky test. JSONB failure is a legitimate but unrelated failure and I will fix in a separate issue. |
This was referenced Oct 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
BTreeIndex::merge_segmentsbuilds every source's stream before consuming any of them:data_streamreads a segment's index file throughpage_stream, which callsIndexReader::read_record_batch_stream. Since #9463,read_record_batch_streamreads thewhole 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, eachIndexStoreowns aScanScheduler, and eachScanSchedulercarries a byte budget of its own.
Merging N segments therefore holds N full budgets at once.
SchedulerConfig::max_bandwidthis 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 bybytesof outstanding data.LanceIndexStoreimplements it by building a scheduler withthe smaller budget over the same object store. Unlike
with_io_priority, the schedulercannot be shared: the budget belongs to the scheduler.
SchedulerConfig::max_bandwidth_bytes(io_parallelism)exposes the budgetmax_bandwidthwould hand a store, so the merge divides the real number rather than asecond copy of the formula.
merge_segmentscounts the sources it will actually read (those not skipped byfilter_keeps_nothing), divides one scan's budget between them, and rescopes eachsegment'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 itselfbe skipped, and segments under different base paths carry different parallelism.
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 divisionconserves one scan's budget only while
num_sources <= io_parallelism— 64 sources oncloud 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_sizeis required, not defaulted, so an out-of-treeIndexStorewillnot 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_prioritybeside it is required forthe same reason. A store that prefetches nothing may still return itself — it just has to
say so.
Every other
IndexStorein the tree is a test double over a real store, so each oneforwards 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 --libpasses (2011 / 9 / 323);cargo clippy --all --tests --benches -- -D warningsis clean.A recording
IndexStoreasserts the two read sources are each rescoped exactly once, tohalf 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).
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.0line, 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,
mainarrived at the same place independently — #9463 streams btreepages sequentially through
read_record_batch_stream, #9310 coalesces page loads into oneread_ranges, and #9462 assembles search results once rather than per page. Between themthey 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_segmentsopens one store per source and builds every source's streambefore consuming any, so it now holds one full budget per source — on
maintoday, with norescoping 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.