lance-format / lance-format/lance

KMeans to_kmeans centroid recomputation is suboptimal — each core redundantly scans all data

Open
#6,369 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-index performance
Dominant language
Rust
Stars
7.1k
Forks
852
Avg merge
3d 18h
Merged PRs (30d)
272

Description

Summary

The current to_kmeans implementation in KMeansAlgoFloat uses a parallel-scan strategy where centroids are split across P cores, and each core scans the entire dataset to accumulate only its assigned centroids. This results in O(N × P) total data reads, which is highly redundant and becomes a bottleneck for large datasets.

Current behavior

In the centroid recomputation step (to_kmeans), the code does:

centroids
    .par_chunks_mut(dim * chunk_size)
    .for_each(|(i, centroids)| {
        // Each thread scans ALL N vectors, but only accumulates
        // vectors belonging to its assigned centroid range [start, end).
        data.chunks(dim).zip(membership.iter()).for_each(|(vector, cid)| {
            if start <= cid && cid < end { ... }
        });
    });

With P cores and N vectors, the total data scanned is N × P — most of which is wasted on the if branch skip.

Expected behavior

Each data point should be read exactly once. A thread-local accumulation pattern (each thread accumulates into its own full centroid buffer, then reduce/merge) would achieve O(N) total reads with only O(k × dim × P) merge overhead.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the KMeansAlgoFloat to_kmeans centroid recomputation entry point and inspect the par_chunks_mut logic described in the issue. Verify that the revised accumulation reads each data point once, preserves centroid results, and limits merging to the stated O(k × dim × P) overhead.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
machine-learning, performance
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.