Skip to content

Commit c12f891

Browse files
committed
Auto merge of rust-lang#116846 - krtab:slice_compare_no_memcmp_opt, r=<try>
A more efficient slice comparison implementation for T: !BytewiseEq (This is a follow up PR on rust-lang#113654) This PR changes the implementation for `[T]` slice comparison when `T: !BytewiseEq`. The previous implementation using zip was not optimized properly by the compiler, which didn't leverage the fact that both length were equal. Performance improvements are for example 20% when testing that `[Some(0_u64); 4096].as_slice() == [Some(0_u64); 4096].as_slice()`.
2 parents 8d39ec1 + a70613b commit c12f891

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

library/core/src/slice/cmp.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,14 @@ where
6060
return false;
6161
}
6262

63-
self.iter().zip(other.iter()).all(|(x, y)| x == y)
63+
for idx in 0..self.len() {
64+
// bound checks are optimized away
65+
if self[idx] != other[idx] {
66+
return false;
67+
}
68+
}
69+
70+
true
6471
}
6572
}
6673

0 commit comments

Comments
 (0)