Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non memcmp slice comparison optimization #113654

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 40 additions & 16 deletions library/core/src/slice/cmp.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//! Comparison traits for `[T]`.

use crate::cmp::{self, BytewiseEq, Ordering};
use crate::cmp::{self, Ordering};
use crate::ffi;
use crate::mem;

use super::from_raw_parts;
use super::memchr;
Expand Down Expand Up @@ -70,25 +69,50 @@ where
return false;
}

self.iter().zip(other.iter()).all(|(x, y)| x == y)
let mut i = self.len();
let mut ptr_self = self.as_ptr();
let mut ptr_other = other.as_ptr();
// SAFETY:
// This is sound because:
// - self.len == other.len
// - self.len <= isize::MAX
// so the two pointers will not overflow,
// will remain in bounds of the slice,
// and dereferencing them is sound.
unsafe {
while (i > 0) && (*ptr_self == *ptr_other) {
i -= 1;
ptr_self = ptr_self.add(1);
ptr_other = ptr_other.add(1);
}
}
i == 0
}
}

// Use memcmp for bytewise equality when the types allow
impl<A, B> SlicePartialEq<B> for [A]
where
A: BytewiseEq<B>,
{
fn equal(&self, other: &[B]) -> bool {
if self.len() != other.len() {
return false;
}
// and `memcmp` is not equivalent to our generic case
#[cfg(not(target_env = "musl"))]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that cfg needs a clear comment (saying that musl's memcmp is not more optimized)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx for reviewing.
I'm planning on commenting more here and in pal.rs but I'm waiting to see how the debate on cfg in core goes.

mod bytewise_memcmp {
use super::{memcmp, SlicePartialEq};
use crate::cmp::BytewiseEq;
use crate::mem;

impl<A, B> SlicePartialEq<B> for [A]
where
A: BytewiseEq<B>,
{
fn equal(&self, other: &[B]) -> bool {
if self.len() != other.len() {
return false;
}

// SAFETY: `self` and `other` are references and are thus guaranteed to be valid.
// The two slices have been checked to have the same size above.
unsafe {
let size = mem::size_of_val(self);
memcmp(self.as_ptr() as *const u8, other.as_ptr() as *const u8, size) == 0
// SAFETY: `self` and `other` are references and are thus guaranteed to be valid.
// The two slices have been checked to have the same size above.
unsafe {
let size = mem::size_of_val(self);
memcmp(self.as_ptr() as *const u8, other.as_ptr() as *const u8, size) == 0
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/tools/tidy/src/pal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const EXCEPTION_PATHS: &[&str] = &[
"library/std/src/path.rs",
"library/std/src/sys_common", // Should only contain abstractions over platforms
"library/std/src/net/test.rs", // Utility helpers for tests
"library/core/src/slice/cmp.rs", // To comment better
];

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