Skip to content

⚡ Bolt: Optimize KNN classification with select_nth_unstable_by and squared distance#145

Open
teerthsharma wants to merge 1 commit into
masterfrom
jules-11381027091103256586-22ee87b1
Open

⚡ Bolt: Optimize KNN classification with select_nth_unstable_by and squared distance#145
teerthsharma wants to merge 1 commit into
masterfrom
jules-11381027091103256586-22ee87b1

Conversation

@teerthsharma

Copy link
Copy Markdown
Owner

💡 What: Optimized the KNNClassifier::predict method by replacing the manual $O(k \cdot N)$ bubble sort with Rust's select_nth_unstable_by, which operates in $O(N)$ average time to find the $k$ nearest neighbors. Also replaced exact Euclidean distance computations using libm::sqrt with squared distance comparisons to avoid the expensive square root operation in the hot loop.
🎯 Why: In high-performance ML routines, finding the nearest neighbors can become a significant bottleneck, especially for large datasets or high $k$ values. Eliminating sqrt and using $O(N)$ selection greatly reduces computational overhead.
📊 Impact: Reduces prediction time complexity from $O(k \cdot N)$ to $O(N)$ and eliminates the libm::sqrt overhead in the distance calculation loop.
🔬 Measurement: Verify the behavior via cargo test --offline classification inside the crates/aether-core directory to ensure existing perceptron and logistic regression logic works, and ensure select_nth_unstable_by returns correct nearest neighbors.


PR created automatically by Jules for task 11381027091103256586 started by @teerthsharma

…quared distance

Co-authored-by: teerthsharma <78080953+teerthsharma@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

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