about summary refs log tree commit diff
path: root/library/core/src/array
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2021-12-17 19:17:29 +0000
committerbors <bors@rust-lang.org>2021-12-17 19:17:29 +0000
commit7abab1efb21617ba6845fa86328dffa16cfcf1dc (patch)
treeab4bcc70374e8b52eadd3280497a97b5a3598c0c /library/core/src/array
parent34dc0d0f249a33fda18755991b4e73ad786d2b19 (diff)
parenta0b96902e4c622d40c7186fc0c7ba13efc1fc912 (diff)
downloadrust-7abab1efb21617ba6845fa86328dffa16cfcf1dc.tar.gz
rust-7abab1efb21617ba6845fa86328dffa16cfcf1dc.zip
Auto merge of #91838 - scottmcm:array-slice-eq-via-arrays-not-slices, r=dtolnay
Do array-slice equality via array equality, rather than always via slices

~~Draft because it needs a rebase after #91766 eventually gets through bors.~~

This enables the optimizations from #85828 to be used for array-to-slice comparisons too, not just array-to-array.

For example, <https://play.rust-lang.org/?version=nightly&mode=release&edition=2021&gist=5f9ba69b3d5825a782f897c830d3a6aa>
```rust
pub fn demo(x: &[u8], y: [u8; 4]) -> bool {
    *x == y
}
```
Currently writes the array to stack for no reason:
```nasm
	sub	rsp, 4
	mov	dword ptr [rsp], edx
	cmp	rsi, 4
	jne	.LBB0_1
	mov	eax, dword ptr [rdi]
	cmp	eax, dword ptr [rsp]
	sete	al
	add	rsp, 4
	ret

.LBB0_1:
	xor	eax, eax
	add	rsp, 4
	ret
```
Whereas with the change in this PR it just compares it directly:
```nasm
	cmp	rsi, 4
	jne	.LBB1_1
	cmp	dword ptr [rdi], edx
	sete	al
	ret

.LBB1_1:
	xor	eax, eax
	ret
```
Diffstat (limited to 'library/core/src/array')
-rw-r--r--library/core/src/array/equality.rs41
1 files changed, 29 insertions, 12 deletions
diff --git a/library/core/src/array/equality.rs b/library/core/src/array/equality.rs
index 25e056501be..33f7f494e9d 100644
--- a/library/core/src/array/equality.rs
+++ b/library/core/src/array/equality.rs
@@ -1,3 +1,4 @@
+use crate::convert::TryInto;
 use crate::num::{NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize};
 use crate::num::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize};
 
@@ -23,11 +24,19 @@ where
 {
     #[inline]
     fn eq(&self, other: &[B]) -> bool {
-        self[..] == other[..]
+        let b: Result<&[B; N], _> = other.try_into();
+        match b {
+            Ok(b) => *self == *b,
+            Err(_) => false,
+        }
     }
     #[inline]
     fn ne(&self, other: &[B]) -> bool {
-        self[..] != other[..]
+        let b: Result<&[B; N], _> = other.try_into();
+        match b {
+            Ok(b) => *self != *b,
+            Err(_) => true,
+        }
     }
 }
 
@@ -38,11 +47,19 @@ where
 {
     #[inline]
     fn eq(&self, other: &[A; N]) -> bool {
-        self[..] == other[..]
+        let b: Result<&[B; N], _> = self.try_into();
+        match b {
+            Ok(b) => *b == *other,
+            Err(_) => false,
+        }
     }
     #[inline]
     fn ne(&self, other: &[A; N]) -> bool {
-        self[..] != other[..]
+        let b: Result<&[B; N], _> = self.try_into();
+        match b {
+            Ok(b) => *b != *other,
+            Err(_) => true,
+        }
     }
 }
 
@@ -53,11 +70,11 @@ where
 {
     #[inline]
     fn eq(&self, other: &&[B]) -> bool {
-        self[..] == other[..]
+        *self == **other
     }
     #[inline]
     fn ne(&self, other: &&[B]) -> bool {
-        self[..] != other[..]
+        *self != **other
     }
 }
 
@@ -68,11 +85,11 @@ where
 {
     #[inline]
     fn eq(&self, other: &[A; N]) -> bool {
-        self[..] == other[..]
+        **self == *other
     }
     #[inline]
     fn ne(&self, other: &[A; N]) -> bool {
-        self[..] != other[..]
+        **self != *other
     }
 }
 
@@ -83,11 +100,11 @@ where
 {
     #[inline]
     fn eq(&self, other: &&mut [B]) -> bool {
-        self[..] == other[..]
+        *self == **other
     }
     #[inline]
     fn ne(&self, other: &&mut [B]) -> bool {
-        self[..] != other[..]
+        *self != **other
     }
 }
 
@@ -98,11 +115,11 @@ where
 {
     #[inline]
     fn eq(&self, other: &[A; N]) -> bool {
-        self[..] == other[..]
+        **self == *other
     }
     #[inline]
     fn ne(&self, other: &[A; N]) -> bool {
-        self[..] != other[..]
+        **self != *other
     }
 }