Skip to content

PERF Thread-safe binary tree (BallTree/KDTree), makes KNeighborsClassifier as much as 2-3× faster - #34187

Merged
thomasjpfan merged 8 commits into
scikit-learn:mainfrom
itamarst:34175-thread-safe-binary-tree
Jun 9, 2026
Merged

thomasjpfan merged 8 commits into
scikit-learn:mainfrom
itamarst:34175-thread-safe-binary-tree

Conversation

@itamarst

@itamarst itamarst commented Jun 2, 2026 •

Copy link
Copy Markdown
Contributor

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:

$ SKLBENCH_NJOBS="[1, 4, 11]" asv continuous --environment=conda:3.11 -b KNeighborsClassifierBenchmark.time_predict origin/main 34175-thread-safe-binary-tree

Gave:

Change Before [a359096] After [d11a6e2] Ratio Benchmark (Parameter)
+ 749±1ms 917±1ms 1.22 time_predict('ball_tree', 'low', 1)
- 424±20ms 283±3ms 0.67 time_predict('kd_tree', 'low', 4)
- 2.12±0.07s 1.23±0.02s 0.58 time_predict('kd_tree', 'high', 4)
- 443±30ms 224±3ms 0.5 time_predict('kd_tree', 'low', 11)
- 887±50ms 378±20ms 0.43 time_predict('ball_tree', 'low', 4)
- 2.47±0.06s 1.03±0.02s 0.42 time_predict('ball_tree', 'high', 4)
- 2.00±0.05s 727±20ms 0.36 time_predict('kd_tree', 'high', 11)
- 990±90ms 287±20ms 0.29 time_predict('ball_tree', 'low', 11)
- 2.56±0.2s 647±30ms 0.25 time_predict('ball_tree', 'high', 11)

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.

@itamarst itamarst changed the title Thread-safe binary tree (BallTree/KDTree) PERF Thread-safe binary tree (BallTree/KDTree) Jun 2, 2026
@itamarst
itamarst marked this pull request as ready for review June 2, 2026 18:18
@itamarst
itamarst marked this pull request as draft June 2, 2026 20:00
@itamarst

itamarst commented Jun 2, 2026 •

Copy link
Copy Markdown
Contributor Author

UPDATE: Added a test. It fails on main (on first assert, but if you comment that out it then fails on second assert).

@itamarst
itamarst marked this pull request as ready for review June 2, 2026 21:20
@itamarst itamarst changed the title PERF Thread-safe binary tree (BallTree/KDTree) PERF Thread-safe binary tree (BallTree/KDTree), makes KNeighborClassifier as much as 2-3× faster Jun 3, 2026
@itamarst itamarst changed the title PERF Thread-safe binary tree (BallTree/KDTree), makes KNeighborClassifier as much as 2-3× faster PERF Thread-safe binary tree (BallTree/KDTree), makes KNeighborsClassifier as much as 2-3× faster Jun 3, 2026

@ogrisel ogrisel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix/efficiency improvement.

It's a bit slower on a single thread but I think it's ok.

@itamarst

itamarst commented Jun 4, 2026

Copy link
Copy Markdown
Contributor Author

Comment thread sklearn/neighbors/_binary_tree.pxi.tp Resolved

@thomasjpfan thomasjpfan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@thomasjpfan
thomasjpfan merged commit fab08f7 into scikit-learn:main Jun 9, 2026
33 checks passed
@jeremiedbb

Copy link
Copy Markdown
Member

The thread-safety fix and performance improvement are useful but tbh I'd just deprecate these stats altogether:

  • They are not tested (apart from the test added in this PR)
  • They are not used anywhere in the doc besides appearing in the KDTree methods.
  • A quick github search doesn't report any usage of it by third-party projects

@itamarst

Copy link
Copy Markdown
Contributor Author

@jeremiedbb I opened a PR for that, #34242

@itamarst
itamarst deleted the 34175-thread-safe-binary-tree branch June 10, 2026 21:02
prady0t pushed a commit to prady0t/scikit-learn that referenced this pull request Sep 2, 2026
@jeremiedbb jeremiedbb mentioned this pull request Sep 8, 2026
14 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BinaryTree/KDTree statistics are not thread-safe (and might be a performance issue)

4 participants