Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

small performance improvement in QuadTree, code cleanup #3253

Merged
merged 3 commits into from
May 4, 2024

Conversation

mrieser
Copy link
Contributor

@mrieser mrieser commented May 4, 2024

try not to call Math.sqrt() for distance calculations, but work with the squared-distance where possible. Results in 2-3% faster getClosest()-calls.

try not to call Math.sqrt() for distance calculations, but work with the squared-distance where possible.
Results in 2-3% faster getClosest()-calls.
@mrieser mrieser self-assigned this May 4, 2024
@mrieser mrieser enabled auto-merge May 4, 2024 18:19
mrieser added 2 commits May 5, 2024 00:10
Reason the results are different: The new QuadTree is also more exact. When searching for the closest item in a QuadTree to a given coordinate, the old code calculated the euclidean distance between item and coordinate, while the new code calculates the square of the euclidean distance (essentially not performing the final Math.sqrt() in the Pythagorean formula). If two floating point values are nearly equal and only differ in the least significant digit, it could happen that the square-root of the values were actually identical due to the limit of significant digits. This resulted in the old QuadTree in the first item being found to be returned, while the new QuadTree might find the second item (which is just a very small amount closer to x/y) to be returned.

The aforementioned case was observed in the failing tests, e.g. where one item had a calculated distance of Math.sqrt(52643.632572290044), while the other had Math.sqrt(52643.632572290040). The old code returned the first one, while the new code returns the second one.
@mrieser mrieser merged commit 670fd95 into master May 4, 2024
49 checks passed
@mrieser mrieser deleted the speedup-quadtree branch May 4, 2024 22:35
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