about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-01-09 20:52:34 +0000
committerbors <bors@rust-lang.org>2024-01-09 20:52:34 +0000
commit190f4c96116a3b59b7de4881cfec544be0246d84 (patch)
tree3f25287a0c8a8d2c3c26fdbb3a2604b000a3e00b
parentae9d24de80b00b4158d1a29a212a6b02aeda0e75 (diff)
parent5b041abc8cfae5076e9187e127945de113c92fed (diff)
downloadrust-190f4c96116a3b59b7de4881cfec544be0246d84.tar.gz
rust-190f4c96116a3b59b7de4881cfec544be0246d84.zip
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()`.
-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
     }
 }