Replace WriteCloneIntoRaw with CloneToUninit.

This commit is contained in:
Kevin Reid 2024-06-11 10:46:27 -07:00
parent ec201b8650
commit a9a4830d25
5 changed files with 11 additions and 34 deletions

View File

@ -424,29 +424,3 @@ pub mod __alloc_error_handler {
} }
} }
} }
#[cfg(not(no_global_oom_handling))]
/// Specialize clones into pre-allocated, uninitialized memory.
/// Used by `Box::clone` and `Rc`/`Arc::make_mut`.
pub(crate) trait WriteCloneIntoRaw: Sized {
unsafe fn write_clone_into_raw(&self, target: *mut Self);
}
#[cfg(not(no_global_oom_handling))]
impl<T: Clone> WriteCloneIntoRaw for T {
#[inline]
default unsafe fn write_clone_into_raw(&self, target: *mut Self) {
// Having allocated *first* may allow the optimizer to create
// the cloned value in-place, skipping the local and move.
unsafe { target.write(self.clone()) };
}
}
#[cfg(not(no_global_oom_handling))]
impl<T: Copy> WriteCloneIntoRaw for T {
#[inline]
unsafe fn write_clone_into_raw(&self, target: *mut Self) {
// We can always copy in-place, without ever involving a local value.
unsafe { target.copy_from_nonoverlapping(self, 1) };
}
}

View File

@ -188,6 +188,8 @@
use core::any::Any; use core::any::Any;
use core::async_iter::AsyncIterator; use core::async_iter::AsyncIterator;
use core::borrow; use core::borrow;
#[cfg(not(no_global_oom_handling))]
use core::clone::CloneToUninit;
use core::cmp::Ordering; use core::cmp::Ordering;
use core::error::Error; use core::error::Error;
use core::fmt; use core::fmt;
@ -207,7 +209,7 @@ use core::slice;
use core::task::{Context, Poll}; use core::task::{Context, Poll};
#[cfg(not(no_global_oom_handling))] #[cfg(not(no_global_oom_handling))]
use crate::alloc::{handle_alloc_error, WriteCloneIntoRaw}; use crate::alloc::handle_alloc_error;
use crate::alloc::{AllocError, Allocator, Global, Layout}; use crate::alloc::{AllocError, Allocator, Global, Layout};
#[cfg(not(no_global_oom_handling))] #[cfg(not(no_global_oom_handling))]
use crate::borrow::Cow; use crate::borrow::Cow;
@ -1346,7 +1348,7 @@ impl<T: Clone, A: Allocator + Clone> Clone for Box<T, A> {
// Pre-allocate memory to allow writing the cloned value directly. // Pre-allocate memory to allow writing the cloned value directly.
let mut boxed = Self::new_uninit_in(self.1.clone()); let mut boxed = Self::new_uninit_in(self.1.clone());
unsafe { unsafe {
(**self).write_clone_into_raw(boxed.as_mut_ptr()); (**self).clone_to_uninit(boxed.as_mut_ptr());
boxed.assume_init() boxed.assume_init()
} }
} }

View File

@ -103,6 +103,7 @@
#![feature(assert_matches)] #![feature(assert_matches)]
#![feature(async_fn_traits)] #![feature(async_fn_traits)]
#![feature(async_iterator)] #![feature(async_iterator)]
#![feature(clone_to_uninit)]
#![feature(coerce_unsized)] #![feature(coerce_unsized)]
#![feature(const_align_of_val)] #![feature(const_align_of_val)]
#![feature(const_box)] #![feature(const_box)]

View File

@ -249,6 +249,8 @@ use std::boxed::Box;
use core::any::Any; use core::any::Any;
use core::borrow; use core::borrow;
use core::cell::Cell; use core::cell::Cell;
#[cfg(not(no_global_oom_handling))]
use core::clone::CloneToUninit;
use core::cmp::Ordering; use core::cmp::Ordering;
use core::fmt; use core::fmt;
use core::hash::{Hash, Hasher}; use core::hash::{Hash, Hasher};
@ -268,8 +270,6 @@ use core::slice::from_raw_parts_mut;
#[cfg(not(no_global_oom_handling))] #[cfg(not(no_global_oom_handling))]
use crate::alloc::handle_alloc_error; use crate::alloc::handle_alloc_error;
#[cfg(not(no_global_oom_handling))]
use crate::alloc::WriteCloneIntoRaw;
use crate::alloc::{AllocError, Allocator, Global, Layout}; use crate::alloc::{AllocError, Allocator, Global, Layout};
use crate::borrow::{Cow, ToOwned}; use crate::borrow::{Cow, ToOwned};
#[cfg(not(no_global_oom_handling))] #[cfg(not(no_global_oom_handling))]
@ -1810,7 +1810,7 @@ impl<T: Clone, A: Allocator + Clone> Rc<T, A> {
let mut rc = Self::new_uninit_in(this.alloc.clone()); let mut rc = Self::new_uninit_in(this.alloc.clone());
unsafe { unsafe {
let data = Rc::get_mut_unchecked(&mut rc); let data = Rc::get_mut_unchecked(&mut rc);
(**this).write_clone_into_raw(data.as_mut_ptr()); (**this).clone_to_uninit(data.as_mut_ptr());
*this = rc.assume_init(); *this = rc.assume_init();
} }
} else if Rc::weak_count(this) != 0 { } else if Rc::weak_count(this) != 0 {

View File

@ -10,6 +10,8 @@
use core::any::Any; use core::any::Any;
use core::borrow; use core::borrow;
#[cfg(not(no_global_oom_handling))]
use core::clone::CloneToUninit;
use core::cmp::Ordering; use core::cmp::Ordering;
use core::fmt; use core::fmt;
use core::hash::{Hash, Hasher}; use core::hash::{Hash, Hasher};
@ -30,8 +32,6 @@ use core::sync::atomic::Ordering::{Acquire, Relaxed, Release};
#[cfg(not(no_global_oom_handling))] #[cfg(not(no_global_oom_handling))]
use crate::alloc::handle_alloc_error; use crate::alloc::handle_alloc_error;
#[cfg(not(no_global_oom_handling))]
use crate::alloc::WriteCloneIntoRaw;
use crate::alloc::{AllocError, Allocator, Global, Layout}; use crate::alloc::{AllocError, Allocator, Global, Layout};
use crate::borrow::{Cow, ToOwned}; use crate::borrow::{Cow, ToOwned};
use crate::boxed::Box; use crate::boxed::Box;
@ -2219,7 +2219,7 @@ impl<T: Clone, A: Allocator + Clone> Arc<T, A> {
let mut arc = Self::new_uninit_in(this.alloc.clone()); let mut arc = Self::new_uninit_in(this.alloc.clone());
unsafe { unsafe {
let data = Arc::get_mut_unchecked(&mut arc); let data = Arc::get_mut_unchecked(&mut arc);
(**this).write_clone_into_raw(data.as_mut_ptr()); (**this).clone_to_uninit(data.as_mut_ptr());
*this = arc.assume_init(); *this = arc.assume_init();
} }
} else if this.inner().weak.load(Relaxed) != 1 { } else if this.inner().weak.load(Relaxed) != 1 {