Skip to content
Open
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
12 changes: 10 additions & 2 deletions library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1649,7 +1649,11 @@ impl<T, A: Allocator> Vec<T, A> {
// * We only construct `&mut` references to `self.buf` through `&mut self` methods; borrow-
// check ensures that it is not possible to mutably alias `self.buf` within the
// returned lifetime.
unsafe { slice::from_raw_parts(self.as_ptr(), self.len) }
unsafe {
// normally this would use `slice::from_raw_parts`, but it's
// instantiated often enough that avoiding the UB check is worth it
&*core::intrinsics::aggregate_raw_ptr::<*const [T], _, _>(self.as_ptr(), self.len)
}
}

/// Extracts a mutable slice of the entire vector.
Expand Down Expand Up @@ -1681,7 +1685,11 @@ impl<T, A: Allocator> Vec<T, A> {
// * We only construct references to `self.buf` through `&self` and `&mut self` methods;
// borrow-check ensures that it is not possible to construct a reference to `self.buf`
// within the returned lifetime.
unsafe { slice::from_raw_parts_mut(self.as_mut_ptr(), self.len) }
unsafe {
// normally this would use `slice::from_raw_parts_mut`, but it's
// hot enough that avoiding the UB check is worth it
&mut *core::intrinsics::aggregate_raw_ptr::<*mut [T], _, _>(self.as_mut_ptr(), self.len)
}
}

/// Returns a raw pointer to the vector's buffer, or a dangling raw pointer
Expand Down
24 changes: 16 additions & 8 deletions library/core/src/slice/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@ impl<T, U> const PartialEq<[U]> for [T]
where
T: [const] PartialEq<U>,
{
#[inline]
fn eq(&self, other: &[U]) -> bool {
SlicePartialEq::equal(self, other)
}

fn ne(&self, other: &[U]) -> bool {
SlicePartialEq::not_equal(self, other)
}
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down Expand Up @@ -99,10 +96,6 @@ impl<T: PartialOrd> PartialOrd for [T] {
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
const trait SlicePartialEq<B> {
fn equal(&self, other: &[B]) -> bool;

fn not_equal(&self, other: &[B]) -> bool {
!self.equal(other)
}
Comment on lines -103 to -105
Copy link
Member Author

Choose a reason for hiding this comment

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

Annot: nothing actually overrode this anywhere, so removed it in favour of the usual PartialEq::ne.

}

// Generic slice equality
Expand All @@ -111,6 +104,11 @@ impl<A, B> const SlicePartialEq<B> for [A]
where
A: [const] PartialEq<B>,
{
// It's not worth trying to inline the loops underneath here *in MIR*,
// and preventing it encourages more useful inlining upstream,
// such as in `<str as PartialEq>::eq`.
// The codegen backend can still inline it later if needed.
#[rustc_no_mir_inline]
default fn equal(&self, other: &[B]) -> bool {
if self.len() != other.len() {
return false;
Expand Down Expand Up @@ -140,6 +138,16 @@ impl<A, B> const SlicePartialEq<B> for [A]
where
A: [const] BytewiseEq<B>,
{
// This is usually a pretty good backend inlining candidate because the
// intrinsic tends to just be `memcmp`. However, as of 2025-12 letting
// MIR inline this makes reuse worse because it means that, for example,
// `String::eq` doesn't inline, whereas by keeping this from inling all
// the wrappers until the call to this disappear. If the heuristics have
// changed and this is no longer fruitful, though, please do remove it.
// In the mean time, it's fine to not inline it in MIR because the backend
// will still inline it if it things it's important to do so.
#[rustc_no_mir_inline]
#[inline]
fn equal(&self, other: &[B]) -> bool {
if self.len() != other.len() {
return false;
Expand Down
Loading
Loading