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 perf improvements to TriangulateSpade #1122

Merged
merged 2 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions geo/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* <https://github.com/georust/geo/pull/1107>
* Make `SpadeTriangulationConfig` actually configurable
* <https://github.com/georust/geo/pull/1123>
* PERF: small improvements to TriangulateSpade trait
* <https://github.com/georust/geo/pull/1122>

## 0.27.0

Expand Down
4 changes: 4 additions & 0 deletions geo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,7 @@ harness = false
[[bench]]
name = "monotone_subdiv"
harness = false

[[bench]]
name = "triangulate"
harness = false
81 changes: 81 additions & 0 deletions geo/benches/triangulate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use criterion::{criterion_group, criterion_main};
use geo::algorithm::{TriangulateEarcut, TriangulateSpade};
use geo::geometry::Polygon;
use geo::triangulate_spade::SpadeTriangulationConfig;

fn criterion_benchmark(c: &mut criterion::Criterion) {
c.bench_function(
"TriangulateSpade (unconstrained) - small polys",
|bencher| {
let multi_poly = geo_test_fixtures::nl_zones::<f64>();
bencher.iter(|| {
for poly in &multi_poly {
let triangulation =
TriangulateSpade::unconstrained_triangulation(poly).unwrap();
assert!(triangulation.len() > 1);
criterion::black_box(triangulation);
}
});
},
);

c.bench_function("TriangulateSpade (constrained) - small polys", |bencher| {
let multi_poly = geo_test_fixtures::nl_zones::<f64>();
bencher.iter(|| {
for poly in &multi_poly {
let triangulation = TriangulateSpade::constrained_triangulation(
poly,
SpadeTriangulationConfig { snap_radius: 1e-8 },
)
.unwrap();
assert!(triangulation.len() > 1);
criterion::black_box(triangulation);
}
});
});

c.bench_function("TriangulateEarcut - small polys", |bencher| {
let multi_poly = geo_test_fixtures::nl_zones::<f64>();
bencher.iter(|| {
for poly in &multi_poly {
let triangulation = TriangulateEarcut::earcut_triangles(poly);
assert!(triangulation.len() > 1);
criterion::black_box(triangulation);
}
});
});

c.bench_function("TriangulateSpade (unconstrained) - large_poly", |bencher| {
let poly = Polygon::new(geo_test_fixtures::norway_main::<f64>(), vec![]);
bencher.iter(|| {
let triangulation = TriangulateSpade::unconstrained_triangulation(&poly).unwrap();
assert!(triangulation.len() > 1);
criterion::black_box(triangulation);
});
});

c.bench_function("TriangulateSpade (constrained) - large_poly", |bencher| {
let poly = Polygon::new(geo_test_fixtures::norway_main::<f64>(), vec![]);
bencher.iter(|| {
let triangulation = TriangulateSpade::constrained_triangulation(
&poly,
SpadeTriangulationConfig { snap_radius: 1e-8 },
)
.unwrap();
assert!(triangulation.len() > 1);
criterion::black_box(triangulation);
});
});

c.bench_function("TriangulateEarcut - large_poly", |bencher| {
let poly = Polygon::new(geo_test_fixtures::norway_main::<f64>(), vec![]);
bencher.iter(|| {
let triangulation = TriangulateEarcut::earcut_triangles(&poly);
assert!(triangulation.len() > 1);
criterion::black_box(triangulation);
});
});
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
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