feat(cosinepair): add lazy constructor that skips init() - #459
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add
CosinePair::lazy(m, top_k)that precomputesrow_normsbut skips theTheta(n²)
init()scan.query_row_top_kandquerywork unchanged becausethey recompute distances on the fly from
samples,row_norms, andparameters.Methods that depend on the precomputed
distances/neighboursfields(
closest_pair,ordered_pairs,query_row,distances_from) will panicon a lazy instance — documented in the method doc comment.
Motivation
arrowspace-rs#141 reports
that
build_laplacian_matrixscales at O(n²·d) becauseCosinePair::with_top_ktriggers
init(), which performs a full symmetric pair scan (n(n-1)/2 dot products).The consumer (
_build_adjacency) only ever callsquery_row_top_k— it never readsself.distancesorself.neighbours. The entireinit()cost is wasted work.Changes
CosinePair::lazy(m, top_k)— same validation androw_normsprecomputation as existing constructors, no
init()call.query_row_top_kresults match the non-lazy pathand
distances/neighboursare empty; one verifying the single-row rejection.Testing
cargo test --all-features— 73 passed, 0 failedcargo clippy --all-features -- -Drust-2018-idioms -Dwarnings— cleancargo fmt --all -- --check— clean