Skip to content

Commit 4f9a2bd

Browse files
committed
Tweak assert_unsafe_precondition helpers so they inline in MIR
1 parent c2ff8ad commit 4f9a2bd

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

library/core/src/intrinsics.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -2297,12 +2297,22 @@ pub(crate) use assert_unsafe_precondition;
22972297

22982298
/// Checks whether `ptr` is properly aligned with respect to
22992299
/// `align_of::<T>()`.
2300+
#[inline]
23002301
pub(crate) fn is_aligned_and_not_null<T>(ptr: *const T) -> bool {
2301-
!ptr.is_null() && ptr.is_aligned()
2302+
// A reasonable implementation of this would be
2303+
// !ptr.is_null() && ptr.is_aligned()
2304+
// However that implementation is based on many layers of abstraction, and results in a lot
2305+
// more MIR being generated, which matters for this function especially because when debug
2306+
// assertions are enabled it is called in very many places. This simpler implementation seems
2307+
// to be worth 0-5% on debug builds.
2308+
let addr = ptr.addr();
2309+
let mask = const { crate::mem::align_of::<T>() - 1 };
2310+
(addr != 0) && (addr & mask == 0)
23022311
}
23032312

23042313
/// Checks whether an allocation of `len` instances of `T` exceeds
23052314
/// the maximum allowed allocation size.
2315+
#[inline]
23062316
pub(crate) fn is_valid_allocation_size<T>(len: usize) -> bool {
23072317
let max_len = const {
23082318
let size = crate::mem::size_of::<T>();
@@ -2313,6 +2323,7 @@ pub(crate) fn is_valid_allocation_size<T>(len: usize) -> bool {
23132323

23142324
/// Checks whether the regions of memory starting at `src` and `dst` of size
23152325
/// `count * size_of::<T>()` do *not* overlap.
2326+
#[inline]
23162327
pub(crate) fn is_nonoverlapping<T>(src: *const T, dst: *const T, count: usize) -> bool {
23172328
let src_usize = src.addr();
23182329
let dst_usize = dst.addr();

0 commit comments

Comments
 (0)