Take advantage of known-valid-align in layout.rs
This commit is contained in:
parent
29554c0a12
commit
079d3eb22f
2 changed files with 37 additions and 15 deletions
|
@ -1,3 +1,9 @@
|
||||||
|
// Seemingly inconsequential code changes to this file can lead to measurable
|
||||||
|
// performance impact on compilation times, due at least in part to the fact
|
||||||
|
// that the layout code gets called from many instantiations of the various
|
||||||
|
// collections, resulting in having to optimize down excess IR multiple times.
|
||||||
|
// Your performance intuition is useless. Run perf.
|
||||||
|
|
||||||
use crate::cmp;
|
use crate::cmp;
|
||||||
use crate::fmt;
|
use crate::fmt;
|
||||||
use crate::mem::{self, ValidAlign};
|
use crate::mem::{self, ValidAlign};
|
||||||
|
@ -85,6 +91,17 @@ impl Layout {
|
||||||
unsafe { Ok(Layout::from_size_align_unchecked(size, align)) }
|
unsafe { Ok(Layout::from_size_align_unchecked(size, align)) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Internal helper constructor to skip revalidating alignment validity.
|
||||||
|
#[inline]
|
||||||
|
const fn from_size_valid_align(size: usize, align: ValidAlign) -> Result<Self, LayoutError> {
|
||||||
|
// See above for the correctness of this check.
|
||||||
|
if size > isize::MAX as usize - (align.as_nonzero().get() - 1) {
|
||||||
|
return Err(LayoutError);
|
||||||
|
}
|
||||||
|
// SAFTEY: as above, this check is sufficient.
|
||||||
|
Ok(Layout { size, align })
|
||||||
|
}
|
||||||
|
|
||||||
/// Creates a layout, bypassing all checks.
|
/// Creates a layout, bypassing all checks.
|
||||||
///
|
///
|
||||||
/// # Safety
|
/// # Safety
|
||||||
|
@ -126,10 +143,9 @@ impl Layout {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub const fn new<T>() -> Self {
|
pub const fn new<T>() -> Self {
|
||||||
let (size, align) = size_align::<T>();
|
let (size, align) = size_align::<T>();
|
||||||
// SAFETY: the align is guaranteed by Rust to be a power of two and
|
// SAFETY: if the type is instantiated, rustc already ensures that its
|
||||||
// the size+align combo is guaranteed to fit in our address space. As a
|
// layout is valid. Use the unchecked constructor to avoid inserting a
|
||||||
// result use the unchecked constructor here to avoid inserting code
|
// panicking codepath that needs to be optimized out.
|
||||||
// that panics if it isn't optimized well enough.
|
|
||||||
unsafe { Layout::from_size_align_unchecked(size, align) }
|
unsafe { Layout::from_size_align_unchecked(size, align) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -141,7 +157,6 @@ impl Layout {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn for_value<T: ?Sized>(t: &T) -> Self {
|
pub fn for_value<T: ?Sized>(t: &T) -> Self {
|
||||||
let (size, align) = (mem::size_of_val(t), mem::align_of_val(t));
|
let (size, align) = (mem::size_of_val(t), mem::align_of_val(t));
|
||||||
debug_assert!(Layout::from_size_align(size, align).is_ok());
|
|
||||||
// SAFETY: see rationale in `new` for why this is using the unsafe variant
|
// SAFETY: see rationale in `new` for why this is using the unsafe variant
|
||||||
unsafe { Layout::from_size_align_unchecked(size, align) }
|
unsafe { Layout::from_size_align_unchecked(size, align) }
|
||||||
}
|
}
|
||||||
|
@ -176,7 +191,6 @@ impl Layout {
|
||||||
pub unsafe fn for_value_raw<T: ?Sized>(t: *const T) -> Self {
|
pub unsafe fn for_value_raw<T: ?Sized>(t: *const T) -> Self {
|
||||||
// SAFETY: we pass along the prerequisites of these functions to the caller
|
// SAFETY: we pass along the prerequisites of these functions to the caller
|
||||||
let (size, align) = unsafe { (mem::size_of_val_raw(t), mem::align_of_val_raw(t)) };
|
let (size, align) = unsafe { (mem::size_of_val_raw(t), mem::align_of_val_raw(t)) };
|
||||||
debug_assert!(Layout::from_size_align(size, align).is_ok());
|
|
||||||
// SAFETY: see rationale in `new` for why this is using the unsafe variant
|
// SAFETY: see rationale in `new` for why this is using the unsafe variant
|
||||||
unsafe { Layout::from_size_align_unchecked(size, align) }
|
unsafe { Layout::from_size_align_unchecked(size, align) }
|
||||||
}
|
}
|
||||||
|
@ -280,8 +294,7 @@ impl Layout {
|
||||||
// > less than or equal to `isize::MAX`)
|
// > less than or equal to `isize::MAX`)
|
||||||
let new_size = self.size() + pad;
|
let new_size = self.size() + pad;
|
||||||
|
|
||||||
// SAFETY: self.align is already known to be valid and new_size has been
|
// SAFETY: padded size is guaranteed to not exceed `isize::MAX`.
|
||||||
// padded already.
|
|
||||||
unsafe { Layout::from_size_align_unchecked(new_size, self.align()) }
|
unsafe { Layout::from_size_align_unchecked(new_size, self.align()) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -304,7 +317,7 @@ impl Layout {
|
||||||
let alloc_size = padded_size.checked_mul(n).ok_or(LayoutError)?;
|
let alloc_size = padded_size.checked_mul(n).ok_or(LayoutError)?;
|
||||||
|
|
||||||
// The safe constructor is called here to enforce the isize size limit.
|
// The safe constructor is called here to enforce the isize size limit.
|
||||||
Layout::from_size_align(alloc_size, self.align()).map(|layout| (layout, padded_size))
|
Layout::from_size_valid_align(alloc_size, self.align).map(|layout| (layout, padded_size))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a layout describing the record for `self` followed by
|
/// Creates a layout describing the record for `self` followed by
|
||||||
|
@ -355,14 +368,14 @@ impl Layout {
|
||||||
#[stable(feature = "alloc_layout_manipulation", since = "1.44.0")]
|
#[stable(feature = "alloc_layout_manipulation", since = "1.44.0")]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn extend(&self, next: Self) -> Result<(Self, usize), LayoutError> {
|
pub fn extend(&self, next: Self) -> Result<(Self, usize), LayoutError> {
|
||||||
let new_align = cmp::max(self.align(), next.align());
|
let new_align = cmp::max(self.align, next.align);
|
||||||
let pad = self.padding_needed_for(next.align());
|
let pad = self.padding_needed_for(next.align());
|
||||||
|
|
||||||
let offset = self.size().checked_add(pad).ok_or(LayoutError)?;
|
let offset = self.size().checked_add(pad).ok_or(LayoutError)?;
|
||||||
let new_size = offset.checked_add(next.size()).ok_or(LayoutError)?;
|
let new_size = offset.checked_add(next.size()).ok_or(LayoutError)?;
|
||||||
|
|
||||||
// The safe constructor is called here to enforce the isize size limit.
|
// The safe constructor is called here to enforce the isize size limit.
|
||||||
let layout = Layout::from_size_align(new_size, new_align)?;
|
let layout = Layout::from_size_valid_align(new_size, new_align)?;
|
||||||
Ok((layout, offset))
|
Ok((layout, offset))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -383,7 +396,7 @@ impl Layout {
|
||||||
pub fn repeat_packed(&self, n: usize) -> Result<Self, LayoutError> {
|
pub fn repeat_packed(&self, n: usize) -> Result<Self, LayoutError> {
|
||||||
let size = self.size().checked_mul(n).ok_or(LayoutError)?;
|
let size = self.size().checked_mul(n).ok_or(LayoutError)?;
|
||||||
// The safe constructor is called here to enforce the isize size limit.
|
// The safe constructor is called here to enforce the isize size limit.
|
||||||
Layout::from_size_align(size, self.align())
|
Layout::from_size_valid_align(size, self.align)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a layout describing the record for `self` followed by
|
/// Creates a layout describing the record for `self` followed by
|
||||||
|
@ -397,7 +410,7 @@ impl Layout {
|
||||||
pub fn extend_packed(&self, next: Self) -> Result<Self, LayoutError> {
|
pub fn extend_packed(&self, next: Self) -> Result<Self, LayoutError> {
|
||||||
let new_size = self.size().checked_add(next.size()).ok_or(LayoutError)?;
|
let new_size = self.size().checked_add(next.size()).ok_or(LayoutError)?;
|
||||||
// The safe constructor is called here to enforce the isize size limit.
|
// The safe constructor is called here to enforce the isize size limit.
|
||||||
Layout::from_size_align(new_size, self.align())
|
Layout::from_size_valid_align(new_size, self.align)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a layout describing the record for a `[T; n]`.
|
/// Creates a layout describing the record for a `[T; n]`.
|
||||||
|
@ -408,7 +421,7 @@ impl Layout {
|
||||||
pub fn array<T>(n: usize) -> Result<Self, LayoutError> {
|
pub fn array<T>(n: usize) -> Result<Self, LayoutError> {
|
||||||
let array_size = mem::size_of::<T>().checked_mul(n).ok_or(LayoutError)?;
|
let array_size = mem::size_of::<T>().checked_mul(n).ok_or(LayoutError)?;
|
||||||
// The safe constructor is called here to enforce the isize size limit.
|
// The safe constructor is called here to enforce the isize size limit.
|
||||||
Layout::from_size_align(array_size, mem::align_of::<T>())
|
Layout::from_size_valid_align(array_size, ValidAlign::of::<T>())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use crate::convert::TryFrom;
|
use crate::convert::TryFrom;
|
||||||
|
use crate::intrinsics::assert_unsafe_precondition;
|
||||||
use crate::num::NonZeroUsize;
|
use crate::num::NonZeroUsize;
|
||||||
use crate::{cmp, fmt, hash, mem, num};
|
use crate::{cmp, fmt, hash, mem, num};
|
||||||
|
|
||||||
|
@ -26,7 +27,8 @@ impl ValidAlign {
|
||||||
/// It must *not* be zero.
|
/// It must *not* be zero.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) const unsafe fn new_unchecked(align: usize) -> Self {
|
pub(crate) const unsafe fn new_unchecked(align: usize) -> Self {
|
||||||
debug_assert!(align.is_power_of_two());
|
// SAFETY: Precondition passed to the caller.
|
||||||
|
unsafe { assert_unsafe_precondition!(align.is_power_of_two()) };
|
||||||
|
|
||||||
// SAFETY: By precondition, this must be a power of two, and
|
// SAFETY: By precondition, this must be a power of two, and
|
||||||
// our variants encompass all possible powers of two.
|
// our variants encompass all possible powers of two.
|
||||||
|
@ -46,6 +48,13 @@ impl ValidAlign {
|
||||||
pub(crate) fn log2(self) -> u32 {
|
pub(crate) fn log2(self) -> u32 {
|
||||||
self.as_nonzero().trailing_zeros()
|
self.as_nonzero().trailing_zeros()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the alignment for a type.
|
||||||
|
#[inline]
|
||||||
|
pub(crate) fn of<T>() -> Self {
|
||||||
|
// SAFETY: rustc ensures that type alignment is always a power of two.
|
||||||
|
unsafe { ValidAlign::new_unchecked(mem::align_of::<T>()) }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Debug for ValidAlign {
|
impl fmt::Debug for ValidAlign {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue