@@ -2297,12 +2297,22 @@ pub(crate) use assert_unsafe_precondition;
2297
2297
2298
2298
/// Checks whether `ptr` is properly aligned with respect to
2299
2299
/// `align_of::<T>()`.
2300
+ #[ inline]
2300
2301
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 )
2302
2311
}
2303
2312
2304
2313
/// Checks whether an allocation of `len` instances of `T` exceeds
2305
2314
/// the maximum allowed allocation size.
2315
+ #[ inline]
2306
2316
pub ( crate ) fn is_valid_allocation_size < T > ( len : usize ) -> bool {
2307
2317
let max_len = const {
2308
2318
let size = crate :: mem:: size_of :: < T > ( ) ;
@@ -2313,6 +2323,7 @@ pub(crate) fn is_valid_allocation_size<T>(len: usize) -> bool {
2313
2323
2314
2324
/// Checks whether the regions of memory starting at `src` and `dst` of size
2315
2325
/// `count * size_of::<T>()` do *not* overlap.
2326
+ #[ inline]
2316
2327
pub ( crate ) fn is_nonoverlapping < T > ( src : * const T , dst : * const T , count : usize ) -> bool {
2317
2328
let src_usize = src. addr ( ) ;
2318
2329
let dst_usize = dst. addr ( ) ;
0 commit comments