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

KdTree::queryNode(): rewrite it to avoid blowing up the stack #481

Merged
merged 1 commit into from
Sep 24, 2021

Conversation

rouault
Copy link
Contributor

@rouault rouault commented Sep 23, 2021

Fixes qgis/QGIS#45226

While we are it, also avoid recursive formulation for queryNodePoint()

Could be worth backporting to 3.9 branch

Fixes qgis/QGIS#45226

While we are it, also avoid recursive formulation for queryNodePoint()
@rouault
Copy link
Contributor Author

rouault commented Sep 24, 2021

@strk strk merged commit d107fcc into libgeos:main Sep 24, 2021
@dr-jts
Copy link
Contributor

dr-jts commented Sep 24, 2021

Thanks for the patch. It's always better to have an iterative algorithm rather than a recursive one, to avoid blowing out the stack.

The geometry causing the problem is pretty crazy - a polygonal coverage with hundreds of thousands of mis-aligned vertices! Kdtrees built on coherent sequences of coordinates (such as occur in polygons) are often highly unbalanced, and I suspect this is what is going on in this case. Switching to using an explicit stack avoids issues with stack overflow, but unfortunately doesn't do anything to improve performance. Elsewhere in OverlayNG randomization has been used to avoid this problem, but not sure if that is possible in this algorithm.

@rouault
Copy link
Contributor Author

rouault commented Sep 24, 2021

and what about rebalancing the tree ?

@dr-jts
Copy link
Contributor

dr-jts commented Sep 24, 2021

My research indicated that balancing KD-trees is complex. I guess it might be possible to monitor for an unbalanced condition and rebuild the tree when it occurs. But there's going to be some trade-off with performance, which would require careful turning.

This is likely a rare situation, so given the complexity of code required it's low on my priority list.

@dr-jts
Copy link
Contributor

dr-jts commented Oct 4, 2021

Fixed in PR JTS-779.

Also includes some refactoring to clarify code. It would be good to port this back to GEOS.

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.

Geoprocessing tools cause libgeos to throw SIGBUS
3 participants