lance icon indicating copy to clipboard operation
lance copied to clipboard

perf(compaction): skip building row-address maps when index remapping is not needed

Open everySympathy opened this issue 4 months ago • 2 comments

What changed

  • Skip capturing row addresses in compaction workers when the dataset has no remappable data index.
    • This avoids scanning the _rowid column, building an O(number of rewritten rows) RoaringTreemap, and serializing/transferring it to the commit process.
    • Deferred index remapping still captures row addresses because the fragment-reuse index (FRI) consumes them.
  • Make IndexRemapperOptions::create_remapper async and return Result<Option<Box<dyn IndexRemapper>>>.
  • Return None when the dataset has no remappable index metadata, including datasets with only FRI or other system indices.
  • Create the remapper before materializing the potentially large old-to-new row-address HashMap during commit.
  • Reuse the index metadata snapshot loaded during remapper creation, while excluding system indices from actual remapping.
  • Add regression coverage for:
    • datasets with no indices;
    • datasets with only system/FRI metadata;
    • datasets with a normal data index;
    • commit-time skipping when no remapper exists;
    • deferred versus immediate remapping under equivalent indexed conditions.

Why

For datasets without stable row IDs, compaction could previously perform two layers of work before discovering that no index could consume the result:

  1. Workers captured every old row address into a RoaringTreemap and serialized it.
  2. The commit process expanded those addresses into an old-to-new HashMap.

Both structures scale with the number of rewritten rows and are unnecessary when no data index needs remapping. On large distributed compactions, either layer can create avoidable memory pressure or OOM.

This PR skips the work at the earliest safe point and retains the commit-side guard for completed tasks that already contain row addresses.

Compatibility note

IndexRemapperOptions is a public trait. This intentionally changes create_remapper from:

fn create_remapper(&self, dataset: &Dataset) -> Result<Box<dyn IndexRemapper>>;

to:

async fn create_remapper(
    &self,
    dataset: &Dataset,
) -> Result<Option<Box<dyn IndexRemapper>>>;

This is a source-breaking API change for external trait implementors and direct callers: implementations must become async, callers must await the result, and both must handle Option. This trade-off has been discussed with a project collaborator.

Related work and attribution

This incorporates the worker-side optimization from #7773 after coordination with its author. Yue Zhang's contribution is preserved in commit with:

Co-authored-by: Yue Zhang <[email protected]>

Thanks @zhangyue19921010 for the original implementation and collaboration.

Validation

  • cargo fmt --all -- --check
  • cargo test -p lance dataset::index::tests::test_remapper_not_created_without_remappable_indices --lib -- --exact
  • cargo test -p lance dataset::optimize::tests::test_row_addrs_only_used_with_remappable_index --lib
  • cargo test -p lance dataset::optimize::tests::test_defer_index_remap --lib -- --exact

everySympathy avatar Jul 14 '26 09:07 everySympathy

Review Change Stack

[!NOTE]

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • [ ] ▶️ Resume reviews
  • [ ] 🔍 Trigger review
📝 Walkthrough

Walkthrough

Makes index remapper creation asynchronous and optional, caches index metadata, and gates compaction row-address capture and index rewriting on the presence of remappable indices.

Changes

Index remap gating

Layer / File(s) Summary
Remapper contract
rust/lance/src/dataset/optimize/remapping.rs, rust/lance/src/dataset/optimize.rs
create_remapper now asynchronously returns an optional remapper; implementations return either Some or None.
Dataset index metadata loading
rust/lance/src/dataset/index.rs
System indices are excluded from remapping, metadata is loaded asynchronously and cached, and tests cover absent and required remappers.
Compaction remap flow
rust/lance/src/dataset/optimize.rs
Compaction conditionally captures row addresses and rewrites indices based on remapper availability, with coverage for non-remappable datasets.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant commit_compaction
  participant DatasetIndexRemapperOptions
  participant DatasetIndexRemapper
  participant RewriteResult
  commit_compaction->>DatasetIndexRemapperOptions: create_remapper().await
  DatasetIndexRemapperOptions->>DatasetIndexRemapper: preload index metadata
  DatasetIndexRemapper-->>commit_compaction: Some(remapper) or None
  commit_compaction->>DatasetIndexRemapper: remap_indices() when Some
  DatasetIndexRemapper-->>RewriteResult: rewritten indices

Possibly related PRs

Suggested labels: bug, performance

Suggested reviewers: wjones127, jackye1995, westonpace

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: compaction now skips row-address map building when index remapping is unnecessary.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • [ ] Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

coderabbitai[bot] avatar Jul 14 '26 09:07 coderabbitai[bot]

Codecov Report

:x: Patch coverage is 89.70588% with 7 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
rust/lance/src/dataset/optimize.rs 75.00% 3 Missing and 2 partials :warning:
rust/lance/src/dataset/index.rs 97.87% 0 Missing and 1 partial :warning:
rust/lance/src/dataset/optimize/remapping.rs 0.00% 1 Missing :warning:

:loudspeaker: Thoughts on this report? Let us know!

codecov[bot] avatar Jul 15 '26 08:07 codecov[bot]