From fa11f57ea4969547ec1f18e69a0b7609b21c704f Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Mon, 18 Apr 2022 00:01:05 -0400 Subject: [PATCH] Inline coords to improve perf * Inline all simple coordinate methods * 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 --- geo-types/src/coordinate.rs | 22 ++++++++++++++++++++++ geo-types/src/macros.rs | 8 ++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/geo-types/src/coordinate.rs b/geo-types/src/coordinate.rs index 9b720d910..5abf0fb68 100644 --- a/geo-types/src/coordinate.rs +++ b/geo-types/src/coordinate.rs @@ -31,6 +31,7 @@ pub struct Coordinate { } impl From<(T, T)> for Coordinate { + #[inline] fn from(coords: (T, T)) -> Self { coord! { x: coords.0, @@ -40,6 +41,7 @@ impl From<(T, T)> for Coordinate { } impl From<[T; 2]> for Coordinate { + #[inline] fn from(coords: [T; 2]) -> Self { coord! { x: coords[0], @@ -49,6 +51,7 @@ impl From<[T; 2]> for Coordinate { } impl From> for Coordinate { + #[inline] fn from(point: Point) -> Self { coord! { x: point.x(), @@ -58,12 +61,14 @@ impl From> for Coordinate { } impl From> for (T, T) { + #[inline] fn from(coord: Coordinate) -> Self { (coord.x, coord.y) } } impl From> for [T; 2] { + #[inline] fn from(coord: Coordinate) -> Self { [coord.x, coord.y] } @@ -86,6 +91,7 @@ impl Coordinate { /// assert_eq!(y, 116.34); /// assert_eq!(x, 40.02f64); /// ``` + #[inline] pub fn x_y(&self) -> (T, T) { (self.x, self.y) } @@ -112,6 +118,7 @@ where { type Output = Self; + #[inline] fn neg(self) -> Self { coord! { x: -self.x, @@ -137,6 +144,7 @@ where impl Add for Coordinate { type Output = Self; + #[inline] fn add(self, rhs: Self) -> Self { coord! { x: self.x + rhs.x, @@ -162,6 +170,7 @@ impl Add for Coordinate { impl Sub for Coordinate { type Output = Self; + #[inline] fn sub(self, rhs: Self) -> Self { coord! { x: self.x - rhs.x, @@ -186,6 +195,7 @@ impl Sub for Coordinate { impl Mul for Coordinate { type Output = Self; + #[inline] fn mul(self, rhs: T) -> Self { coord! { x: self.x * rhs, @@ -210,6 +220,7 @@ impl Mul for Coordinate { impl Div for Coordinate { type Output = Self; + #[inline] fn div(self, rhs: T) -> Self { coord! { x: self.x / rhs, @@ -233,6 +244,7 @@ use num_traits::Zero; /// assert_eq!(p.y, 0.); /// ``` impl Coordinate { + #[inline] pub fn zero() -> Self { coord! { x: T::zero(), @@ -242,9 +254,11 @@ impl Coordinate { } impl Zero for Coordinate { + #[inline] fn zero() -> Self { Self::zero() } + #[inline] fn is_zero(&self) -> bool { self.x.is_zero() && self.y.is_zero() } @@ -290,10 +304,12 @@ impl UlpsEq for Coordinate 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) @@ -309,6 +325,7 @@ where const DIMENSIONS: usize = 2; + #[inline] fn generate(generator: impl Fn(usize) -> Self::Scalar) -> Self { coord! { x: generator(0), @@ -316,6 +333,7 @@ where } } + #[inline] fn nth(&self, index: usize) -> Self::Scalar { match index { 0 => self.x, @@ -324,6 +342,7 @@ where } } + #[inline] fn nth_mut(&mut self, index: usize) -> &mut Self::Scalar { match index { 0 => &mut self.x, @@ -342,6 +361,7 @@ where const DIMENSIONS: usize = 2; + #[inline] fn generate(mut generator: impl FnMut(usize) -> Self::Scalar) -> Self { coord! { x: generator(0), @@ -349,6 +369,7 @@ where } } + #[inline] fn nth(&self, index: usize) -> Self::Scalar { match index { 0 => self.x, @@ -357,6 +378,7 @@ where } } + #[inline] fn nth_mut(&mut self, index: usize) -> &mut Self::Scalar { match index { 0 => &mut self.x, diff --git a/geo-types/src/macros.rs b/geo-types/src/macros.rs index 558525f7a..ff69cc865 100644 --- a/geo-types/src/macros.rs +++ b/geo-types/src/macros.rs @@ -55,7 +55,7 @@ macro_rules! point { /// [`Coordinate`]: ./struct.Point.html #[macro_export] macro_rules! coord { - (x: $x:expr, y: $y:expr $(,)?) => { + (x: $x:expr, y: $y:expr $(,)* ) => { $crate::Coordinate { x: $x, y: $y } }; } @@ -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),* $(,)? )),* $(,)? @@ -138,7 +138,7 @@ macro_rules! line_string { $($coord:expr),* $(,)? ) => { - $crate::LineString( + $crate::LineString::new( <[_]>::into_vec( ::std::boxed::Box::new( [$($coord), *] @@ -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);