Skip to content

Commit

Permalink
Auto merge of rust-lang#77771 - nagisa:revert-77023, r=Mark-Simulacrum
Browse files Browse the repository at this point in the history
Revert "Assume slice len is bounded by allocation size"

rust-lang#77023 (comment)
suggests that the original PR introduced a significant perf regression.

This reverts commit e44784b / rust-lang#77023.

cc `@HeroicKatora`
  • Loading branch information
bors committed Oct 10, 2020
2 parents cae8bc1 + 54a5608 commit 87b71ed
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 46 deletions.
1 change: 0 additions & 1 deletion library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@
#![feature(const_pin)]
#![feature(const_fn)]
#![feature(const_fn_union)]
#![feature(const_assume)]
#![cfg_attr(not(bootstrap), feature(const_impl_trait))]
#![feature(const_fn_floating_point_arithmetic)]
#![feature(const_fn_fn_ptr_basics)]
Expand Down
23 changes: 2 additions & 21 deletions library/core/src/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,6 @@ pub use index::check_range;
#[lang = "slice"]
#[cfg(not(test))]
impl<T> [T] {
/// The maximum, inclusive, length such that the slice is no larger than `isize::MAX` bytes.
/// This constant is used in `len` below.
const MAX_LEN_BOUND: usize = {
if mem::size_of::<T>() == 0 {
usize::MAX
} else {
isize::MAX as usize / mem::size_of::<T>()
}
};

/// Returns the number of elements in the slice.
///
/// # Examples
Expand All @@ -101,20 +91,11 @@ impl<T> [T] {
#[rustc_const_stable(feature = "const_slice_len", since = "1.32.0")]
#[inline]
// SAFETY: const sound because we transmute out the length field as a usize (which it must be)
#[allow_internal_unstable(const_fn_union, const_assume)]
#[allow_internal_unstable(const_fn_union)]
pub const fn len(&self) -> usize {
// SAFETY: this is safe because `&[T]` and `FatPtr<T>` have the same layout.
// Only `std` can make this guarantee.
let raw_len = unsafe { crate::ptr::Repr { rust: self }.raw.len };

// SAFETY: this assume asserts that `raw_len * size_of::<T>() <= isize::MAX`. All
// references must point to one allocation with size at most isize::MAX. Note that we the
// multiplication could appear to overflow until we have assumed the bound. This overflow
// would make additional values appear 'valid' and then `n > 1` the range of permissible
// length would no longer be the full or even a single range.
unsafe { crate::intrinsics::assume(raw_len <= Self::MAX_LEN_BOUND) };

raw_len
unsafe { crate::ptr::Repr { rust: self }.raw.len }
}

/// Returns `true` if the slice has a length of 0.
Expand Down
24 changes: 0 additions & 24 deletions src/test/codegen/len-is-bounded.rs

This file was deleted.

0 comments on commit 87b71ed

Please sign in to comment.