Skip to content

Commit 99f6206

Browse files
committed
Auto merge of #21894 - dotdash:assume_rc, r=alexcrichton
This is half of what @Aatch implemented in #21418. The non-null assumption is later canonicalized to !nonnull metadata and doesn't cause any slowdowns (in fact the build is slightly faster with this change). I left out the other half of #21418 because that still causes a ~16% increase in compile times (30m -> 35m).
2 parents 189930f + 40b6e34 commit 99f6206

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

src/liballoc/rc.rs

+25-2
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ use core::option::Option::{Some, None};
160160
use core::ptr::{self, PtrExt};
161161
use core::result::Result;
162162
use core::result::Result::{Ok, Err};
163+
use core::intrinsics::assume;
163164

164165
use heap::deallocate;
165166

@@ -769,12 +770,34 @@ trait RcBoxPtr<T> {
769770

770771
impl<T> RcBoxPtr<T> for Rc<T> {
771772
#[inline(always)]
772-
fn inner(&self) -> &RcBox<T> { unsafe { &(**self._ptr) } }
773+
fn inner(&self) -> &RcBox<T> {
774+
unsafe {
775+
// Safe to assume this here, as if it weren't true, we'd be breaking
776+
// the contract anyway.
777+
// This allows the null check to be elided in the destructor if we
778+
// manipulated the reference count in the same function.
779+
if cfg!(not(stage0)) { // NOTE remove cfg after next snapshot
780+
assume(!self._ptr.is_null());
781+
}
782+
&(**self._ptr)
783+
}
784+
}
773785
}
774786

775787
impl<T> RcBoxPtr<T> for Weak<T> {
776788
#[inline(always)]
777-
fn inner(&self) -> &RcBox<T> { unsafe { &(**self._ptr) } }
789+
fn inner(&self) -> &RcBox<T> {
790+
unsafe {
791+
// Safe to assume this here, as if it weren't true, we'd be breaking
792+
// the contract anyway.
793+
// This allows the null check to be elided in the destructor if we
794+
// manipulated the reference count in the same function.
795+
if cfg!(not(stage0)) { // NOTE remove cfg after next snapshot
796+
assume(!self._ptr.is_null());
797+
}
798+
&(**self._ptr)
799+
}
800+
}
778801
}
779802

780803
#[cfg(test)]

0 commit comments

Comments
 (0)