Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize Rc<T>::default #132031

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion library/alloc/src/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2312,7 +2312,16 @@ impl<T: Default> Default for Rc<T> {
/// ```
#[inline]
fn default() -> Rc<T> {
Rc::new(Default::default())
unsafe {
Self::from_inner(
Box::leak(Box::write(Box::new_uninit(), RcInner {
strong: Cell::new(1),
weak: Cell::new(1),
value: T::default(),
}))
.into(),
)
}
}
}

Expand Down
17 changes: 10 additions & 7 deletions library/alloc/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3447,13 +3447,16 @@ impl<T: Default> Default for Arc<T> {
/// assert_eq!(*x, 0);
/// ```
fn default() -> Arc<T> {
let x = Box::into_raw(Box::write(Box::new_uninit(), ArcInner {
strong: atomic::AtomicUsize::new(1),
weak: atomic::AtomicUsize::new(1),
data: T::default(),
}));
// SAFETY: `Box::into_raw` consumes the `Box` and never returns null
unsafe { Self::from_inner(NonNull::new_unchecked(x)) }
unsafe {
Self::from_inner(
Box::leak(Box::write(Box::new_uninit(), ArcInner {
strong: atomic::AtomicUsize::new(1),
weak: atomic::AtomicUsize::new(1),
data: T::default(),
}))
.into(),
)
}
}
}

Expand Down
12 changes: 12 additions & 0 deletions tests/codegen/placement-new.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
//@ compile-flags: -O
//@ compile-flags: -Zmerge-functions=disabled
#![crate_type = "lib"]

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

use std::rc::Rc;
use std::sync::Arc;

// CHECK-LABEL: @box_default_inplace
Expand All @@ -16,6 +18,16 @@ pub fn box_default_inplace() -> Box<(String, String)> {
Box::default()
}

// CHECK-LABEL: @rc_default_inplace
#[no_mangle]
pub fn rc_default_inplace() -> Rc<(String, String)> {
// CHECK-NOT: alloca
// CHECK: [[RC:%.*]] = {{.*}}call {{.*}}__rust_alloc(
// CHECK-NOT: call void @llvm.memcpy
// CHECK: ret ptr [[RC]]
Rc::default()
}

// CHECK-LABEL: @arc_default_inplace
#[no_mangle]
pub fn arc_default_inplace() -> Arc<(String, String)> {
Expand Down
Loading