From 5b12d906bb66d308fb3e94d6d79e076fa87c7f7c Mon Sep 17 00:00:00 2001 From: Slanterns Date: Tue, 22 Oct 2024 01:08:15 -0700 Subject: [PATCH 1/3] optimize `Rc::default` --- library/alloc/src/rc.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/library/alloc/src/rc.rs b/library/alloc/src/rc.rs index e98ae7c31ad..582d850e14b 100644 --- a/library/alloc/src/rc.rs +++ b/library/alloc/src/rc.rs @@ -2312,7 +2312,16 @@ impl Default for Rc { /// ``` #[inline] fn default() -> Rc { - 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(), + ) + } } } From 7782401c520ab1e744dbcf58d0f0254672da9026 Mon Sep 17 00:00:00 2001 From: Slanterns Date: Tue, 22 Oct 2024 01:33:22 -0700 Subject: [PATCH 2/3] add codegen test --- tests/codegen/placement-new.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/codegen/placement-new.rs b/tests/codegen/placement-new.rs index edb25df5eb4..0ec2b6a6f20 100644 --- a/tests/codegen/placement-new.rs +++ b/tests/codegen/placement-new.rs @@ -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 @@ -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)> { From 0a963ab2da139b177ef1e7f09efed7e4e1e8e930 Mon Sep 17 00:00:00 2001 From: Slanterns Date: Tue, 22 Oct 2024 01:37:13 -0700 Subject: [PATCH 3/3] refactor `Arc::default` --- library/alloc/src/sync.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/library/alloc/src/sync.rs b/library/alloc/src/sync.rs index acbc325a514..13677245e98 100644 --- a/library/alloc/src/sync.rs +++ b/library/alloc/src/sync.rs @@ -3447,13 +3447,16 @@ impl Default for Arc { /// assert_eq!(*x, 0); /// ``` fn default() -> Arc { - 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(), + ) + } } }