Skip to content

Commit 190f4c9

Browse files
committed
Auto merge of #116846 - krtab:slice_compare_no_memcmp_opt, r=the8472
A more efficient slice comparison implementation for T: !BytewiseEq (This is a follow up PR on #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 ae9d24d + 5b041ab commit 190f4c9

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

library/core/src/slice/cmp.rs

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

63-
self.iter().zip(other.iter()).all(|(x, y)| x == y)
63+
// Implemented as explicit indexing rather
64+
// than zipped iterators for performance reasons.
65+
// See PR https://github.com/rust-lang/rust/pull/116846
66+
for idx in 0..self.len() {
67+
// bound checks are optimized away
68+
if self[idx] != other[idx] {
69+
return false;
70+
}
71+
}
72+
73+
true
6474
}
6575
}
6676

0 commit comments

Comments
 (0)