Skip to content

Commit 4c29cc8

Browse files
committed
Auto merge of #87777 - the8472:fix-mir-max-rss, r=oli-obk,joshtriplett
Use zeroed allocations in the mir interpreter instead eagerly touching the memory #86255 introduced a 30% regression in [page faults](https://perf.rust-lang.org/compare.html?start=64ae15ddd3f3cca7036ab2b2f3a6b130b62af4da&end=39e20f1ae5f13451eb35247808d6a2527cb7d060&stat=faults ) and a 3% regression in [max-rss](https://perf.rust-lang.org/index.html?start=2021-07-01&end=&absolute=false&stat=max-rss) in the ctfe-stress benchmarks. That's most likely happened because it separated allocation from initialization of the vec which defeats the zero-optimization. Currently there's no allocation API that is fallible, zeroing and returns a slice, so this PR introduces one and then uses that to solve the problem. In principle `vec.resize(len, 0)` could be optimized to use `alloc::grow_zeroed` where appropriate but that would require new specializations and new plumbing in `RawVec`.
2 parents 1f94abc + 1c21373 commit 4c29cc8

File tree

3 files changed

+72
-6
lines changed

3 files changed

+72
-6
lines changed

compiler/rustc_middle/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
//! This API is completely unstable and subject to change.
2424
2525
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
26+
#![feature(allocator_api)]
2627
#![feature(array_windows)]
2728
#![feature(assert_matches)]
2829
#![feature(backtrace)]
@@ -33,6 +34,7 @@
3334
#![feature(discriminant_kind)]
3435
#![feature(never_type)]
3536
#![feature(extern_types)]
37+
#![feature(new_uninit)]
3638
#![feature(nll)]
3739
#![feature(once_cell)]
3840
#![feature(min_specialization)]

compiler/rustc_middle/src/mir/interpret/allocation.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use crate::ty;
2828
pub struct Allocation<Tag = AllocId, Extra = ()> {
2929
/// The actual bytes of the allocation.
3030
/// Note that the bytes of a pointer represent the offset of the pointer.
31-
bytes: Vec<u8>,
31+
bytes: Box<[u8]>,
3232
/// Maps from byte addresses to extra data for each pointer.
3333
/// Only the first byte of a pointer is inserted into the map; i.e.,
3434
/// every entry in this map applies to `pointer_size` consecutive bytes starting
@@ -112,7 +112,7 @@ impl<Tag> Allocation<Tag> {
112112
align: Align,
113113
mutability: Mutability,
114114
) -> Self {
115-
let bytes = slice.into().into_owned();
115+
let bytes = Box::<[u8]>::from(slice.into());
116116
let size = Size::from_bytes(bytes.len());
117117
Self {
118118
bytes,
@@ -131,8 +131,7 @@ impl<Tag> Allocation<Tag> {
131131
/// Try to create an Allocation of `size` bytes, failing if there is not enough memory
132132
/// available to the compiler to do so.
133133
pub fn uninit(size: Size, align: Align, panic_on_fail: bool) -> InterpResult<'static, Self> {
134-
let mut bytes = Vec::new();
135-
bytes.try_reserve(size.bytes_usize()).map_err(|_| {
134+
let bytes = Box::<[u8]>::try_new_zeroed_slice(size.bytes_usize()).map_err(|_| {
136135
// This results in an error that can happen non-deterministically, since the memory
137136
// available to the compiler can change between runs. Normally queries are always
138137
// deterministic. However, we can be non-determinstic here because all uses of const
@@ -146,7 +145,8 @@ impl<Tag> Allocation<Tag> {
146145
});
147146
InterpError::ResourceExhaustion(ResourceExhaustionInfo::MemoryExhausted)
148147
})?;
149-
bytes.resize(size.bytes_usize(), 0);
148+
// SAFETY: the box was zero-allocated, which is a valid initial value for Box<[u8]>
149+
let bytes = unsafe { bytes.assume_init() };
150150
Ok(Allocation {
151151
bytes,
152152
relocations: Relocations::new(),

library/alloc/src/boxed.rs

+65-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ use crate::alloc::{handle_alloc_error, WriteCloneIntoRaw};
157157
use crate::alloc::{AllocError, Allocator, Global, Layout};
158158
#[cfg(not(no_global_oom_handling))]
159159
use crate::borrow::Cow;
160-
#[cfg(not(no_global_oom_handling))]
161160
use crate::raw_vec::RawVec;
162161
#[cfg(not(no_global_oom_handling))]
163162
use crate::str::from_boxed_utf8_unchecked;
@@ -589,6 +588,71 @@ impl<T> Box<[T]> {
589588
pub fn new_zeroed_slice(len: usize) -> Box<[mem::MaybeUninit<T>]> {
590589
unsafe { RawVec::with_capacity_zeroed(len).into_box(len) }
591590
}
591+
592+
/// Constructs a new boxed slice with uninitialized contents. Returns an error if
593+
/// the allocation fails
594+
///
595+
/// # Examples
596+
///
597+
/// ```
598+
/// #![feature(allocator_api, new_uninit)]
599+
///
600+
/// let mut values = Box::<[u32]>::try_new_uninit_slice(3)?;
601+
/// let values = unsafe {
602+
/// // Deferred initialization:
603+
/// values[0].as_mut_ptr().write(1);
604+
/// values[1].as_mut_ptr().write(2);
605+
/// values[2].as_mut_ptr().write(3);
606+
/// values.assume_init()
607+
/// };
608+
///
609+
/// assert_eq!(*values, [1, 2, 3]);
610+
/// # Ok::<(), std::alloc::AllocError>(())
611+
/// ```
612+
#[unstable(feature = "allocator_api", issue = "32838")]
613+
#[inline]
614+
pub fn try_new_uninit_slice(len: usize) -> Result<Box<[mem::MaybeUninit<T>]>, AllocError> {
615+
unsafe {
616+
let layout = match Layout::array::<mem::MaybeUninit<T>>(len) {
617+
Ok(l) => l,
618+
Err(_) => return Err(AllocError),
619+
};
620+
let ptr = Global.allocate(layout)?;
621+
Ok(RawVec::from_raw_parts_in(ptr.as_mut_ptr() as *mut _, len, Global).into_box(len))
622+
}
623+
}
624+
625+
/// Constructs a new boxed slice with uninitialized contents, with the memory
626+
/// being filled with `0` bytes. Returns an error if the allocation fails
627+
///
628+
/// See [`MaybeUninit::zeroed`][zeroed] for examples of correct and incorrect usage
629+
/// of this method.
630+
///
631+
/// # Examples
632+
///
633+
/// ```
634+
/// #![feature(allocator_api, new_uninit)]
635+
///
636+
/// let values = Box::<[u32]>::try_new_zeroed_slice(3)?;
637+
/// let values = unsafe { values.assume_init() };
638+
///
639+
/// assert_eq!(*values, [0, 0, 0]);
640+
/// # Ok::<(), std::alloc::AllocError>(())
641+
/// ```
642+
///
643+
/// [zeroed]: mem::MaybeUninit::zeroed
644+
#[unstable(feature = "allocator_api", issue = "32838")]
645+
#[inline]
646+
pub fn try_new_zeroed_slice(len: usize) -> Result<Box<[mem::MaybeUninit<T>]>, AllocError> {
647+
unsafe {
648+
let layout = match Layout::array::<mem::MaybeUninit<T>>(len) {
649+
Ok(l) => l,
650+
Err(_) => return Err(AllocError),
651+
};
652+
let ptr = Global.allocate_zeroed(layout)?;
653+
Ok(RawVec::from_raw_parts_in(ptr.as_mut_ptr() as *mut _, len, Global).into_box(len))
654+
}
655+
}
592656
}
593657

594658
impl<T, A: Allocator> Box<[T], A> {

0 commit comments

Comments
 (0)