Skip to content

Commit a0b9690

Browse files
committed
Do array-slice equality via arrays, rather than always via slices
This'll still go via slices eventually for large arrays, but this way slice comparisons to short arrays can use the same memcmp-avoidance tricks. Added some tests for all the combinations to make sure I didn't accidentally infinitely-recurse something.
1 parent 404c847 commit a0b9690

File tree

3 files changed

+89
-15
lines changed

3 files changed

+89
-15
lines changed

library/core/src/array/equality.rs

+29-12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::convert::TryInto;
12
use crate::num::{NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize};
23
use crate::num::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize};
34

@@ -23,11 +24,19 @@ where
2324
{
2425
#[inline]
2526
fn eq(&self, other: &[B]) -> bool {
26-
self[..] == other[..]
27+
let b: Result<&[B; N], _> = other.try_into();
28+
match b {
29+
Ok(b) => *self == *b,
30+
Err(_) => false,
31+
}
2732
}
2833
#[inline]
2934
fn ne(&self, other: &[B]) -> bool {
30-
self[..] != other[..]
35+
let b: Result<&[B; N], _> = other.try_into();
36+
match b {
37+
Ok(b) => *self != *b,
38+
Err(_) => true,
39+
}
3140
}
3241
}
3342

@@ -38,11 +47,19 @@ where
3847
{
3948
#[inline]
4049
fn eq(&self, other: &[A; N]) -> bool {
41-
self[..] == other[..]
50+
let b: Result<&[B; N], _> = self.try_into();
51+
match b {
52+
Ok(b) => *b == *other,
53+
Err(_) => false,
54+
}
4255
}
4356
#[inline]
4457
fn ne(&self, other: &[A; N]) -> bool {
45-
self[..] != other[..]
58+
let b: Result<&[B; N], _> = self.try_into();
59+
match b {
60+
Ok(b) => *b != *other,
61+
Err(_) => true,
62+
}
4663
}
4764
}
4865

@@ -53,11 +70,11 @@ where
5370
{
5471
#[inline]
5572
fn eq(&self, other: &&[B]) -> bool {
56-
self[..] == other[..]
73+
*self == **other
5774
}
5875
#[inline]
5976
fn ne(&self, other: &&[B]) -> bool {
60-
self[..] != other[..]
77+
*self != **other
6178
}
6279
}
6380

@@ -68,11 +85,11 @@ where
6885
{
6986
#[inline]
7087
fn eq(&self, other: &[A; N]) -> bool {
71-
self[..] == other[..]
88+
**self == *other
7289
}
7390
#[inline]
7491
fn ne(&self, other: &[A; N]) -> bool {
75-
self[..] != other[..]
92+
**self != *other
7693
}
7794
}
7895

@@ -83,11 +100,11 @@ where
83100
{
84101
#[inline]
85102
fn eq(&self, other: &&mut [B]) -> bool {
86-
self[..] == other[..]
103+
*self == **other
87104
}
88105
#[inline]
89106
fn ne(&self, other: &&mut [B]) -> bool {
90-
self[..] != other[..]
107+
*self != **other
91108
}
92109
}
93110

@@ -98,11 +115,11 @@ where
98115
{
99116
#[inline]
100117
fn eq(&self, other: &[A; N]) -> bool {
101-
self[..] == other[..]
118+
**self == *other
102119
}
103120
#[inline]
104121
fn ne(&self, other: &[A; N]) -> bool {
105-
self[..] != other[..]
122+
**self != *other
106123
}
107124
}
108125

library/core/tests/array.rs

+44
Original file line numberDiff line numberDiff line change
@@ -624,3 +624,47 @@ fn array_intoiter_advance_back_by() {
624624
assert_eq!(it.len(), 0);
625625
assert_eq!(counter.get(), 100);
626626
}
627+
628+
#[test]
629+
fn array_mixed_equality_integers() {
630+
let array3: [i32; 3] = [1, 2, 3];
631+
let array3b: [i32; 3] = [3, 2, 1];
632+
let array4: [i32; 4] = [1, 2, 3, 4];
633+
634+
let slice3: &[i32] = &{ array3 };
635+
let slice3b: &[i32] = &{ array3b };
636+
let slice4: &[i32] = &{ array4 };
637+
assert!(array3 == slice3);
638+
assert!(array3 != slice3b);
639+
assert!(array3 != slice4);
640+
assert!(slice3 == array3);
641+
assert!(slice3b != array3);
642+
assert!(slice4 != array3);
643+
644+
let mut3: &mut [i32] = &mut { array3 };
645+
let mut3b: &mut [i32] = &mut { array3b };
646+
let mut4: &mut [i32] = &mut { array4 };
647+
assert!(array3 == mut3);
648+
assert!(array3 != mut3b);
649+
assert!(array3 != mut4);
650+
assert!(mut3 == array3);
651+
assert!(mut3b != array3);
652+
assert!(mut4 != array3);
653+
}
654+
655+
#[test]
656+
fn array_mixed_equality_nans() {
657+
let array3: [f32; 3] = [1.0, std::f32::NAN, 3.0];
658+
659+
let slice3: &[f32] = &{ array3 };
660+
assert!(!(array3 == slice3));
661+
assert!(array3 != slice3);
662+
assert!(!(slice3 == array3));
663+
assert!(slice3 != array3);
664+
665+
let mut3: &mut [f32] = &mut { array3 };
666+
assert!(!(array3 == mut3));
667+
assert!(array3 != mut3);
668+
assert!(!(mut3 == array3));
669+
assert!(mut3 != array3);
670+
}

src/test/codegen/slice-ref-equality.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,31 @@
44

55
// #71602 reported a simple array comparison just generating a loop.
66
// This was originally fixed by ensuring it generates a single bcmp,
7-
// but we now generate it as a load instead. `is_zero_slice` was
7+
// but we now generate it as a load+icmp instead. `is_zero_slice` was
88
// tweaked to still test the case of comparison against a slice,
99
// and `is_zero_array` tests the new array-specific behaviour.
10+
// The optimization was then extended to short slice-to-array comparisons,
11+
// so the first test here now has a long slice to still get the bcmp.
1012

11-
// CHECK-LABEL: @is_zero_slice
13+
// CHECK-LABEL: @is_zero_slice_long
1214
#[no_mangle]
13-
pub fn is_zero_slice(data: &[u8; 4]) -> bool {
15+
pub fn is_zero_slice_long(data: &[u8; 456]) -> bool {
1416
// CHECK: :
1517
// CHECK-NEXT: %{{.+}} = getelementptr {{.+}}
1618
// CHECK-NEXT: %[[BCMP:.+]] = tail call i32 @{{bcmp|memcmp}}({{.+}})
1719
// CHECK-NEXT: %[[EQ:.+]] = icmp eq i32 %[[BCMP]], 0
1820
// CHECK-NEXT: ret i1 %[[EQ]]
21+
&data[..] == [0; 456]
22+
}
23+
24+
// CHECK-LABEL: @is_zero_slice_short
25+
#[no_mangle]
26+
pub fn is_zero_slice_short(data: &[u8; 4]) -> bool {
27+
// CHECK: :
28+
// CHECK-NEXT: %[[PTR:.+]] = bitcast [4 x i8]* {{.+}} to i32*
29+
// CHECK-NEXT: %[[LOAD:.+]] = load i32, i32* %[[PTR]], align 1
30+
// CHECK-NEXT: %[[EQ:.+]] = icmp eq i32 %[[LOAD]], 0
31+
// CHECK-NEXT: ret i1 %[[EQ]]
1932
&data[..] == [0; 4]
2033
}
2134

0 commit comments

Comments
 (0)