geo icon indicating copy to clipboard operation
geo copied to clipboard

Point in triangle and rect performance improvements

Open b4l opened this issue 2 years ago • 6 comments

  • [x] I agree to follow the project's code of conduct.
  • [x] I added an entry to CHANGES.md if knowledge of this change could be valuable to users.

b4l avatar Aug 24 '23 17:08 b4l

Thanks for the PR @b4l! Could you say a little about how this perf improvement works? Is it purely down to switching away from using Robust? Are there any drawbacks (e.g. precision issues?)

urschrei avatar Aug 25 '23 08:08 urschrei

From my rather superficial understanding, the previous versions involved a conversion to a polygon, which allocates a vector that is considered expensive. Then subsequent, in the case of rect robust predicates are suboptimal as there is no interpolation in play since it is axis-aligned and a simple comparison is adequate. For the triangle cases, this implementation leverages ideas around barycentric coordinates and half-plane calculations, which are somewhat robust in terms of not involving any division, though presumably not as robust as robust predicates, as the name suggests. For the latter implementations based on robust predicates without allocation are commented out offering lesser performance improvements given the robustness trade-off.

Regarding precision issues, I have only a very basic understanding of the robustness and no idea of the implications this has in practice. The test /bench with the point on the triangle line works as expected though actually looking for reviewers' input regarding that.

b4l avatar Aug 25 '23 14:08 b4l

Given the explanation (thank you!) and that it's passing tests currently, I'm inclined to merge this. Anyone else got input for or against?

urschrei avatar Oct 24 '23 10:10 urschrei

Let's make a decision on this as it's a perf improvement. As per the author's comments on Discord:

I can not vouch for the robustness, though there is a version using robust predicates for the triangle case commented out which still offers major performance improvements compared to the existing implementation. so maybe we can use this one for confidence?

So the conservative solution is to go with the robust predicate if there's some doubt about robustness. Alternatively, someone dreams up some more tests.

urschrei avatar Jan 22 '24 14:01 urschrei

Thanks for the PR @b4l ; looks good except the non-robust impl for Triangle; let's use the slightly slower robust impl. on that too.

+1 for removing the Vec allocations though.

rmanoka avatar Jan 23 '24 06:01 rmanoka

Thanks for reviewing and sorry for the late response! I changed to the versions using robust predicates.

Should I remove the commented-out versions?

Edit: CI failure seems to be unrelated, see https://github.com/georust/geo/issues/1149

b4l avatar Feb 19 '24 12:02 b4l

The ahash MSRV issue was resolved upstream https://github.com/tkaitchuck/aHash/pull/208

frewsxcv avatar Feb 23 '24 03:02 frewsxcv

Are we good to merge this before releasing? I know we have version control, but I'm personally OK with hanging on to the commented-out versions for now.

urschrei avatar Feb 23 '24 10:02 urschrei