Skip to content

Commit 733cb54

Browse files
committed
Remove pointer comparison from slice equality
This resurrects rust-lang#71735. Fixes rust-lang#71602, helps with rust-lang#80140. r? `@Mark-Simulacrum`
1 parent 1f5bc17 commit 733cb54

File tree

2 files changed

+16
-27
lines changed

2 files changed

+16
-27
lines changed

Diff for: library/core/src/slice/cmp.rs

-27
Original file line numberDiff line numberDiff line change
@@ -75,28 +75,6 @@ where
7575
}
7676
}
7777

78-
// Use an equal-pointer optimization when types are `Eq`
79-
// We can't make `A` and `B` the same type because `min_specialization` won't
80-
// allow it.
81-
impl<A, B> SlicePartialEq<B> for [A]
82-
where
83-
A: MarkerEq<B>,
84-
{
85-
default fn equal(&self, other: &[B]) -> bool {
86-
if self.len() != other.len() {
87-
return false;
88-
}
89-
90-
// While performance would suffer if `guaranteed_eq` just returned `false`
91-
// for all arguments, correctness and return value of this function are not affected.
92-
if self.as_ptr().guaranteed_eq(other.as_ptr() as *const A) {
93-
return true;
94-
}
95-
96-
self.iter().zip(other.iter()).all(|(x, y)| x == y)
97-
}
98-
}
99-
10078
// Use memcmp for bytewise equality when the types allow
10179
impl<A, B> SlicePartialEq<B> for [A]
10280
where
@@ -107,11 +85,6 @@ where
10785
return false;
10886
}
10987

110-
// While performance would suffer if `guaranteed_eq` just returned `false`
111-
// for all arguments, correctness and return value of this function are not affected.
112-
if self.as_ptr().guaranteed_eq(other.as_ptr() as *const A) {
113-
return true;
114-
}
11588
// SAFETY: `self` and `other` are references and are thus guaranteed to be valid.
11689
// The two slices have been checked to have the same size above.
11790
unsafe {

Diff for: src/test/codegen/slice-ref-equality.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// compile-flags: -C opt-level=3
2+
3+
#![crate_type = "lib"]
4+
5+
// #71602: check that slice equality just generates a single bcmp
6+
7+
// CHECK-LABEL: @is_zero_slice
8+
#[no_mangle]
9+
pub fn is_zero_slice(data: &[u8; 4]) -> bool {
10+
// CHECK: start:
11+
// CHECK-NEXT: %{{.+}} = getelementptr {{.+}}
12+
// CHECK-NEXT: %[[BCMP:.+]] = tail call i32 @{{bcmp|memcmp}}({{.+}})
13+
// CHECK-NEXT: %[[EQ:.+]] = icmp eq i32 %[[BCMP]], 0
14+
// CHECK-NEXT: ret i1 %[[EQ]]
15+
*data == [0; 4]
16+
}

0 commit comments

Comments
 (0)