Repository navigation
perf(encoding): backport per-page, range-scoped page initialization - #112
Conversation
|
This review was written by Claude Fable 5.2 (an AI assistant) on behalf of Adam, from three independent review passes at Summary: the port is faithful and upstream's tests came over intact, but the backport breaks one vector-index build path in the fork. That one should be fixed before merge. PRs this review refers to:
BlockingBuilding a vector index with exactly one partition now fails once the build spills to disk more than once. Background: an IVF (inverted file) vector index groups vectors into partitions. While building one, Lance writes the rows to a temporary "shuffle" file in batches, and writes a second file of offsets saying where each partition's rows sit inside each batch.
Should fix before merge
Port fidelityThe three adaptations listed in the description are the only production-code deviations from lance-format#7465 and lance-format#9278. Each is forced by #87 and is correct. No upstream hunks or files were dropped. Test-only deviations: TestsUpstream's tests are ported without dropped cases, weakened assertions, Gaps:
Latent, not blocking
Checked and sound
Not run: |
…ance-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`.
…-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.
The page metadata cache is keyed per page since the lance-format#7465 backport.
80987d3 to
f2b743f
Compare
|
Follow-up to the review above, written by Claude Fable 5.2 (an AI assistant) on behalf of Adam. Re-assessed at Summary: nothing blocking remains. Three small items are still open: the commit split, the description, and a missing label. Corrections to the review
Status of each item
New since the review
Checked at
|
This PR was written by Claude Opus 5.5 (an AI assistant), on behalf of @zehiko, to fix a Metastore querier OOM on btree index lookups.
Related
What
Backports range-scoped, per-page page initialization from upstream.
On
release-10.0.0,StructuralPrimitiveFieldScheduler::initializeinitializes every page of a column and caches the result as one per-column entry.A btree index lookup reads one page of the index's
page_datafile but pays for all of them: on 10x, 7,567 pages and ~108 MB per cold initialization of thechunk_partition_idindex, and two ~300 MB entries forchunk_id.The initialization is not single-flight, so concurrent cold lookups each build their own copy.
Entries above the metadata cache's hot target are dropped silently, so the
chunk_idones are rebuilt on every request.After this backport a read initializes only the pages its requested ranges overlap, and caches each page under its own key.
The btree lookup path (
BTreeIndex::lookup_page→read_record_batch→FileReader::read_range→schedule_and_decode→DecodeBatchScheduler::try_new_with_ranges(Some(ranges))) already passes its ranges down, so it gets the lazy path without further changes.Adaptations for fork-only code (the dictionary-page pruning from #87):
BlockDecompressor::decompress(LanceBuffer, ..), which takes the buffer directly rather than anOption.CoreFieldDecoderStrategy::rows_that_may_contain_anykeeps its eager behaviour: it builds every page scheduler (try_new, deprecated upstream,#[allow(deprecated)]here) and initializes withNone, since pruning inspects every page.StructuralPrimitiveFieldScheduler::rows_that_may_contain_anyreads page bounds fromrow_range, which replacednum_rows.80987d397updates the fork'stest_rows_that_may_contain_anyto the per-page cache key.Each backport commit carries
(cherry picked from commit …).Please rebase-merge (not squash), so the backports can be dropped on the next rebase onto upstream.
Not included: single-flight for page initialization (upstream still does
get_with_keytheninsert_with_key; the herd now costs one page per request instead of the whole column), and upstream's dictionary-size cap on write (lance-format#9939).Confirmed on a stack: D1 and D2 on 10x no longer OOM the querier at their spec concurrency (see Testing).
Testing
cargo test -p lance-encoding --lib: 605 passedcargo test -p lance-file --lib: 142 passedcargo test -p lance-index --lib btree: 61 passedcargo clippy -p lance-encoding -p lance-file -p lance-index -p lance --tests -- -D warnings: clean.Load tests on a 1 CU stack (querier 3 CPU / 6 GiB), abc-130k-10x (195M chunk rows, 1.43M segments), reality branch
zeljko/abc130k-10x-cs-limits, before (dfaa7b8) and after this patch (b2b023c); 5-minute runs:For reference, on 1x: D1 2,748 samples/s at 64 workers (querier peak 1.07 GiB), D2 3,654 at 56 workers (1.4 GiB).
Both 10x runs saturate the querier CPU (95%); the remaining gap to 1x is CPU, not memory.
Not fixed by this PR: D1 with sample shuffle still OOM-kills the querier, on 1x as well.
That is a request flood (~950 concurrent
FindChunks), not index initialization.Not measured: a local before/after probe of a single cold btree lookup.
Compatibility
On-disk format unchanged.
API:
StructuralFieldScheduler::initializeandDecodeBatchScheduler::try_newgain arequested_rangesargument (breaking for direct callers oflance-encoding; no callers in reality).FieldDataCacheKeyis replaced by per-pagePageDataCacheKey, so metadata cached by an older build is not reused (in-memory only).🤖 Generated with Claude Code