Skip to content

Commit f566552

Browse files
committed
Do not use memcmp on musl
Musl implementation is equivalent to our general case implementation. Not using it: - Allows for better optimization leveraging alignment information - Prevents a `call` - Likely favors inlining and further optimization
1 parent be64090 commit f566552

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

library/core/src/slice/cmp.rs

+22-15
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
//! Comparison traits for `[T]`.
22
3-
use crate::cmp::{self, BytewiseEq, Ordering};
3+
use crate::cmp::{self, Ordering};
44
use crate::ffi;
5-
use crate::mem;
65

76
use super::from_raw_parts;
87
use super::memchr;
@@ -92,20 +91,28 @@ where
9291
}
9392

9493
// Use memcmp for bytewise equality when the types allow
95-
impl<A, B> SlicePartialEq<B> for [A]
96-
where
97-
A: BytewiseEq<B>,
98-
{
99-
fn equal(&self, other: &[B]) -> bool {
100-
if self.len() != other.len() {
101-
return false;
102-
}
94+
// and `memcmp` is not equivalent to our generic case
95+
#[cfg(not(target_env = "musl"))]
96+
mod bytewise_memcmp {
97+
use super::{memcmp, SlicePartialEq};
98+
use crate::cmp::BytewiseEq;
99+
use crate::mem;
100+
101+
impl<A, B> SlicePartialEq<B> for [A]
102+
where
103+
A: BytewiseEq<B>,
104+
{
105+
fn equal(&self, other: &[B]) -> bool {
106+
if self.len() != other.len() {
107+
return false;
108+
}
103109

104-
// SAFETY: `self` and `other` are references and are thus guaranteed to be valid.
105-
// The two slices have been checked to have the same size above.
106-
unsafe {
107-
let size = mem::size_of_val(self);
108-
memcmp(self.as_ptr() as *const u8, other.as_ptr() as *const u8, size) == 0
110+
// SAFETY: `self` and `other` are references and are thus guaranteed to be valid.
111+
// The two slices have been checked to have the same size above.
112+
unsafe {
113+
let size = mem::size_of_val(self);
114+
memcmp(self.as_ptr() as *const u8, other.as_ptr() as *const u8, size) == 0
115+
}
109116
}
110117
}
111118
}

src/tools/tidy/src/pal.rs

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ const EXCEPTION_PATHS: &[&str] = &[
5555
"library/std/src/path.rs",
5656
"library/std/src/sys_common", // Should only contain abstractions over platforms
5757
"library/std/src/net/test.rs", // Utility helpers for tests
58+
"library/core/src/slice/cmp.rs", // To comment better
5859
];
5960

6061
pub fn check(path: &Path, bad: &mut bool) {

0 commit comments

Comments
 (0)