Skip to content

perf: avoid cloning deletion masks when the prefilter only has deletions - #10403

Merged
Xuanwo merged 3 commits into
lance-format:mainfrom
HeartLinked:perf/deletions-only-prefilter-no-copy
Nov 4, 2026
Merged

Xuanwo merged 3 commits into
lance-format:mainfrom
HeartLinked:perf/deletions-only-prefilter-no-copy

Conversation

@HeartLinked

@HeartLinked HeartLinked commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Part of #10402. (Independent of #10404; can be reviewed and merged in either order.)

Problem

When a vector search has no scalar query filter (filtered_ids), no FTS fragment pruning (deleted_fragments), and no overlay_block, DatasetPreFilter only removes deleted rows. However, the current implementation still clones and intersects RowAddrMask in two places on every query:

  1. DatasetPreFilter::wait_for_ready (rust/lance/src/index/prefilter.rs):
    Intersects deleted_ids with RowAddrMask::default() (an empty block-list that selects every row):

    combined = combined & deleted_ids.get_ready().as_ref().clone();

    When deleted_ids is the only active filter, this clones the entire dataset-wide deletion mask on every query only to produce the deletion mask itself.

  2. SegmentPreFilter::wait_for_ready (rust/lance/src/io/exec/knn.rs):
    Clones both the dataset-wide base mask and the segment's ownership_mask for every index segment to compute their intersection:

    Arc::new(self.base.mask().as_ref().clone() & self.ownership_mask.as_ref().clone())

    self.ownership_mask (built by create_restricted_deletion_mask and cached under SegmentOwnershipMaskKey by dataset version and e_tag) is already an allow-list of the live rows belonging to that segment's fragments at the current dataset version. When self.base only filters deletions, ownership_mask is already a subset of self.base.mask(), so their intersection is identical to ownership_mask itself.

This also affects compacted tables with zero deletion files: when fragment IDs have gaps (needs_restriction == true), create_deletion_mask_impl builds a live-row allow-list across all fragments, which SegmentPreFilter then clones into every segment on each query.

Solution

  1. Add DatasetPreFilter::only_deletions(&self) -> bool, which returns true when filtered_ids, deleted_fragments, and overlay_block are all None. It destructures Self so adding any new filter field to DatasetPreFilter fails to compile until accounted for.
  2. In DatasetPreFilter::wait_for_ready, return deleted_ids.get_ready() directly when self.only_deletions() is true.
  3. In SegmentPreFilter::wait_for_ready, return self.ownership_mask.clone() (Arc clone) directly when self.base.only_deletions() is true.

Benchmark

Steady-state k=10, nprobes=1 latency on a 6.8B-row dataset (99,020 fragments, 153 IVF segments, metadata_cache_size_bytes=48 GiB, index_cache_size_bytes=96 GiB; see #10402 for full details):

Dataset State main This PR
With deletion vectors (10.4% / 790M rows deleted) 66.0 – 66.9 s 34.5 – 35.2 s
Compacted (0 deletions, non-contiguous fragment IDs) 0.70 s 0.17 – 0.18 s
No deletions (baseline) 0.30 s 0.29 – 0.32 s

(Companion PR #10404 caches the address-based deletion mask per dataset version, eliminating the remaining ~35 s reconstruction cost and bringing the 10.4%-deleted case down to 0.37 s when both PRs are applied.)

Tests

  • test_vector_search_excludes_rows_deleted_after_indexing (dataset/tests/dataset_index.rs): end-to-end regression test parameterized over use_stable_row_id in [false, true] × compact in [false, true] (builds 2 IVF segments, deletes rows post-indexing, runs compact_files to retire fragment IDs and deletes again in the compacted cases, and probes all partitions with k above total row count to assert the returned rows equal the exact set of live rows).
  • test_deletions_only_prefilter_shares_mask (index/prefilter.rs): verifies via Arc::ptr_eq that a deletions-only DatasetPreFilter shares the deleted_ids Arc.
  • test_append_only_deltas_keep_empty_prefilter_fast_path (io/exec/knn.rs): verifies via Arc::ptr_eq that SegmentPreFilter reuses ownership_mask directly.

Integration

The benchmark table above was measured on PR head f7427c4e4899 against its original base; it was not rerun. The final integrated head b1bac4f4a041becc2aaf9afdfb00e809c31b7b92 includes main at f56a08d40b5630ce1124535c4c3b1406d5ceedf1; the merge preserves the mask-sharing path, original regression assertions and the 15.0.0-beta.10 release stamp only.

When a vector search has no query filter, no FTS fragment pruning and no
overlay block, the prefilter only removes deleted rows. Two places still
copied a mask in that case:

- `DatasetPreFilter::wait_for_ready` ANDed the deletion mask into an empty
  `RowAddrMask::default()` (selects all rows), cloning the whole
  dataset-wide deletion mask on every query.
- `SegmentPreFilter::wait_for_ready` cloned that dataset-wide mask again for
  every index segment and intersected it with the segment's ownership mask.

The ownership mask is the allow list of the live rows of the segment's
fragments at the dataset's current version, so with a deletions-only base
the intersection is the ownership mask itself. Share the existing masks
instead of copying them.

On a 6.8B-row table with 99,020 fragments, 153 index segments and 10%
rows deleted, a steady-state k=10 search drops from 66 s to 35 s; on the
same table after compaction (no deletion files, retired fragment ids) it
drops from 0.70 s to 0.17 s. Results are unchanged.

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

@HeartLinked

Copy link
Copy Markdown
Contributor Author

@claude review

HeartLinked added a commit to HeartLinked/lance that referenced this pull request Oct 28, 2026
Without stable row ids, every vector search rebuilds the dataset-wide
deletion mask: it walks every fragment with a deletion file, converts each
deletion vector into a bitmap and merges them. Deletion files of a version
are immutable, so the result only depends on the version and the fragment
set. The stable-row-id path already caches its mask under `RowAddrMaskKey`;
the address path did not.

Cache the address mask the same way, in the metadata cache, keyed by
(version, manifest e-tag, fragment-set hash, restricted or not). As on the
row-id path, a dataset without an e-tag is not cached, because a dataset
recreated at the same URI restarts at the same version numbers.

On a 6.8B-row table with 99,020 fragments (all with deletion files), 153
index segments and 10% rows deleted, this eliminates the ~35 s per-query
mask reconstruction overhead; combined with lance-format#10403, steady-state k=10
search drops from 66 s to 0.37 s, versus 0.30 s on the same data files
without deletions. Results are unchanged.
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Oct 28, 2026
@HeartLinked

Copy link
Copy Markdown
Contributor Author

Regarding the lance-gatekeeper note on end-to-end verification across both row-ID modes and compaction: this is covered by test_vector_search_excludes_rows_deleted_after_indexing in rust/lance/src/dataset/tests/dataset_index.rs (added in this PR).

It is parameterized over use_stable_row_id in [false, true] × compact in [false, true]:

  • Builds two IVF index segments (create_index + append + optimize_indices).
  • Deletes rows after indexing; in the compact = true cases, runs compact_files (retiring fragment IDs) followed by another delete batch.
  • Probes all partitions with k above the total row count and asserts that the returned IDs equal the exact set of live rows.

All 4 parameter combinations pass locally (cargo test -p lance --test '*' test_vector_search_excludes_rows_deleted_after_indexing), and top-10 results on the 6.8B-row dataset match main across all three table states.

@lance-gatekeeper lance-gatekeeper Bot removed K-risk Latest Gatekeeper recommendation includes a non-blocking risk. K-approved Latest Gatekeeper recommendation permits acceptance. labels Oct 28, 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 Oct 28, 2026
@lance-gatekeeper lance-gatekeeper Bot removed the K-approved Latest Gatekeeper recommendation permits acceptance. label Nov 4, 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 4, 2026
@lance-gatekeeper lance-gatekeeper Bot removed the K-approved Latest Gatekeeper recommendation permits acceptance. label Nov 4, 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.

The release merge only updates version stamps. The reviewed code and tests are identical to the revision where all 31 focused regressions passed; no further changes are requested.

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

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

Approved for merge. Deletions-only mask sharing matches the accepted semantics on current main; final-head targeted regressions, fmt, full ci-profile Clippy, current-head Gatekeeper, format vote, metadata gates, module-size, typo and license checks pass, with Linux workspace, Python and Java JNI coverage reused for the verified version-only main update.

@Xuanwo
Xuanwo merged commit 9b43805 into lance-format:main Nov 4, 2026
42 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

K-approved Latest Gatekeeper recommendation permits acceptance. performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants