Skip to content

perf(index): accelerate RQ scale search - #8924

Merged
BubbleCal merged 2 commits into
mainfrom
yang/perf-rq-radix-scale-search
Sep 1, 2026
Merged

BubbleCal merged 2 commits into
mainfrom
yang/perf-rq-radix-scale-search

Conversation

@BubbleCal

@BubbleCal BubbleCal commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

What is the performance issue?

RQ scale search generates several quantization thresholds per vector dimension and sorts them before selecting the best rescale factor. For RQ8 on 1,536-dimensional vectors, comparison-based tuple sorting was the dominant sampled cost in this part of index training.

How does this PR improve performance?

This PR packs each (positive finite f32 threshold, dimension index) pair into a u64 and sorts the threshold bits with four stable byte-wise radix passes. Positive finite IEEE-754 values have the same ordering as their bit patterns, so this removes comparison-heavy tuple sorting for the common unique-key case. Equal thresholds retain the previous quantizer behavior by falling back to the original comparison sort because their event order can affect the incrementally evaluated floating-point objective.

The implementation is isolated from the shuffle reader and scheduling changes in #8894.

Benchmark

The following supporting A/B measurement isolated this implementation on the #8894 benchmark context. Both variants ran on the same AWS m7i.4xlarge VM (16 vCPU, 64 GiB RAM, gp3 storage) against the same S3 dataset: 1,000,000 rows, 1,536 float32 dimensions, 10,000 supplied IVF centroids, and RQ8. Each value is from one fresh process.

Scenario / metric Comparison sort (c7508ad49) Radix sort (174663d0a) Benefit
Shuffle elapsed (lower is better) 70.219 s 51.470 s 1.36x speedup
Full index build elapsed (lower is better) 76.831 s 58.346 s 1.32x speedup
Shuffle CPU time (lower is better) 1,012.95 core-s 718.29 core-s 1.41x less CPU time
S3 read throughput during shuffle (higher is better) 84.16 MB/s 117.61 MB/s 1.40x higher

The two measured commits differed only by the initial RQ radix implementation, but they were on the stacked #8894 context and predate the equal-threshold comparison-sort fallback in this standalone latest-main PR. The standalone head has not been remeasured, so the table is supporting implementation evidence rather than a current-head benchmark claim.

Testing

  • cargo test -p lance-index vector::bq::builder::tests --lib --no-fail-fast (13 passed)
  • cargo fmt --all -- --check
  • cargo clippy --all --tests --benches -- -D warnings

The added regression tests verify that radix sorting matches floating-point threshold ordering and that the selected rescale factor is bit-for-bit identical to the comparison-sort reference for RQ1 through RQ8, including zero, duplicate, NaN, and infinite inputs. A targeted equal-threshold regression also verifies the exact rescale factor from the previous comparison-sort behavior.

@github-actions github-actions Bot added A-index Vector index, linalg, tokenizer performance labels Sep 1, 2026
@BubbleCal
BubbleCal marked this pull request as ready for review September 1, 2026 11:18
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added the K-changes Latest Gatekeeper recommendation requests changes. label Sep 1, 2026
@lance-gatekeeper lance-gatekeeper Bot removed the K-changes Latest Gatekeeper recommendation requests changes. label Sep 1, 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 with a non-blocking risk.

The equal-threshold correctness issue is fixed: duplicate keys now reproduce the prior comparison-sort behavior, while unique keys retain the radix path. The remaining risk is that the benchmark predates this fallback; duplicate-heavy inputs perform both sorting paths and use additional temporary memory, so the target-workload speedup is not yet demonstrated on this head.

@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Sep 1, 2026
@BubbleCal
BubbleCal merged commit 45dc241 into main Sep 1, 2026
38 of 40 checks passed
@BubbleCal
BubbleCal deleted the yang/perf-rq-radix-scale-search branch September 1, 2026 13:04
Xuanwo pushed a commit that referenced this pull request Sep 29, 2026
…PK sidecar (#9595)

## Problem

The memtable primary-key index (`BTreeMemIndex`) is a skiplist
**multiset**: inserting the same `(value, position)` twice stores it
twice. A row *is* indexed twice when a WAL replay (`replay_wal` →
`insert_batches_parallel`) re-indexes buffered batches the WAL flusher
(`flush_from_batch_store`) already indexed.

The flushed generation's PK sidecar (`create_pk_index` /
`write_pk_sidecar`) is trained straight from `pk_training_batches()`, so
the on-disk BTree page inherits the repeated row id. When
`contains_keys` later loads that page, `FlatIndex::try_new` builds a
`RowAddrTreeMap` via `RoaringBitmap::from_sorted_iter`, which requires
strictly increasing input, and every probe of that generation fails
with:

```
RowAddrTreeMap::from_sorted_iter called with non-sorted input
```

Because the block list is built for every LSM scan, one bad generation
makes every read of the table fail. Observed in production (lance
9.0.0): a 2-row generation whose sidecar page was `[(item:a, 0),
(item:a, 0), (item:b, 1)]` broke `list` on a 54k-row table for weeks,
deterministically, for any `limit` large enough to enable the
`PkBlockFilterExec`.

This is the same failure signature as #7230 (base-table BTree merge).
The fixes proposed there were rejected in favour of fixing the training
input; this PR does exactly that for the MemWAL sidecar path.

## Fix

In `BTreeMemIndex::to_training_batches`, skip a position equal to the
previous one within a value group. Positions within a group are already
sorted, so this is a one-comparison dedup at the only place the multiset
is turned into training data.

The write path is left as a multiset on purpose:
`insert_and_check_neighbors` relies on neighbor inspection for the
`pk_has_overrides` signal, and deduping there would change its cost
profile.

## Verification

- `test_btree_index_to_training_batches_dedups_repeated_positions`:
indexes a batch twice into both the fixed-int and bytes backends; the
training batches carry each position once. Fails without the fix.
- `sidecar_from_doubly_indexed_memtable_is_probeable` (block_list):
trains a real on-disk sidecar from a doubly-indexed `IndexStore`, opens
it with `open_pk_index`, and probes it. On the 9.0.0 read path this is
the exact production failure; on `main` the probe path no longer builds
a `RowAddrTreeMap` eagerly (#8924), so the test passes either way there,
but it pins the invariant for the `search`/`into_row_addr_set` paths
that still do.
- `cargo test -p lance --lib mem_wal`: 781 passed. `cargo fmt --check`,
`cargo clippy --lib --tests -D warnings` clean.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Fable 5 <[email protected]>
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 K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants