Skip to content

Commit

Permalink
Bump proj, perf improvements
Browse files Browse the repository at this point in the history
* Bump proj dependency to 0.26
* Inline all simple coordinate methods
* create a new hidden `new__()` method that will be changed with 3D migration.  This is needed because it seems that `Coordinate {x, y}` instantiation from `coord!` macro causes a significant performance degradation (~7% for `cargo bench relate`)
* use `new()` method in some macros

test | improvement
---|---
concave hull f32 | -1.8840%
point outside polygon | -4.9574%
Polygon Euclidean distance rotating calipers f64 | -1.6313%
simplify vwp f32 | -7.0465%

the rest were statistically unchanged
  • Loading branch information
nyurik committed Apr 18, 2022
1 parent a588c34 commit 6e3ba30
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 18 deletions.
24 changes: 12 additions & 12 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
matrix:
container_image:
# Use the latest stable version. No need for older versions.
- "georust/geo-ci:rust-1.59"
- "georust/geo-ci:proj-9.0.0-rust-1.59"
container:
image: ${{ matrix.container_image }}
steps:
Expand All @@ -66,10 +66,10 @@ jobs:
# giving us about 6 months of coverage.
#
# Minimum supported rust version (MSRV)
- "georust/geo-ci:rust-1.56"
- "georust/geo-ci:proj-9.0.0-rust-1.56"
# Two most recent releases - we omit older ones for expedient CI
- "georust/geo-ci:rust-1.58"
- "georust/geo-ci:rust-1.59"
- "georust/geo-ci:proj-9.0.0-rust-1.58"
- "georust/geo-ci:proj-9.0.0-rust-1.59"
container:
image: ${{ matrix.container_image }}
steps:
Expand All @@ -92,10 +92,10 @@ jobs:
# giving us about 6 months of coverage.
#
# Minimum supported rust version (MSRV)
- "georust/geo-ci:rust-1.56"
- "georust/geo-ci:proj-9.0.0-rust-1.56"
# Two most recent releases - we omit older ones for expedient CI
- "georust/geo-ci:rust-1.58"
- "georust/geo-ci:rust-1.59"
- "georust/geo-ci:proj-9.0.0-rust-1.58"
- "georust/geo-ci:proj-9.0.0-rust-1.59"
container:
image: ${{ matrix.container_image }}
steps:
Expand All @@ -119,10 +119,10 @@ jobs:
# giving us about 6 months of coverage.
#
# Minimum supported rust version (MSRV)
- "georust/geo-ci:rust-1.56"
- "georust/geo-ci:proj-9.0.0-rust-1.56"
# Two most recent releases - we omit older ones for expedient CI
- "georust/geo-ci:rust-1.58"
- "georust/geo-ci:rust-1.59"
- "georust/geo-ci:proj-9.0.0-rust-1.58"
- "georust/geo-ci:proj-9.0.0-rust-1.59"
container:
image: ${{ matrix.container_image }}
steps:
Expand All @@ -142,7 +142,7 @@ jobs:
matrix:
container_image:
# Fuzz only on latest
- "georust/geo-ci:rust-1.59"
- "georust/geo-ci:proj-9.0.0-rust-1.59"
container:
image: ${{ matrix.container_image }}
steps:
Expand All @@ -155,7 +155,7 @@ jobs:
runs-on: ubuntu-latest
if: "!contains(github.event.head_commit.message, '[skip ci]')"
container:
image: georust/geo-ci:rust-1.59
image: georust/geo-ci:proj-9.0.0-rust-1.59
steps:
- name: Checkout repository
uses: actions/checkout@v2
Expand Down
32 changes: 32 additions & 0 deletions geo-types/src/coordinate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,18 @@ pub struct Coordinate<T: CoordNum> {
pub y: T,
}

impl<T: CoordNum> Coordinate<T> {
/// Create a new instance of a coordinate.
/// **ATTENTION** Use [`coord!`] macro or one of the [`from`] methods instead.
#[inline]
#[doc(hidden)]
pub fn new__(x: T, y: T) -> Self {
Self { x, y }
}
}

impl<T: CoordNum> From<(T, T)> for Coordinate<T> {
#[inline]
fn from(coords: (T, T)) -> Self {
coord! {
x: coords.0,
Expand All @@ -40,6 +51,7 @@ impl<T: CoordNum> From<(T, T)> for Coordinate<T> {
}

impl<T: CoordNum> From<[T; 2]> for Coordinate<T> {
#[inline]
fn from(coords: [T; 2]) -> Self {
coord! {
x: coords[0],
Expand All @@ -49,6 +61,7 @@ impl<T: CoordNum> From<[T; 2]> for Coordinate<T> {
}

impl<T: CoordNum> From<Point<T>> for Coordinate<T> {
#[inline]
fn from(point: Point<T>) -> Self {
coord! {
x: point.x(),
Expand All @@ -58,12 +71,14 @@ impl<T: CoordNum> From<Point<T>> for Coordinate<T> {
}

impl<T: CoordNum> From<Coordinate<T>> for (T, T) {
#[inline]
fn from(coord: Coordinate<T>) -> Self {
(coord.x, coord.y)
}
}

impl<T: CoordNum> From<Coordinate<T>> for [T; 2] {
#[inline]
fn from(coord: Coordinate<T>) -> Self {
[coord.x, coord.y]
}
Expand All @@ -86,6 +101,7 @@ impl<T: CoordNum> Coordinate<T> {
/// assert_eq!(y, 116.34);
/// assert_eq!(x, 40.02f64);
/// ```
#[inline]
pub fn x_y(&self) -> (T, T) {
(self.x, self.y)
}
Expand All @@ -112,6 +128,7 @@ where
{
type Output = Self;

#[inline]
fn neg(self) -> Self {
coord! {
x: -self.x,
Expand All @@ -137,6 +154,7 @@ where
impl<T: CoordNum> Add for Coordinate<T> {
type Output = Self;

#[inline]
fn add(self, rhs: Self) -> Self {
coord! {
x: self.x + rhs.x,
Expand All @@ -162,6 +180,7 @@ impl<T: CoordNum> Add for Coordinate<T> {
impl<T: CoordNum> Sub for Coordinate<T> {
type Output = Self;

#[inline]
fn sub(self, rhs: Self) -> Self {
coord! {
x: self.x - rhs.x,
Expand All @@ -186,6 +205,7 @@ impl<T: CoordNum> Sub for Coordinate<T> {
impl<T: CoordNum> Mul<T> for Coordinate<T> {
type Output = Self;

#[inline]
fn mul(self, rhs: T) -> Self {
coord! {
x: self.x * rhs,
Expand All @@ -210,6 +230,7 @@ impl<T: CoordNum> Mul<T> for Coordinate<T> {
impl<T: CoordNum> Div<T> for Coordinate<T> {
type Output = Self;

#[inline]
fn div(self, rhs: T) -> Self {
coord! {
x: self.x / rhs,
Expand All @@ -233,6 +254,7 @@ use num_traits::Zero;
/// assert_eq!(p.y, 0.);
/// ```
impl<T: CoordNum> Coordinate<T> {
#[inline]
pub fn zero() -> Self {
coord! {
x: T::zero(),
Expand All @@ -242,9 +264,11 @@ impl<T: CoordNum> Coordinate<T> {
}

impl<T: CoordNum> Zero for Coordinate<T> {
#[inline]
fn zero() -> Self {
Self::zero()
}
#[inline]
fn is_zero(&self) -> bool {
self.x.is_zero() && self.y.is_zero()
}
Expand Down Expand Up @@ -290,10 +314,12 @@ impl<T: CoordNum + UlpsEq> UlpsEq for Coordinate<T>
where
T::Epsilon: Copy,
{
#[inline]
fn default_max_ulps() -> u32 {
T::default_max_ulps()
}

#[inline]
fn ulps_eq(&self, other: &Self, epsilon: T::Epsilon, max_ulps: u32) -> bool {
T::ulps_eq(&self.x, &other.x, epsilon, max_ulps)
&& T::ulps_eq(&self.y, &other.y, epsilon, max_ulps)
Expand All @@ -309,13 +335,15 @@ where

const DIMENSIONS: usize = 2;

#[inline]
fn generate(generator: impl Fn(usize) -> Self::Scalar) -> Self {
coord! {
x: generator(0),
y: generator(1),
}
}

#[inline]
fn nth(&self, index: usize) -> Self::Scalar {
match index {
0 => self.x,
Expand All @@ -324,6 +352,7 @@ where
}
}

#[inline]
fn nth_mut(&mut self, index: usize) -> &mut Self::Scalar {
match index {
0 => &mut self.x,
Expand All @@ -342,13 +371,15 @@ where

const DIMENSIONS: usize = 2;

#[inline]
fn generate(mut generator: impl FnMut(usize) -> Self::Scalar) -> Self {
coord! {
x: generator(0),
y: generator(1),
}
}

#[inline]
fn nth(&self, index: usize) -> Self::Scalar {
match index {
0 => self.x,
Expand All @@ -357,6 +388,7 @@ where
}
}

#[inline]
fn nth_mut(&mut self, index: usize) -> &mut Self::Scalar {
match index {
0 => &mut self.x,
Expand Down
10 changes: 5 additions & 5 deletions geo-types/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ macro_rules! point {
/// [`Coordinate`]: ./struct.Point.html
#[macro_export]
macro_rules! coord {
(x: $x:expr, y: $y:expr $(,)?) => {
$crate::Coordinate { x: $x, y: $y }
(x: $x:expr, y: $y:expr $(,)* ) => {
$crate::Coordinate::new__($x, $y)
};
}

Expand Down Expand Up @@ -123,7 +123,7 @@ macro_rules! coord {
/// [`LineString`]: ./line_string/struct.LineString.html
#[macro_export]
macro_rules! line_string {
() => { $crate::LineString(vec![]) };
() => { $crate::LineString::new(vec![]) };
(
$(( $($tag:tt : $val:expr),* $(,)? )),*
$(,)?
Expand All @@ -138,7 +138,7 @@ macro_rules! line_string {
$($coord:expr),*
$(,)?
) => {
$crate::LineString(
$crate::LineString::new(
<[_]>::into_vec(
::std::boxed::Box::new(
[$($coord), *]
Expand Down Expand Up @@ -296,7 +296,7 @@ macro_rules! polygon {
mod test {
#[test]
fn test_point() {
let p = point!(x: 1.2, y: 3.4);
let p = point! { x: 1.2, y: 3.4 };
assert_eq!(p.x(), 1.2);
assert_eq!(p.y(), 3.4);

Expand Down
2 changes: 1 addition & 1 deletion geo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ geo-types = { version = "0.7.3", features = ["approx", "use-rstar"] }
geographiclib-rs = "0.2"
log = "0.4.11"
num-traits = "0.2"
proj = { version = "0.25.2", optional = true }
proj = { version = "0.26.0", optional = true }
robust = "0.2.2"
rstar = "0.8"
serde = { version = "1.0", optional = true, features = ["derive"] }
Expand Down

0 comments on commit 6e3ba30

Please sign in to comment.