Skip to content

perf(vector): stop rebuilding the quantizer and copying masks per query - #10829

Merged
wjones127 merged 8 commits into
lance-format:mainfrom
dshepelev15:perf/vector-search-prefilter-allocs
Nov 17, 2026
Merged

wjones127 merged 8 commits into
lance-format:mainfrom
dshepelev15:perf/vector-search-prefilter-allocs

Conversation

@dshepelev15

@dshepelev15 dshepelev15 commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Allocation profiling of an IVF_PQ search with a with_row_id_prefilter mask (a counting global allocator plus perf stat page-fault, dTLB and L1 counters) found work that every query repeated although nothing about it changes between queries:

  • The quantizer was rebuilt from the cached state on every open. open_vector_index hits the IvfIndexState cache and calls reconstruct, which ran Q::from_metadata again: for a PQ codebook that widens the f16 codebook to f32 and builds the 48 prepared L2 targets, about 1.5 MB per query. IvfIndexState now carries the quantizer as a runtime-only Option<Quantizer>, excluded from the cache codec like rq_search_cache. It is set when the state is built (taken from the storage on export, built from the metadata on decode), so the cache charges for it on admission and a reconstruction cannot grow the entry; only storages that retain a quantizer (PQ) carry it. IvfQuantizationStorage::retained_quantizer hands out the storage's copy for the export, and the hidden from_cached_with_remapper constructor takes the cached quantizer.
  • The external mask was deep-copied twice per query. MaskAndLoader::load returned the mask by value, so Arc::unwrap_or_clone copied it while the plan still held the Arc, and DatasetPreFilter::wait_for_ready cloned both the loaded mask and the deletion mask before intersecting. FilterLoader::load now returns Arc<RowAddrMask> (the trait is documented as internal; MaskAndLoader hands out its shared Arc, other loaders wrap a fresh mask). RowAddrMask::intersect_ref (with RowAddrTreeMap::intersection / difference) builds the combined mask from references, RowAddrMask::intersect_with narrows an owned mask by a borrowed one in place, and a lone mask is shared with its cache entry. DatasetPreFilter combines its first two masks by reference and folds the rest into that result; PrefilterUnionExec and the shared multi-match prefilter intersect without cloning either.
  • One definition of the mask set algebra. The in-place (&=, -=) and by-reference (intersection, difference) operations of RowAddrTreeMap go through one per-fragment rule each on RowAddrSelection, and RowAddrMask's & and intersect go through intersect_with. A whole fragment minus an empty partial now stays whole instead of becoming a bitmap of every offset.

Per query on the same 4M-row dataset as #10828 (fsl<f16,768>, IVF_PQ 128 × 48, k=10, nprobes=20, one client, measured with the scratch pool also cached; the quantizer is the larger part):

before after
plain: allocations / bytes 7 849 / 11.7 MB 6 489 / 4.3 MB
plain: p50 5.3 ms 4.4 ms
mask (5% allowlist): allocations / bytes 19 717 / 15.7 MB 17 828 / 9.0 MB
mask: p50 6.4 ms 6.0 ms

The remaining mask-side allocations are the per-run roaring intersections inside selected_indices; walking the bitmap's range instead allocates nothing but measured slower, so they stay.

Tests

  • test_state_entry_carries_quantizer_before_admission: an exported and a decoded PQ state entry report the same deep_size_of after a reconstruction, so the cache's admission charge covers everything the entry retains.
  • test_intersect_ref_matches_intersect: intersect_ref, intersect_with and the consuming intersect agree for every pairing of whole, partial, empty and absent fragments across allow/allow, allow/block and block/block masks, and the allow-list results are also spelled out.
  • Existing MaskAndLoader, DatasetPreFilter and prefilter loader tests cover the shared return.

cargo fmt --all and cargo clippy --all --tests --benches -- -D warnings are clean.

Opening a vector index from its cached IvfIndexState reconstructed the
index object for every query: Q::from_metadata ran again (widening an f16
PQ codebook and preparing its L2 targets, about 1.5 MB per query) and a
fresh QueryScratchPool was created whose scratches then grew on first use.
The state now holds the quantizer and the scratch pool in runtime-only
cells, excluded from the cache codec like rq_search_cache, so later
reconstructions share them.

With an external row-id mask the mask was deep-copied twice per query:
MaskAndLoader::load returned it by value while the plan still held the
Arc, and DatasetPreFilter cloned both the loaded mask and the deletion
mask before intersecting. FilterLoader::load now returns the Arc,
RowAddrMask::intersect_ref combines masks from references, and a lone
mask is shared with its cache entry.

Per query (4M rows, fsl<f16,768>, IVF_PQ 128 x 48, k=10, nprobes=20):
plain 7 849 -> 6 489 allocations and 11.7 -> 4.3 MB, p50 5.3 -> 4.4 ms;
5% allowlist mask 19 717 -> 17 828 allocations and 15.7 -> 9.0 MB,
p50 6.4 -> 6.0 ms.

@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 performance labels Nov 7, 2026
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added the K-changes Latest Gatekeeper recommendation requests changes. label Nov 7, 2026
FilterLoader is public, so its load signature stays; a provided
load_shared hands out the mask behind an Arc and MaskAndLoader overrides
it to share the Arc it already holds. The cached quantizer is only built
for storages that keep one, so an RQ index does not carry its rotation
matrices in the cache for nothing. The intersect_ref test now pins an
empty intersection and a Full/Partial pair.
@lance-gatekeeper lance-gatekeeper Bot removed the K-changes Latest Gatekeeper recommendation requests changes. label Nov 7, 2026
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added the K-changes Latest Gatekeeper recommendation requests changes. label Nov 7, 2026
The quantizer is now built when a state entry is exported or decoded, so
the cache charges for it on admission and a reconstruction can no longer
grow the entry. The scratch pool is no longer kept in the state: its
buffers grow with use after admission, which the cache would not see.
@dshepelev15

Copy link
Copy Markdown
Contributor Author

Addressed the accounting note: the quantizer cell is now filled when the state entry is exported or decoded, before the entry enters the cache, so its size is part of the admission charge, and the scratch pool is no longer kept in the state since its buffers grow after admission. test_state_entry_carries_quantizer_before_admission checks that both an exported and a decoded entry keep the same deep_size_of across a reconstruction. The FilterLoader::load signature is unchanged (load_shared is a provided method), so no breaking-change label should be needed.

@lance-gatekeeper lance-gatekeeper Bot removed the K-changes Latest Gatekeeper recommendation requests changes. label Nov 7, 2026
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Nov 7, 2026

@wjones127 wjones127 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.

Nice profiling-driven PR: caching the PQ quantizer in the index cache entry and intersecting masks by reference are both clear wins, and the cache-accounting issue from the first revision is fixed.

Requesting changes for one thing: the new RowAddrTreeMap::intersection/difference sit on every prefiltered query, and their whole-fragment branches (including the deleted-fragments path) aren't covered by the parity test. The rest is non-blocking: making the quantizer field a plain Option, tightening the state-entry test, reusing the storage's quantizer on export, and collapsing load/load_shared into one method.

Comment thread rust/lance-select/src/mask.rs Resolved
Comment thread rust/lance/src/index/vector/ivf/v2.rs Resolved
Comment thread rust/lance/src/index/vector/ivf/v2.rs Resolved
Comment thread rust/lance-index/src/prefilter.rs Resolved
Comment thread rust/lance-select/src/mask.rs Resolved
Comment thread rust/lance/src/index/prefilter.rs Resolved
- Cover every arm of `RowAddrTreeMap::intersection` and `difference`
  against the consuming `intersect`, and spell out the expected masks.
- `intersection` drops a fragment left without rows whichever side the
  empty set came from, as `BitAndAssign` does.
- `FilterLoader::load` returns `Arc<RowAddrMask>`; `load_shared` is gone.
- `IvfIndexState::quantizer` is a plain `Option<Quantizer>`, taken from the
  storage on export instead of being rebuilt.
- The state test asserts that the quantizer is present.
- `DatasetPreFilter` folds its masks without a counter or `expect`.
@lance-gatekeeper lance-gatekeeper Bot removed the K-approved Latest Gatekeeper recommendation permits acceptance. label Nov 12, 2026
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Nov 12, 2026

@wjones127 wjones127 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.

Thanks for working through the last round so thoroughly. The parity test now reaches every arm of intersection/difference, including the deleted-fragment block list, and spells out the expected masks. The quantizer is a plain field set at construction, export reuses the storage's copy, and load/load_shared are one method. Those were all my earlier points.

Blocking (1): the FilterLoader::load signature change doesn't compile against current main, because PrefilterUnionExec (#10001) landed after this branch was cut. Rebasing and updating that call site should clear the CI failures.

Encouraged: find a way to share the set-algebra logic between the in-place and by-reference mask operations without giving up performance, plus a couple of small simplifications inline.

Re the gatekeeper's breaking-change ask: FilterLoader is documented as internal, so no label is needed.

Comment thread rust/lance-index/src/prefilter.rs Resolved
Comment thread rust/lance-select/src/mask.rs Resolved
Comment thread rust/lance-select/src/mask.rs Resolved
Comment thread rust/lance/src/index/vector/ivf/v2.rs Resolved
Comment thread rust/lance-index/src/vector/storage.rs Resolved
Comment thread rust/lance-select/src/mask.rs Resolved
Comment thread rust/lance/src/index/prefilter.rs Resolved
PrefilterUnionExec now takes the shared mask FilterLoader::load returns.
The in-place and by-reference intersection and difference of
RowAddrTreeMap go through one per-fragment definition. The storage hands
out its retained quantizer for cache export, and from_cached_with_remapper
takes the prebuilt quantizer instead of a third constructor.
@lance-gatekeeper lance-gatekeeper Bot removed the K-approved Latest Gatekeeper recommendation permits acceptance. label Nov 16, 2026
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Nov 16, 2026
RowAddrMask::intersect_with narrows an owned mask by a borrowed one, and
the consuming intersect goes through it, so the allow/block rules of the
owned form exist once.
@lance-gatekeeper lance-gatekeeper Bot removed the K-approved Latest Gatekeeper recommendation permits acceptance. label Nov 16, 2026
@dshepelev15

Copy link
Copy Markdown
Contributor Author

@wjones127 thanks for the second round, everything from it is addressed:

  • Blocking: merged current main and updated PrefilterUnionExec for the shared mask (a141cef). cargo clippy --all --tests --benches -- -D warnings passes on the merged branch. I merged rather than rebased because the rebase conflicted in the same test across several commits; I can rebase if you prefer.
  • Shared set algebra: the in-place and by-reference intersection and difference of RowAddrTreeMap go through one per-fragment definition each (e7ecbf8). At the mask level, the consuming intersect delegates to a new intersect_with(self, &other), which the prefilter fold also uses (609df6f).
  • Inline simplifications: IvfQuantizationStorage::retained_quantizer() for the export, one hidden from_cached_with_remapper constructor taking the quantizer, and the block_rows rename (e7ecbf8).

Details are in the inline threads.

@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.

The author's update is reflected in the current code: later mask-fold steps reuse the owned result while preserving the allow/block semantics. The main integration and cache-admission fixes remain intact. No blocking findings remain.

Please mark this PR with the breaking-change label: lance_index::prefilter::FilterLoader::load returns Result<Arc<RowAddrMask>>, so Rust loader implementations and callers must use the shared return type; lance_index::vector::storage::IvfQuantizationStorage::from_cached_with_remapper takes a new Option<Quantizer> argument, so its Rust callers must supply it.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Nov 16, 2026

@wjones127 wjones127 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.

Thanks for another thorough round. Everything from last time is in: the in-place and by-reference set operations share one per-fragment definition, and export takes the storage's quantizer directly. CI is green. Approving, with a few non-blocking cleanups inline.

suggestion: The description still says IvfQuantizationStorage::from_cached_with_quantizer takes the cached quantizer, but that constructor is gone now that from_cached_with_remapper takes it. Could you update that line, and the FilterLoader bullet, to match the final shape? The description becomes the squash commit message.

Comment thread rust/lance-select/src/mask.rs Resolved
Comment thread rust/lance/src/index/vector/ivf/v2.rs Resolved
Comment thread rust/lance-select/src/mask.rs Resolved
Comment thread rust/lance/src/index/prefilter.rs Resolved
BitAnd for RowAddrMask goes through intersect. PrefilterUnionExec and the
shared multi-match prefilter intersect without cloning a mask. A whole
fragment minus an empty partial stays whole. The decode-side quantizer
helper is build_retained_quantizer, and two no-op async wrappers around
FilterLoader::load are gone.
@dshepelev15

Copy link
Copy Markdown
Contributor Author

Thanks for the approval! The inline cleanups are in c454036, and I updated the description to the final shape: from_cached_with_remapper takes the cached quantizer (with retained_quantizer for the export), and the FilterLoader bullet now covers intersect_with and the shared per-fragment set algebra.

@lance-gatekeeper lance-gatekeeper Bot removed the K-approved Latest Gatekeeper recommendation permits acceptance. label Nov 17, 2026
@wjones127
wjones127 merged commit a254af6 into lance-format:main Nov 17, 2026
42 of 44 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-index Vector index, linalg, tokenizer performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants