Skip to content

Commit c581bee

Browse files
authored
Unrolled build for rust-lang#132031
Rollup merge of rust-lang#132031 - slanterns:rc_default, r=ibraheemdev Optimize `Rc<T>::default` The missing piece of rust-lang#131460. Also refactored `Arc<T>::default` by using a safe `NonNull::from(Box::leak(_))` to replace the unnecessarily unsafe call to `NonNull::new_unchecked(Box::into_raw(_))`. The remaining unsafety is coming from `[Rc|Arc]::from_inner`, which is safe from the construction of `[Rc|Arc]Inner`.
2 parents e1f3068 + 0a963ab commit c581bee

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

library/alloc/src/rc.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -2312,7 +2312,16 @@ impl<T: Default> Default for Rc<T> {
23122312
/// ```
23132313
#[inline]
23142314
fn default() -> Rc<T> {
2315-
Rc::new(Default::default())
2315+
unsafe {
2316+
Self::from_inner(
2317+
Box::leak(Box::write(Box::new_uninit(), RcInner {
2318+
strong: Cell::new(1),
2319+
weak: Cell::new(1),
2320+
value: T::default(),
2321+
}))
2322+
.into(),
2323+
)
2324+
}
23162325
}
23172326
}
23182327

library/alloc/src/sync.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -3447,13 +3447,16 @@ impl<T: Default> Default for Arc<T> {
34473447
/// assert_eq!(*x, 0);
34483448
/// ```
34493449
fn default() -> Arc<T> {
3450-
let x = Box::into_raw(Box::write(Box::new_uninit(), ArcInner {
3451-
strong: atomic::AtomicUsize::new(1),
3452-
weak: atomic::AtomicUsize::new(1),
3453-
data: T::default(),
3454-
}));
3455-
// SAFETY: `Box::into_raw` consumes the `Box` and never returns null
3456-
unsafe { Self::from_inner(NonNull::new_unchecked(x)) }
3450+
unsafe {
3451+
Self::from_inner(
3452+
Box::leak(Box::write(Box::new_uninit(), ArcInner {
3453+
strong: atomic::AtomicUsize::new(1),
3454+
weak: atomic::AtomicUsize::new(1),
3455+
data: T::default(),
3456+
}))
3457+
.into(),
3458+
)
3459+
}
34573460
}
34583461
}
34593462

tests/codegen/placement-new.rs

+12
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
//@ compile-flags: -O
2+
//@ compile-flags: -Zmerge-functions=disabled
23
#![crate_type = "lib"]
34

45
// Test to check that types with "complex" destructors, but trivial `Default` impls
56
// are constructed directly into the allocation in `Box::default` and `Arc::default`.
67

8+
use std::rc::Rc;
79
use std::sync::Arc;
810

911
// CHECK-LABEL: @box_default_inplace
@@ -16,6 +18,16 @@ pub fn box_default_inplace() -> Box<(String, String)> {
1618
Box::default()
1719
}
1820

21+
// CHECK-LABEL: @rc_default_inplace
22+
#[no_mangle]
23+
pub fn rc_default_inplace() -> Rc<(String, String)> {
24+
// CHECK-NOT: alloca
25+
// CHECK: [[RC:%.*]] = {{.*}}call {{.*}}__rust_alloc(
26+
// CHECK-NOT: call void @llvm.memcpy
27+
// CHECK: ret ptr [[RC]]
28+
Rc::default()
29+
}
30+
1931
// CHECK-LABEL: @arc_default_inplace
2032
#[no_mangle]
2133
pub fn arc_default_inplace() -> Arc<(String, String)> {

0 commit comments

Comments
 (0)