Skip to content

Commit 83b01b9

Browse files
committed
use zeroed allocation instead of eagerly initializing the memory
1 parent de91157 commit 83b01b9

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
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

+4-3
Original file line numberDiff line numberDiff line change
@@ -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,9 @@ impl<Tag> Allocation<Tag> {
146145
});
147146
InterpError::ResourceExhaustion(ResourceExhaustionInfo::MemoryExhausted)
148147
})?;
149-
bytes.resize(size.bytes_usize(), 0);
148+
// SAFETY: This turns a Box<[MaybeUninit<u8>]> into a Vec<u8>. This is safe since the box
149+
// was zero-allocated which is a valid value for u8.
150+
let bytes = unsafe { bytes.assume_init().to_vec() };
150151
Ok(Allocation {
151152
bytes,
152153
relocations: Relocations::new(),

0 commit comments

Comments
 (0)