|
60 | 60 | return false;
|
61 | 61 | }
|
62 | 62 |
|
63 |
| - self.iter().zip(other.iter()).all(|(x, y)| x == y) |
| 63 | + if self.len() == 0 { |
| 64 | + return true; |
| 65 | + } |
| 66 | + |
| 67 | + // ZSTs have no identity and slices don't guarantee which addresses-to-ZSTs they produce |
| 68 | + // so we only need to compare them once to determine the behavior of the PartialEq impl |
| 69 | + if const { mem::size_of::<A>() == 0 && mem::size_of::<B>() == 0 } { |
| 70 | + // zero-length slices are always equal |
| 71 | + // SAFETY: A and B are ZSTs so it's ok to conjure them out of thin air |
| 72 | + return unsafe { mem::zeroed::<A>() == mem::zeroed::<B>() }; |
| 73 | + } |
| 74 | + |
| 75 | + const UNROLL: usize = 4; |
| 76 | + let mut i = 0; |
| 77 | + let mut is_eq = true; |
| 78 | + |
| 79 | + let a = self.as_ptr(); |
| 80 | + let b = other.as_ptr(); |
| 81 | + let len = self.len(); |
| 82 | + |
| 83 | + // compare items 1 by 1 in case comparisons are expensive. at least one item, then |
| 84 | + // until the remainder is a multiple of UNROLL |
| 85 | + loop { |
| 86 | + // SAFETY: slices are of the same length and loop conditions ensure indexes are in bounds |
| 87 | + unsafe { |
| 88 | + is_eq = is_eq & PartialEq::eq(&*a.add(i), &*b.add(i)); |
| 89 | + i = i.unchecked_add(1); |
| 90 | + } |
| 91 | + |
| 92 | + if !is_eq || i == len || (len - i) % UNROLL == 0 { |
| 93 | + break; |
| 94 | + } |
| 95 | + } |
| 96 | + while is_eq && i + UNROLL <= len { |
| 97 | + // SAFETY: slices are of the same length and loop conditions ensure indexes are in bounds |
| 98 | + unsafe { |
| 99 | + is_eq = is_eq & PartialEq::eq(&*a.add(i), &*b.add(i)); |
| 100 | + is_eq = is_eq & PartialEq::eq(&*a.add(i + 1), &*b.add(i + 1)); |
| 101 | + is_eq = is_eq & PartialEq::eq(&*a.add(i + 2), &*b.add(i + 2)); |
| 102 | + is_eq = is_eq & PartialEq::eq(&*a.add(i + 3), &*b.add(i + 3)); |
| 103 | + i = i.unchecked_add(UNROLL); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + is_eq |
64 | 108 | }
|
65 | 109 | }
|
66 | 110 |
|
|
0 commit comments