Skip to content

perf: skip subtrees in the kd-tree search - #285

Open
ADAMAMZAR wants to merge 1 commit into
ShisatoYano:mainfrom
ADAMAMZAR:perf/kd-tree-skip-subtrees
Open

ADAMAMZAR wants to merge 1 commit into
ShisatoYano:mainfrom
ADAMAMZAR:perf/kd-tree-skip-subtrees

Conversation

@ADAMAMZAR

Copy link
Copy Markdown

Summary

Closes #281.

_search_nearest_neighbor_candidates now visits the near child first, and the far child only when the distance from the target to the splitting plane is within the search radius. The public methods and their results are unchanged. This is used by both search_nearest_neighbor_point and search_neighbor_points_within_r.

Nodes visited per search, averaged over 50 random targets:

Points Before After
100 100 14.6
1000 1000 20.8
10000 10000 26.9

How I checked it works

  • Added test/test_kd_tree.py:
    • nearest neighbor and neighbors within r match a brute-force search for random clouds of 10, 100 and 1000 points
    • a nearest neighbor search on 1000 points visits fewer than 100 nodes (this test fails on main, where every node is visited)
  • For the table above I ran the old and new code on the same clouds: both return the same nearest neighbor and the same neighbors within r for every target.
  • pytest -l on the whole suite: 145 passed.

Images

Last frame of the two simulations that use the kd-tree, before and after. search_nearest_neighbor_kd_tree is identical pixel for pixel. In search_neighbor_points_kd_tree the same neighbors are found, but they come back in a different order, so the green lines are drawn in a different order and 9 pixels differ where they overlap at the target.

search_nearest_neighbor_kd_tree_before_after search_neighbor_points_kd_tree_before_after

Book

Visit the near child first and the far child only when the splitting plane is within the search radius. Results are unchanged; add tests that compare both searches with a brute-force search and check that a search visits far fewer nodes than the tree has.
Copilot AI balanced review requested due to automatic review settings November 24, 2026 10:24

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

This branch has not been deployed

No deployments
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.

Skip subtrees in the kd-tree search

2 participants