about summary refs log tree commit diff
diff options
context:
space:
mode:
authorArthur Carcano <arthur.carcano@ocamlpro.com>2023-07-13 12:46:14 +0200
committerArthur Carcano <arthur.carcano@ocamlpro.com>2024-01-08 16:36:48 +0100
commit5b041abc8cfae5076e9187e127945de113c92fed (patch)
treefa7b1cc9f65c799a1b64f77d53de43e2668c259f
parent347452e7e378d93e7ba29af05f022a5445e1f459 (diff)
downloadrust-5b041abc8cfae5076e9187e127945de113c92fed.tar.gz
rust-5b041abc8cfae5076e9187e127945de113c92fed.zip
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.
-rw-r--r--library/core/src/slice/cmp.rs12
1 files changed, 11 insertions, 1 deletions
diff --git a/library/core/src/slice/cmp.rs b/library/core/src/slice/cmp.rs
index 075347b80d0..8a8d634c007 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
     }
 }