Skip to content

feat(cosinepair): add lazy constructor that skips init() - #459

Merged
Mec-iS merged 1 commit into
mainfrom
feat/cosinepair-lazy-constructor
Aug 26, 2026
Merged

feat(cosinepair): add lazy constructor that skips init()#459
Mec-iS merged 1 commit into
mainfrom
feat/cosinepair-lazy-constructor

Conversation

@Mec-iS

@Mec-iS Mec-iS commented Aug 26, 2026

Copy link
Copy Markdown
Collaborator

Summary

Add CosinePair::lazy(m, top_k) that precomputes row_norms but skips the
Theta(n²) init() scan. query_row_top_k and query work unchanged because
they recompute distances on the fly from samples, row_norms, and parameters.

Methods that depend on the precomputed distances / neighbours fields
(closest_pair, ordered_pairs, query_row, distances_from) will panic
on a lazy instance — documented in the method doc comment.

Motivation

arrowspace-rs#141 reports
that build_laplacian_matrix scales at O(n²·d) because CosinePair::with_top_k
triggers init(), which performs a full symmetric pair scan (n(n-1)/2 dot products).
The consumer (_build_adjacency) only ever calls query_row_top_k — it never reads
self.distances or self.neighbours. The entire init() cost is wasted work.

Changes

  • New method: CosinePair::lazy(m, top_k) — same validation and row_norms
    precomputation as existing constructors, no init() call.
  • Two new tests: one verifying query_row_top_k results match the non-lazy path
    and distances/neighbours are empty; one verifying the single-row rejection.

Testing

  • cargo test --all-features — 73 passed, 0 failed
  • cargo clippy --all-features -- -Drust-2018-idioms -Dwarnings — clean
  • cargo fmt --all -- --check — clean

Add CosinePair::lazy(m, top_k) that precomputes row_norms but skips
the Theta(n^2) init() scan. query_row_top_k and query work unchanged
since they recompute distances on the fly. Methods depending on
distances/neighbours (closest_pair, ordered_pairs, query_row,
distances_from) will panic on a lazy instance.

Closes #458
@Mec-iS
Mec-iS merged commit 7073d6e into main Aug 26, 2026
13 checks passed
@Mec-iS
Mec-iS deleted the feat/cosinepair-lazy-constructor branch August 26, 2026 09:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant