Skip to content

Commit deea7a9

Browse files
committed
perf experiment: try to replace box_new with write_via_move
1 parent ebe145e commit deea7a9

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

library/alloc/src/boxed.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,12 @@ impl<T> Box<T> {
259259
#[rustc_diagnostic_item = "box_new"]
260260
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
261261
pub fn new(x: T) -> Self {
262-
return box_new(x);
262+
let mut b = Box::new_uninit();
263+
let ptr = mem::MaybeUninit::as_mut_ptr(&mut *b);
264+
// SAFETY: we just allocated the box to store `x`.
265+
unsafe { core::intrinsics::write_via_move(ptr, x) };
266+
// SAFETY: we just initialized `b`.
267+
unsafe { b.assume_init() }
263268
}
264269

265270
/// Constructs a new box with uninitialized contents.

0 commit comments

Comments
 (0)