From 5b12d906bb66d308fb3e94d6d79e076fa87c7f7c Mon Sep 17 00:00:00 2001 From: Slanterns Date: Tue, 22 Oct 2024 01:08:15 -0700 Subject: [PATCH] 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(), + ) + } } }