Repository navigation
perf(encoding): build page schedulers only for requested pages - #9278
Conversation
There was a problem hiding this comment.
✅ Gate recommendation: approve.
This completes the range-scoped initialization contract by constructing schedulers only for pages a read touches while preserving the eager None path. The row-boundary mapping, nested range propagation, cache initialization, and uncovered-page error retain existing read semantics without changing the file format.
`StructuralPrimitiveFieldScheduler::try_new` built a boxed page scheduler for every page of a column on every read, cloning buffer tables and instantiating decompressors, even after #7465 made initialization range-scoped. A column with hundreds of thousands of pages paid this fixed cost for each small read, which is exactly the pattern IVF prewarm follows (one `read_stream` per partition window) and nprobes=1 queries follow. Add `try_new_lazy`, which stores only the per-page row boundaries at construction and builds page schedulers in `initialize` for the pages the requested ranges touch (or all pages when no ranges are given). The decoder now uses it. The existing `try_new` keeps its signature and eager behaviour and is deprecated. Scheduling a page that was not covered by the initialized ranges returns `Error::Internal` instead of panicking on an uninitialized page. Linear: OSS-2276 Co-Authored-By: Claude Fable 5.1 <[email protected]>
5a23e36 to
dd59c2a
Compare
There was a problem hiding this comment.
✅ Gate recommendation: approve.
This revision preserves the existing eager constructor as a deprecated compatibility path while production decoding uses lazy construction. The page-boundary mapping and ordered insertion keep sparse and repeated initialization consistent with existing scheduling and cache semantics, so the performance fix remains worth accepting.
…-format#9278) Every `read_stream`/`read_range` on a structural (2.1+) file rebuilt a boxed `StructuralPageScheduler` for **every page of every projected column** in `StructuralPrimitiveFieldScheduler::try_new`, cloning buffer tables and instantiating decompressors. lance-format#7465 made page *initialization* range-scoped, but construction stayed O(pages). For a column with hundreds of thousands of pages this is a large fixed cost per read, which is exactly the pattern IVF prewarm follows (one read per partition window; 4,069+ windows over a 620,881-page `auxiliary.idx` in the September 10B RQ benchmark) and that nprobes=1 queries pay per query. The captured CPU profile was dominated by malloc/free of `PageInfoAndScheduler`/`MiniBlockScheduler`. Linear: [OSS-2276](https://linear.app/lancedb/issue/OSS-2276) - `try_new` now stores only the per-page row boundaries (`page_row_ends`, one `u64` per page) plus a `PageSchedulerSource` (column info, decompression strategy, target field). - `initialize(Some(ranges))` builds schedulers only for the pages those ranges touch, merged in page order; `initialize(None)` keeps the eager behaviour and builds every page. - Range-to-page mapping works from `page_row_ends`, so it no longer needs built schedulers. - Scheduling a page that was not covered by the initialized ranges returns `Error::Internal` with a descriptive message instead of panicking on an uninitialized page. - The lazy scheduler must own its column info and decompression strategy, so the new constructor is `try_new_lazy(Arc<ColumnInfo>, Arc<dyn DecompressionStrategy>, ..)`. The existing `try_new(&ColumnInfo, &dyn DecompressionStrategy, ..)` keeps its signature and eager behaviour and is marked `#[deprecated]`. Matched local micro-benchmark against `main` (3a12096): a v2.1 file with 5 `UInt8` columns, 131,072 pages per column (655,360 pages total), read from local disk, reading successive disjoint row windows with `read_stream`, 200 reads per pass, two repetitions per binary. Both binaries built from the same harness with `cargo build --release`; numbers below are the second repetition's warm pass (page metadata cached), lower is better. Machine: Apple Silicon Mac, single process. | Scenario / metric | Baseline | This PR | Benefit | | --- | ---: | ---: | ---: | | 64K-row read (256 pages/column touched), mean latency | 83.0 ms | 19.5 ms | 4.3x speedup | | 64K-row read, p99 latency | 106.5 ms | 22.5 ms | 4.7x speedup | | 4K-row read (16 pages/column touched), mean latency | 68.0 ms | 7.8 ms | 8.7x speedup | | 4K-row read, p99 latency | 79.2 ms | 11.2 ms | 7.1x speedup | Cold-pass numbers (first touch of each window's page metadata) show the same ratios. The baseline's ~68 ms floor is the whole-file scheduler construction; this PR removes it. Same index as the Linear issue (RQ1 1-bit, 244,140 partitions, 1B rows, `auxiliary.idx` 113.8 GB), copied to local EBS on an r8i.8xlarge (32 vCPU, 247 GiB RAM), `LANCE_CPU_THREADS=30`, 192 GiB index cache, both wheels built from source with `--profile release`. Cold = page cache dropped before the run. Lower is better for time, higher for fill rate. | Scenario / metric | Baseline (`main` 3a12096) | This PR | Benefit | | --- | ---: | ---: | ---: | | Cold prewarm of 118.0 GB, 64 MiB windows, wall time | > 3,600 s (timed out at 104.4 GB, 88%) | 153.8 s | > 23x speedup | | Cold prewarm, average cache fill rate | 29 MB/s (decaying from 77 to 15 MB/s) | 767 MB/s | 26x | | Cold prewarm, process CPU | 1 core at 100% for the whole hour | 285 CPU-s over 154 s | n/a | | Warm page cache prewarm, 64 MiB windows | not run (baseline too slow) | 61.1 s (1,933 MB/s, 0 physical reads) | n/a | | Warm page cache prewarm, 1 GiB windows (issue config) | not run | 60.9 s | n/a | | 64 nprobes=1 queries without prewarm (partition loads on demand) | 19.0 s | 6.9 s | 2.8x speedup | Peak RSS after prewarm was 121 GB on this PR (cache 118.0 GB). The 64 nprobes=1, k=10 query results (row ids and float32 distances) hash identically on `main` and this PR, both with and without prewarm. Raw summaries and 5-second progress logs: `s3://mmlb-us-east-1/rq-benchmark/results/20260916-oss-2276-prewarm-ab/`. **Not yet verified:** QPS under load on the 10B index. The remaining ~7.7 ms fixed cost per read at this page count is two O(pages) walks over immutable `ColumnInfo` (the `page_row_ends` prefix sum here and `normalized_column_num_rows` in lance-file's read-length validation); caching those per file is a follow-up. - New test `initialize_builds_only_requested_page_schedulers`: construction builds nothing; range-scoped initialize builds exactly the touched pages in order; scheduling an uncovered page errors instead of panicking; widening the request adds only new pages; `None` builds all and drops the source. - Existing lazy-initialization tests adapted to the new constructor. - `cargo test -p lance-encoding` (1826 passed), `cargo test -p lance-file` (182 passed), `cargo test -p lance --lib ivf` (277 passed), `cargo check --workspace --tests --benches`, `cargo fmt --all -- --check`, `cargo clippy --all --tests --benches -- -D warnings`. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Fable 5.1 <[email protected]> (cherry picked from commit 525136d) Backport conflict resolved for fork-only code: the dictionary-page prune helpers (#87) are kept next to the new lazy constructors. The prune check still builds every page with the eager `try_new`, since it inspects every page.
…112) * perf(encoding)!: initialize only the page metadata a read will touch (lance-format#7465) The structural decoder's `StructuralFieldScheduler::initialize` eagerly initializes **every** page in a column before scheduling, even when the read only touches one page. For a column with thousands of large, non-coalescing pages (e.g. a BTree leaf scan that reads one leaf), this means paying metadata IO proportional to the page count regardless of how few rows are actually requested. This PR makes page-metadata initialization **range-scoped and per-page**: - `DecodeBatchScheduler::try_new` and `StructuralFieldScheduler::initialize` now take an optional `requested_ranges: Option<Arc<[Range<u64>]>>` — the top-level row ranges that will later be scheduled. The structural path initializes only the pages those ranges overlap (`pages_overlapping_ranges`). `None` preserves the prior eager behavior. - The page-scheduler `initialize` is split into `init_ranges()` (declare the byte ranges the page needs) and `init_from_buffers(buffers, io)` (finish initialization from those bytes). The field scheduler concatenates the misses' ranges into a **single** `submit_request` so adjacent ranges coalesce into shared GETs instead of one request per page. - Page metadata is cached per page via `PageDataCacheKey { column_index, page_index, view_tag }` (replacing the per-column `FieldDataCacheKey`). `view_tag` is retained so a column decoded under two shapes (blob descriptor `Struct<pos,size>` vs raw `LargeBinary`) can't collide on cached state. - Nested schedulers forward ranges consistently with their existing `schedule_ranges`: lists/maps/structs pass the top-level ranges through unchanged; fixed-size-list scales by `dimension`. The requested ranges are always a **superset** of what `schedule_ranges`/`schedule_take` later touch, so any page a read schedules was initialized here. Net effect: a cold point/range read's metadata IO is invariant to the column's total page count. - [x] `cargo test -p lance-encoding --lib` (393 passed) — the round-trip encoding harness now exercises **both** the eager path (full scan, `None`) and the lazy path (range reads and scattered takes pass their `requested_ranges`). - [x] New unit test `test_initialize_coalesces_missed_page_metadata`: an N-page cache miss issues exactly **one** `submit_request` carrying every page's metadata range (not one request per page). - [x] Updated FullZip page-scheduler tests to the `init_ranges` / `init_from_buffers` split. - [x] `cargo test -p lance-file --lib` (76 passed). - [x] `cargo test -p lance --lib dataset::blob::tests::test_blob_cache_key_distinguishes_views` — confirms the per-page key still distinguishes blob decoder views. - [x] `cargo fmt --all` and `cargo clippy -p lance-encoding -p lance-file --tests --benches -- -D warnings` clean. Made with [Cursor](https://cursor.com) --------- Co-authored-by: Cursor <[email protected]> (cherry picked from commit 156d7e7) Backport conflicts resolved for fork-only code: - MiniBlock dictionary init calls the fork's `BlockDecompressor::decompress(LanceBuffer, ..)`. - The dictionary-page prune check (#87) initializes with `None` (all pages), as before. - `rows_that_may_contain_any` reads page bounds from `row_range`. * perf(encoding): build page schedulers only for requested pages (lance-format#9278) Every `read_stream`/`read_range` on a structural (2.1+) file rebuilt a boxed `StructuralPageScheduler` for **every page of every projected column** in `StructuralPrimitiveFieldScheduler::try_new`, cloning buffer tables and instantiating decompressors. lance-format#7465 made page *initialization* range-scoped, but construction stayed O(pages). For a column with hundreds of thousands of pages this is a large fixed cost per read, which is exactly the pattern IVF prewarm follows (one read per partition window; 4,069+ windows over a 620,881-page `auxiliary.idx` in the September 10B RQ benchmark) and that nprobes=1 queries pay per query. The captured CPU profile was dominated by malloc/free of `PageInfoAndScheduler`/`MiniBlockScheduler`. Linear: [OSS-2276](https://linear.app/lancedb/issue/OSS-2276) - `try_new` now stores only the per-page row boundaries (`page_row_ends`, one `u64` per page) plus a `PageSchedulerSource` (column info, decompression strategy, target field). - `initialize(Some(ranges))` builds schedulers only for the pages those ranges touch, merged in page order; `initialize(None)` keeps the eager behaviour and builds every page. - Range-to-page mapping works from `page_row_ends`, so it no longer needs built schedulers. - Scheduling a page that was not covered by the initialized ranges returns `Error::Internal` with a descriptive message instead of panicking on an uninitialized page. - The lazy scheduler must own its column info and decompression strategy, so the new constructor is `try_new_lazy(Arc<ColumnInfo>, Arc<dyn DecompressionStrategy>, ..)`. The existing `try_new(&ColumnInfo, &dyn DecompressionStrategy, ..)` keeps its signature and eager behaviour and is marked `#[deprecated]`. Matched local micro-benchmark against `main` (3a12096): a v2.1 file with 5 `UInt8` columns, 131,072 pages per column (655,360 pages total), read from local disk, reading successive disjoint row windows with `read_stream`, 200 reads per pass, two repetitions per binary. Both binaries built from the same harness with `cargo build --release`; numbers below are the second repetition's warm pass (page metadata cached), lower is better. Machine: Apple Silicon Mac, single process. | Scenario / metric | Baseline | This PR | Benefit | | --- | ---: | ---: | ---: | | 64K-row read (256 pages/column touched), mean latency | 83.0 ms | 19.5 ms | 4.3x speedup | | 64K-row read, p99 latency | 106.5 ms | 22.5 ms | 4.7x speedup | | 4K-row read (16 pages/column touched), mean latency | 68.0 ms | 7.8 ms | 8.7x speedup | | 4K-row read, p99 latency | 79.2 ms | 11.2 ms | 7.1x speedup | Cold-pass numbers (first touch of each window's page metadata) show the same ratios. The baseline's ~68 ms floor is the whole-file scheduler construction; this PR removes it. Same index as the Linear issue (RQ1 1-bit, 244,140 partitions, 1B rows, `auxiliary.idx` 113.8 GB), copied to local EBS on an r8i.8xlarge (32 vCPU, 247 GiB RAM), `LANCE_CPU_THREADS=30`, 192 GiB index cache, both wheels built from source with `--profile release`. Cold = page cache dropped before the run. Lower is better for time, higher for fill rate. | Scenario / metric | Baseline (`main` 3a12096) | This PR | Benefit | | --- | ---: | ---: | ---: | | Cold prewarm of 118.0 GB, 64 MiB windows, wall time | > 3,600 s (timed out at 104.4 GB, 88%) | 153.8 s | > 23x speedup | | Cold prewarm, average cache fill rate | 29 MB/s (decaying from 77 to 15 MB/s) | 767 MB/s | 26x | | Cold prewarm, process CPU | 1 core at 100% for the whole hour | 285 CPU-s over 154 s | n/a | | Warm page cache prewarm, 64 MiB windows | not run (baseline too slow) | 61.1 s (1,933 MB/s, 0 physical reads) | n/a | | Warm page cache prewarm, 1 GiB windows (issue config) | not run | 60.9 s | n/a | | 64 nprobes=1 queries without prewarm (partition loads on demand) | 19.0 s | 6.9 s | 2.8x speedup | Peak RSS after prewarm was 121 GB on this PR (cache 118.0 GB). The 64 nprobes=1, k=10 query results (row ids and float32 distances) hash identically on `main` and this PR, both with and without prewarm. Raw summaries and 5-second progress logs: `s3://mmlb-us-east-1/rq-benchmark/results/20260916-oss-2276-prewarm-ab/`. **Not yet verified:** QPS under load on the 10B index. The remaining ~7.7 ms fixed cost per read at this page count is two O(pages) walks over immutable `ColumnInfo` (the `page_row_ends` prefix sum here and `normalized_column_num_rows` in lance-file's read-length validation); caching those per file is a follow-up. - New test `initialize_builds_only_requested_page_schedulers`: construction builds nothing; range-scoped initialize builds exactly the touched pages in order; scheduling an uncovered page errors instead of panicking; widening the request adds only new pages; `None` builds all and drops the source. - Existing lazy-initialization tests adapted to the new constructor. - `cargo test -p lance-encoding` (1826 passed), `cargo test -p lance-file` (182 passed), `cargo test -p lance --lib ivf` (277 passed), `cargo check --workspace --tests --benches`, `cargo fmt --all -- --check`, `cargo clippy --all --tests --benches -- -D warnings`. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Fable 5.1 <[email protected]> (cherry picked from commit 525136d) Backport conflict resolved for fork-only code: the dictionary-page prune helpers (#87) are kept next to the new lazy constructors. The prune check still builds every page with the eager `try_new`, since it inspects every page. * test(encoding): check per-page cache keys after a prune check The page metadata cache is keyed per page since the lance-format#7465 backport. --------- Co-authored-by: Ali Arslan <[email protected]> Co-authored-by: Yang Cen <[email protected]>
Problem
Every
read_stream/read_rangeon a structural (2.1+) file rebuilt a boxedStructuralPageSchedulerfor every page of every projected column inStructuralPrimitiveFieldScheduler::try_new, cloning buffer tables and instantiating decompressors. #7465 made page initialization range-scoped, but construction stayed O(pages). For a column with hundreds of thousands of pages this is a large fixed cost per read, which is exactly the pattern IVF prewarm follows (one read per partition window; 4,069+ windows over a 620,881-pageauxiliary.idxin the September 10B RQ benchmark) and that nprobes=1 queries pay per query. The captured CPU profile was dominated by malloc/free ofPageInfoAndScheduler/MiniBlockScheduler.Linear: OSS-2276
Change
try_newnow stores only the per-page row boundaries (page_row_ends, oneu64per page) plus aPageSchedulerSource(column info, decompression strategy, target field).initialize(Some(ranges))builds schedulers only for the pages those ranges touch, merged in page order;initialize(None)keeps the eager behaviour and builds every page.Range-to-page mapping works from
page_row_ends, so it no longer needs built schedulers.Scheduling a page that was not covered by the initialized ranges returns
Error::Internalwith a descriptive message instead of panicking on an uninitialized page.The lazy scheduler must own its column info and decompression strategy, so the new constructor is
try_new_lazy(Arc<ColumnInfo>, Arc<dyn DecompressionStrategy>, ..). The existingtry_new(&ColumnInfo, &dyn DecompressionStrategy, ..)keeps its signature and eager behaviour and is marked#[deprecated].Performance
Matched local micro-benchmark against
main(3a12096): a v2.1 file with 5UInt8columns, 131,072 pages per column (655,360 pages total), read from local disk, reading successive disjoint row windows withread_stream, 200 reads per pass, two repetitions per binary. Both binaries built from the same harness withcargo build --release; numbers below are the second repetition's warm pass (page metadata cached), lower is better. Machine: Apple Silicon Mac, single process.Cold-pass numbers (first touch of each window's page metadata) show the same ratios. The baseline's ~68 ms floor is the whole-file scheduler construction; this PR removes it.
End-to-end: prewarming one 1B IVF_RQ segment
Same index as the Linear issue (RQ1 1-bit, 244,140 partitions, 1B rows,
auxiliary.idx113.8 GB), copied to local EBS on an r8i.8xlarge (32 vCPU, 247 GiB RAM),LANCE_CPU_THREADS=30, 192 GiB index cache, both wheels built from source with--profile release. Cold = page cache dropped before the run. Lower is better for time, higher for fill rate.main3a12096)Peak RSS after prewarm was 121 GB on this PR (cache 118.0 GB). The 64 nprobes=1, k=10 query results (row ids and float32 distances) hash identically on
mainand this PR, both with and without prewarm. Raw summaries and 5-second progress logs:s3://mmlb-us-east-1/rq-benchmark/results/20260916-oss-2276-prewarm-ab/.Not yet verified: QPS under load on the 10B index. The remaining ~7.7 ms fixed cost per read at this page count is two O(pages) walks over immutable
ColumnInfo(thepage_row_endsprefix sum here andnormalized_column_num_rowsin lance-file's read-length validation); caching those per file is a follow-up.Validation
initialize_builds_only_requested_page_schedulers: construction builds nothing; range-scoped initialize builds exactly the touched pages in order; scheduling an uncovered page errors instead of panicking; widening the request adds only new pages;Nonebuilds all and drops the source.cargo test -p lance-encoding(1826 passed),cargo test -p lance-file(182 passed),cargo test -p lance --lib ivf(277 passed),cargo check --workspace --tests --benches,cargo fmt --all -- --check,cargo clippy --all --tests --benches -- -D warnings.🤖 Generated with Claude Code