Skip to content

Commit

Permalink
PERF: TriangulateSpade uses Iter, not Vec
Browse files Browse the repository at this point in the history
This is an internal API, so we can safely break the API.

Bench output:
```
TriangulateSpade (unconstrained)
                        time:   [8.8442 ms 8.8522 ms 8.8619 ms]
                        change: [-2.9583% -2.7775% -2.6122%] (p = 0.00 <
0.05)
                        Performance has improved.
Found 4 outliers among 100 measurements (4.00%)
  1 (1.00%) high mild
  3 (3.00%) high severe

TriangulateSpade (constrained)
                        time:   [8.9126 ms 8.9274 ms 8.9444 ms]
                        change: [-2.4017% -2.1584% -1.9066%] (p = 0.00 <
0.05)
                        Performance has improved.
Found 3 outliers among 100 measurements (3.00%)
  3 (3.00%) high severe

TriangulateEarcut       time:   [6.9632 ms 6.9750 ms 6.9883 ms]
                        change: [-1.0174% -0.7692% -0.5114%] (p = 0.00 <
0.05)
                        Change within noise threshold.
Found 2 outliers among 100 measurements (2.00%)
```
  • Loading branch information
michaelkirk committed Dec 5, 2023
1 parent 993204d commit f55e977
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
2 changes: 2 additions & 0 deletions geo/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* <https://github.com/georust/geo/pull/1119>
* Add `LineStringSegmentizeHaversine` trait as a an alternative to `LineStringSegmentize` for geographic coordinates.
* <https://github.com/georust/geo/pull/1107>
* PERF: small improvements to TriangulateSpade trait
* <https://github.com/georust/geo/pull/1122>

## 0.27.0

Expand Down
21 changes: 12 additions & 9 deletions geo/src/algorithm/triangulate_spade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ pub type Triangles<T> = Vec<Triangle<T>>;
// so that we don't leak these weird methods on the public interface.
mod private {
use super::*;

pub(crate) type CoordsIter<'a, T> = Box<dyn Iterator<Item = Coord<T>> + 'a>;

pub trait TriangulationRequirementTrait<'a, T>
where
T: SpadeTriangulationFloat,
Expand All @@ -76,7 +79,7 @@ mod private {
fn lines(&'a self) -> Vec<Line<T>>;
/// collect all the coords that are relevant for triangulations from the geometric object that
/// should be triangulated
fn coords(&'a self) -> Vec<Coord<T>>;
fn coords(&'a self) -> CoordsIter<T>;
/// define a predicate that decides if a point is inside of the object (used for constrained triangulation)
fn contains_point(&'a self, p: Point<T>) -> bool;

Expand Down Expand Up @@ -315,8 +318,8 @@ where
T: SpadeTriangulationFloat,
G: LinesIter<'l, Scalar = T> + CoordsIter<Scalar = T> + Contains<Point<T>>,
{
fn coords(&'a self) -> Vec<Coord<T>> {
self.coords_iter().collect::<Vec<_>>()
fn coords(&'a self) -> private::CoordsIter<T> {
Box::new(self.coords_iter())
}

fn lines(&'a self) -> Vec<Line<T>> {
Expand All @@ -333,11 +336,11 @@ where

impl<'a, T, G> private::TriangulationRequirementTrait<'a, T> for Vec<G>
where
T: SpadeTriangulationFloat,
T: SpadeTriangulationFloat + 'a,
G: TriangulateSpade<'a, T>,
{
fn coords(&'a self) -> Vec<Coord<T>> {
self.iter().flat_map(|g| g.coords()).collect::<Vec<_>>()
fn coords(&'a self) -> private::CoordsIter<T> {
Box::new(self.iter().flat_map(|g| g.coords()))
}

fn lines(&'a self) -> Vec<Line<T>> {
Expand All @@ -351,11 +354,11 @@ where

impl<'a, T, G> private::TriangulationRequirementTrait<'a, T> for &[G]
where
T: SpadeTriangulationFloat,
T: SpadeTriangulationFloat + 'a,
G: TriangulateSpade<'a, T>,
{
fn coords(&'a self) -> Vec<Coord<T>> {
self.iter().flat_map(|g| g.coords()).collect::<Vec<_>>()
fn coords(&'a self) -> private::CoordsIter<T> {
Box::new(self.iter().flat_map(|g| g.coords()))
}

fn lines(&'a self) -> Vec<Line<T>> {
Expand Down

0 comments on commit f55e977

Please sign in to comment.