about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2020-12-26 06:43:51 +0000
committerbors <bors@rust-lang.org>2020-12-26 06:43:51 +0000
commit733cb54d18c6c3bbd8dfcd6184cdf678bd64bd0e (patch)
tree7d3ac4690369953409e35bfde95c56d197d688f9
parent1f5bc176b0e54a8e464704adcd7e571700207fe9 (diff)
downloadrust-733cb54d18c6c3bbd8dfcd6184cdf678bd64bd0e.tar.gz
rust-733cb54d18c6c3bbd8dfcd6184cdf678bd64bd0e.zip
Remove pointer comparison from slice equality
This resurrects #71735.

Fixes #71602, helps with #80140.

r? `@Mark-Simulacrum`
-rw-r--r--library/core/src/slice/cmp.rs27
-rw-r--r--src/test/codegen/slice-ref-equality.rs16
2 files changed, 16 insertions, 27 deletions
diff --git a/library/core/src/slice/cmp.rs b/library/core/src/slice/cmp.rs
index 18073f4afed..72af47c71dd 100644
--- a/library/core/src/slice/cmp.rs
+++ b/library/core/src/slice/cmp.rs
@@ -75,28 +75,6 @@ where
     }
 }
 
-// Use an equal-pointer optimization when types are `Eq`
-// We can't make `A` and `B` the same type because `min_specialization` won't
-// allow it.
-impl<A, B> SlicePartialEq<B> for [A]
-where
-    A: MarkerEq<B>,
-{
-    default fn equal(&self, other: &[B]) -> bool {
-        if self.len() != other.len() {
-            return false;
-        }
-
-        // While performance would suffer if `guaranteed_eq` just returned `false`
-        // for all arguments, correctness and return value of this function are not affected.
-        if self.as_ptr().guaranteed_eq(other.as_ptr() as *const A) {
-            return true;
-        }
-
-        self.iter().zip(other.iter()).all(|(x, y)| x == y)
-    }
-}
-
 // Use memcmp for bytewise equality when the types allow
 impl<A, B> SlicePartialEq<B> for [A]
 where
@@ -107,11 +85,6 @@ where
             return false;
         }
 
-        // While performance would suffer if `guaranteed_eq` just returned `false`
-        // for all arguments, correctness and return value of this function are not affected.
-        if self.as_ptr().guaranteed_eq(other.as_ptr() as *const A) {
-            return true;
-        }
         // SAFETY: `self` and `other` are references and are thus guaranteed to be valid.
         // The two slices have been checked to have the same size above.
         unsafe {
diff --git a/src/test/codegen/slice-ref-equality.rs b/src/test/codegen/slice-ref-equality.rs
new file mode 100644
index 00000000000..acc7879e7b1
--- /dev/null
+++ b/src/test/codegen/slice-ref-equality.rs
@@ -0,0 +1,16 @@
+// compile-flags: -C opt-level=3
+
+#![crate_type = "lib"]
+
+// #71602: check that slice equality just generates a single bcmp
+
+// CHECK-LABEL: @is_zero_slice
+#[no_mangle]
+pub fn is_zero_slice(data: &[u8; 4]) -> bool {
+    // CHECK: start:
+    // CHECK-NEXT: %{{.+}} = getelementptr {{.+}}
+    // CHECK-NEXT: %[[BCMP:.+]] = tail call i32 @{{bcmp|memcmp}}({{.+}})
+    // CHECK-NEXT: %[[EQ:.+]] = icmp eq i32 %[[BCMP]], 0
+    // CHECK-NEXT: ret i1 %[[EQ]]
+    *data == [0; 4]
+}