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

Inline coords to boost perf improvements #812

Merged
merged 1 commit into from
Apr 23, 2022
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
22 changes: 22 additions & 0 deletions geo-types/src/coordinate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub struct Coordinate<T: CoordNum> {
}

impl<T: CoordNum> From<(T, T)> for Coordinate<T> {
#[inline]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are good inlines IMO! Thanks!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does #[inline] even do much for generic code?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lnicola that's a good question - but would require too substantial of a research to pinpoint why adding these inlines boosts performance. I think it is far easier to just add inline everywhere it seems relevant (tiny code, frequent use).

fn from(coords: (T, T)) -> Self {
coord! {
x: coords.0,
Expand All @@ -40,6 +41,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 +51,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 +61,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 +91,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 +118,7 @@ where
{
type Output = Self;

#[inline]
fn neg(self) -> Self {
coord! {
x: -self.x,
Expand All @@ -137,6 +144,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 +170,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 +195,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 +220,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 +244,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 +254,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 +304,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 +325,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 +342,7 @@ where
}
}

#[inline]
fn nth_mut(&mut self, index: usize) -> &mut Self::Scalar {
match index {
0 => &mut self.x,
Expand All @@ -342,13 +361,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 +378,7 @@ where
}
}

#[inline]
fn nth_mut(&mut self, index: usize) -> &mut Self::Scalar {
match index {
0 => &mut self.x,
Expand Down
8 changes: 4 additions & 4 deletions geo-types/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
};
}
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