Skip to content

Commit

Permalink
Rollup merge of rust-lang#61665 - aschampion:slice-eq-ptr, r=sfackler
Browse files Browse the repository at this point in the history
core: check for pointer equality when comparing Eq slices

Because `Eq` types must be reflexively equal, an equal-length slice to the same memory location must be equal.

This is related to rust-lang#33892 (and rust-lang#32699) answering this comment from that PR:

> Great! One more easy question: why does this optimization not apply in the non-BytewiseEquality implementation directly above?

Because slices of non-reflexively equal types (like `f64`) are not equal even if it's the same slice. But if the types are `Eq`, we can use this same-address optimization, which this PR implements. Obviously this changes behavior if types violate the reflexivity condition of `Eq`, because their impls of `PartialEq` will no longer be called per-item, but 🤷‍♂ .

It's not clear how often this optimization comes up in the real world outside of the same-`&str` case covered by rust-lang#33892, so **I'm requesting a perf run** (on MacOS today, so can't run `rustc_perf` myself). I'm going ahead and making the PR on the basis of being surprised things didn't already work this way.

This is my first time hacking rust itself, so as a perf sanity check I ran `./x.py bench --stage 0 src/lib{std,alloc}`, but the differences were noisy.

To make the existing specialization for `BytewiseEquality` explicit, it's now a supertrait of `Eq + Copy`. `Eq` should be sufficient, but `Copy` was included for clarity.
  • Loading branch information
Centril committed Jul 11, 2019
2 parents 35cacbc + d482589 commit ea62f9b
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/libcore/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5342,13 +5342,24 @@ impl<A, B> SlicePartialEq<B> for [A]
return false;
}

for i in 0..self.len() {
if !self[i].eq(&other[i]) {
return false;
}
self.iter().zip(other.iter()).all(|(x, y)| x == y)
}
}

// Use an equal-pointer optimization when types are `Eq`
impl<A> SlicePartialEq<A> for [A]
where A: PartialEq<A> + Eq
{
default fn equal(&self, other: &[A]) -> bool {
if self.len() != other.len() {
return false;
}

if self.as_ptr() == other.as_ptr() {
return true;
}

true
self.iter().zip(other.iter()).all(|(x, y)| x == y)
}
}

Expand Down Expand Up @@ -5457,7 +5468,7 @@ impl SliceOrd<u8> for [u8] {
#[doc(hidden)]
/// Trait implemented for types that can be compared for equality using
/// their bytewise representation
trait BytewiseEquality { }
trait BytewiseEquality: Eq + Copy { }

macro_rules! impl_marker_for {
($traitname:ident, $($ty:ty)*) => {
Expand Down

0 comments on commit ea62f9b

Please sign in to comment.