Skip to content

perf(index): skip the write-only distance column when loading HNSW partitions - #8136

Merged
Xuanwo merged 1 commit into
lance-format:mainfrom
wombatu-kun:perf/hnsw-skip-distance-column-on-load
Aug 1, 2026
Merged

Xuanwo merged 1 commit into
lance-format:mainfrom
wombatu-kun:perf/hnsw-skip-distance-column-on-load

Conversation

@wombatu-kun

Copy link
Copy Markdown
Contributor

Closes #8135

Summary

HNSW index partitions no longer fetch the _distance column from storage. IvfSubIndex gains an optional read_columns() projection naming the columns load actually consumes; HNSW declares __vector_id and __neighbors, and load_partition_entry reads through FileReader::read_stream_projected when a sub-index declares one.

This is the second half of #6798. That PR stopped materializing _distance in memory but left it being fetched from storage, because changing the on-disk schema was out of scope for a load-path performance fix.

Why a read projection and not a schema change

Dropping the column from to_batch() would break forward compatibility: readers older than v8.0.0 access it as batch[DIST_COL], a panicking indexer rather than an Option. Gating that behind an index version bump would silently disable the index for every older reader and send those queries to a full scan. A read projection has no such cost: the file still carries the column, readers of every version keep working, and no index version is bumped. Removing it from the format stays available as a separate, versioned change later.

Results

All figures are bytes fetched from storage, measured with ObjectStore::io_stats_incremental and summed over every partition. Both projections are exercised against the same row ranges in one binary, so nothing is confounded by build layout, and the projected read is taken first so it pays any first-touch cost rather than benefiting from it.

End-to-end, one cold partition load. IVF_HNSW_SQ, 100,000 rows at dim 768, nlist = 2, m = 20. This is the number that matters in practice: it includes the auxiliary quantized storage the load also has to fetch.

Cold partition load Bytes
Before 93,932,846
After 83,379,448
Saved 10,553,398 (11.2%)

The graph read itself drops 16,882,542 -> 6,329,144, a 62.5% cut. It is 18.0% of the partition at this dimension, which is where the 11.2% comes from.

That 62.5% is stable across configurations: an index over 500,000 rows at dim 32 with nlist = 4 (525,902 graph rows, ~131k nodes per partition) gives 102,548,810 -> 38,706,238, or 62.3%, and 50,000 rows gives 66.7%. The saving is a property of the graph encoding, not of one benchmark: neighbor ids bitpack against the partition's node count while f32 distances do not compress, so the column nothing reads is also the one that stays wide.

What does vary is how much of a partition the graph accounts for, because graph cost per vector depends on m and the id bit-width while quantized storage scales with dimension. At dim 768 storage is 770 B/vector against 169 B/vector of graph, giving the 18.0% share above; at low dimensions the graph dominates and the end-to-end saving approaches the 62% figure. So 11.2% is the conservative end of the range, not the typical one. The same bytes also stop occupying LanceCache.

Tests

All in rust/lance/src/index/vector/ivf/v2.rs. The first three fail without the change.

  • test_hnsw_partition_load_reads_only_graph_columns - the written index file still declares all three columns while every loaded partition carries only two, re-checked after an append plus a delta merge.
  • test_hnsw_projected_read_matches_full_read - the two surviving columns are identical to the same columns of a full-schema read, per partition. __neighbors.item and _distance.item are both 4-byte List children named item, so a mis-mapped column index would reinterpret distances as neighbor ids with no type error to catch it.
  • test_hnsw_read_projection_moves_fewer_bytes - the declared projection moves strictly fewer bytes than the full read over a real partition range, measured with io_stats_incremental. The projected read is taken first so it pays any first-touch cost.
  • test_prewarm_and_query_with_serializing_backend - extended with an IVF_HNSW_SQ case alongside the existing IVF_PQ one, so the narrowed batch round-trips through cache serialization, reloads, and still meets the recall bar with no extra I/O.

@github-actions github-actions Bot added A-index Vector index, linalg, tokenizer performance labels Aug 1, 2026
@wombatu-kun
wombatu-kun force-pushed the perf/hnsw-skip-distance-column-on-load branch from ba7db9d to 2d73147 Compare August 1, 2026 13:55

@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 read projection removes unused HNSW graph I/O at the right boundary while preserving the three-column persistent format for older readers. It resolves names against the actual file schema and version, leaves other sub-indexes on full reads, and covers projected/full equality, reduced bytes, delta-merge writes, and serialized-cache restart/query behavior.

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

Thank you!

@Xuanwo
Xuanwo merged commit 4a54e5d into lance-format:main Aug 1, 2026
39 of 40 checks passed
Xuanwo pushed a commit that referenced this pull request Nov 30, 2026
…m test (#11629)

Closes #6745.

## Summary

Most of #6745 had already been done by earlier PRs. This PR adds no new
codec or serialization logic. It only extends test coverage to the two
IVF+HNSW variants that weren't tested yet.

The issue asked for two things. Both were already on `main`:

1. **Zero-copy Arrow IPC serde for HNSW cache entries.** HNSW partitions
go through the generic IVF `PartitionEntry<S, Q>` codecs in
`rust/lance/src/index/vector/ivf/partition_serde.rs`. Those write the
sub-index with `write_ipc(index.to_batch())` and read it back through
`HNSW::load`. Since #6798, `HNSW::load` keeps the Arrow batch and serves
neighbor lists zero-copy (`LoadedHnswGraph`). `to_batch()` hands that
batch back unchanged, so a loaded graph round-trips through the cache
without being rebuilt.
2. **A test showing that, after prewarm, a serializing backend serves
HNSW queries with no IO.**
`test_prewarm_and_query_with_serializing_backend` prewarms through
`SerializingCacheBackend`, restarts the backend so only serialized
entries survive, queries, checks recall, and asserts `read_iops == 0`.
It already covered IVF_HNSW_SQ and IVF_HNSW_RQ.

Earlier PRs that did the work:

- #6090: prewarm for IVF indices and the no-IO assertion
- #6223: `CacheCodec` and the IVF partition codecs, which are generic
over the sub-index
- #6798: zero-copy HNSW loading
- #8136: the IVF_HNSW_SQ case
- #10522: the IVF_HNSW_RQ case

## Changes

Adds `ivf_hnsw_pq` (`PartitionEntry<HNSW, ProductQuantizer>`) and
`ivf_hnsw_flat` (`PartitionEntry<HNSW, FlatQuantizer>`) cases to
`test_prewarm_and_query_with_serializing_backend`. Every IVF+HNSW
variant is now covered by the prewarm, restart and no-IO check.

## Testing

- `cargo test --profile ci -p lance --lib
test_prewarm_and_query_with_serializing_backend`: all 5 cases pass
(`ivf_pq`, `ivf_hnsw_sq`, `ivf_hnsw_rq`, and the new `ivf_hnsw_pq` and
`ivf_hnsw_flat`) in 1.55s.
- `cargo fmt --all -- --check`: clean.
- `cargo clippy --profile ci -p lance --tests --benches -- -D warnings`:
clean. Clippy was run on the touched crate only, not with `--all`.

---
_Generated by [Claude Code](https://claude.ai/code)_

Co-authored-by: Claude <[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 performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf: HNSW index partitions read a write-only column on every cold load

2 participants