From 5b041abc8cfae5076e9187e127945de113c92fed Mon Sep 17 00:00:00 2001 From: Arthur Carcano Date: Thu, 13 Jul 2023 12:46:14 +0200 Subject: [PATCH] A more efficient slice comparison implementation for T: !BytewiseEq The previous implementation was not optimized properly by the compiler, which didn't leverage the fact that both length were equal. --- library/core/src/slice/cmp.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/library/core/src/slice/cmp.rs b/library/core/src/slice/cmp.rs index 075347b80d031..8a8d634c0072a 100644 --- a/library/core/src/slice/cmp.rs +++ b/library/core/src/slice/cmp.rs @@ -60,7 +60,17 @@ where return false; } - self.iter().zip(other.iter()).all(|(x, y)| x == y) + // Implemented as explicit indexing rather + // than zipped iterators for performance reasons. + // See PR https://github.com/rust-lang/rust/pull/116846 + for idx in 0..self.len() { + // bound checks are optimized away + if self[idx] != other[idx] { + return false; + } + } + + true } }