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

Faster slice PartialOrd #28436

Merged
merged 6 commits into from
Sep 17, 2015
Merged
Show file tree
Hide file tree
Changes from 2 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
26 changes: 22 additions & 4 deletions src/libcore/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,17 +463,35 @@ mod impls {
}
}

partial_ord_impl! { char usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
partial_ord_impl! { f32 f64 }

macro_rules! ord_impl {
($($t:ty)*) => ($(
#[stable(feature = "rust1", since = "1.0.0")]
impl PartialOrd for $t {
#[inline]
fn partial_cmp(&self, other: &$t) -> Option<Ordering> {
if *self == *other { Some(Equal) }
else if *self < *other { Some(Less) }
else { Some(Greater) }
}
#[inline]
fn lt(&self, other: &$t) -> bool { (*self) < (*other) }
#[inline]
fn le(&self, other: &$t) -> bool { (*self) <= (*other) }
#[inline]
fn ge(&self, other: &$t) -> bool { (*self) >= (*other) }
#[inline]
fn gt(&self, other: &$t) -> bool { (*self) > (*other) }
}

#[stable(feature = "rust1", since = "1.0.0")]
impl Ord for $t {
#[inline]
fn cmp(&self, other: &$t) -> Ordering {
if *self < *other { Less }
else if *self > *other { Greater }
else { Equal }
if *self == *other { Equal }
else if *self < *other { Less }
else { Greater }
}
}
)*)
Expand Down
39 changes: 21 additions & 18 deletions src/libcore/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1558,31 +1558,34 @@ impl<T: Eq> Eq for [T] {}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: Ord> Ord for [T] {
#[inline]
fn cmp(&self, other: &[T]) -> Ordering {
self.iter().cmp(other.iter())
let l = cmp::min(self.len(), other.len());

for i in 0..l {
match self[i].cmp(&other[i]) {
Copy link
Member

Choose a reason for hiding this comment

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

Are bounds checks eliminated here? This suggests you need to slice self and other to length l for it to be.

Copy link
Member

Choose a reason for hiding this comment

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

(I guess this wasn't needed in the partialeq case since it requires equal lengths, and llvm understands that)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are right, in order to remove all bound checks I need to re-slice self and other, which further improves performance :)

Copy link
Member

Choose a reason for hiding this comment

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

Yay!

Copy link
Member

Choose a reason for hiding this comment

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

When you add this, maybe add a comment to indicate why you reslice.

Ordering::Equal => (),
non_eq => return non_eq,
}
}

self.len().cmp(&other.len())
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: PartialOrd> PartialOrd for [T] {
#[inline]
fn partial_cmp(&self, other: &[T]) -> Option<Ordering> {
self.iter().partial_cmp(other.iter())
}
#[inline]
fn lt(&self, other: &[T]) -> bool {
self.iter().lt(other.iter())
}
#[inline]
fn le(&self, other: &[T]) -> bool {
self.iter().le(other.iter())
}
#[inline]
fn ge(&self, other: &[T]) -> bool {
self.iter().ge(other.iter())
}
#[inline]
fn gt(&self, other: &[T]) -> bool {
self.iter().gt(other.iter())
let l = cmp::min(self.len(), other.len());

for i in 0..l {
match self[i].partial_cmp(&other[i]) {
Some(Ordering::Equal) => (),
non_eq => return non_eq,
}
}

self.len().partial_cmp(&other.len())
}
}