Skip to content

Commit b0ca5ff

Browse files
committed
Add a short-circuiting path to slice comparison
This adds a short-circuiting path to slice comparison using the fact that two slices of the same length and pointing at the same address are equal.
1 parent d8899c5 commit b0ca5ff

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

library/core/src/slice/cmp.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ where
8787
// SAFETY: `self` and `other` are references and are thus guaranteed to be valid.
8888
// The two slices have been checked to have the same size above.
8989
unsafe {
90-
let size = mem::size_of_val(self);
90+
let size =
91+
if self.as_ptr() == other.as_ptr().cast() { 0 } else { mem::size_of_val(self) };
9192
memcmp(self.as_ptr() as *const u8, other.as_ptr() as *const u8, size) == 0
9293
}
9394
}

0 commit comments

Comments
 (0)