Repository navigation
PERF Thread-safe binary tree (BallTree/KDTree), makes KNeighborsClassifier as much as 2-3× faster - #34187
Merged
thomasjpfan merged 8 commits intoJun 9, 2026
Conversation
itamarst
marked this pull request as ready for review
June 2, 2026 18:18
itamarst
marked this pull request as draft
June 2, 2026 20:00
Contributor
Author
|
UPDATE: Added a test. It fails on |
itamarst
marked this pull request as ready for review
June 2, 2026 21:20
ogrisel
approved these changes
Jun 4, 2026
ogrisel
left a comment
Member
There was a problem hiding this comment.
Thanks for the fix/efficiency improvement.
It's a bit slower on a single thread but I think it's ok.
Contributor
Author
|
TIL about verbatim C includes (https://cython.readthedocs.io/en/latest/src/userguide/external_C_code.html#including-verbatim-c-code) will update this PR to do that just so it's more succinct. |
thomasjpfan
reviewed
Jun 7, 2026
Member
|
The thread-safety fix and performance improvement are useful but tbh I'd just deprecate these stats altogether:
|
Contributor
Author
|
@jeremiedbb I opened a PR for that, #34242 |
prady0t
pushed a commit
to prady0t/scikit-learn
that referenced
this pull request
Sep 2, 2026
…ifier as much as 2-3× faster (scikit-learn#34187)
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.
Reference Issues/PRs
Fixes #34175
What does this implement/fix? Explain your changes.
Fixes the lack of thread-safety in stats and calls calculation; unclear if anyone actually uses these, but it is part of the public API so can't just be ripped out. The thread-safety isn't actually a big deal for correctness, given it's just stats.
I fixed it by using thread locals.
The main benefit is that it's mostly much faster, since it's not trying to access the same memory address from multiple cores a huge number of times:
Gave:
AI usage disclosure
No "AI".
Any other comments?
A previous failed attempt was #19884 - rejected because it was not backwards compatible.
If these APIs are not used, would also be worth deprecating them at the same time, so the code can just be ripped out eventually. But the current change is still worth it, I think, because it will provide the speedup in the interim.