Skip to content

Commit

Permalink
Auto merge of rust-lang#95362 - scottmcm:calloc-arrays, r=Mark-Simula…
Browse files Browse the repository at this point in the history
…crum

Support arrays of zeros in Vec's __rust_alloc_zeroed optimization

I happened to notice in https://users.rust-lang.org/t/any-advantage-of-box-u64-16-16-16-over-vec-u64/73500/3?u=scottmcm that the calloc optimization wasn't applying to vectors-of-arrays, so here's the easy fix for that.
  • Loading branch information
bors committed May 1, 2022
2 parents 2c858a7 + 8034c45 commit bf61143
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion library/alloc/src/vec/is_zero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::boxed::Box;

#[rustc_specialization_trait]
pub(super) unsafe trait IsZero {
/// Whether this value is zero
/// Whether this value's representation is all zeros
fn is_zero(&self) -> bool;
}

Expand Down Expand Up @@ -49,6 +49,13 @@ unsafe impl<T> IsZero for *mut T {
}
}

unsafe impl<T: IsZero, const N: usize> IsZero for [T; N] {
#[inline]
fn is_zero(&self) -> bool {
self.iter().all(IsZero::is_zero)
}
}

// `Option<&T>` and `Option<Box<T>>` are guaranteed to represent `None` as null.
// For fat pointers, the bytes that would be the pointer metadata in the `Some`
// variant are padding in the `None` variant, so ignoring them and
Expand Down

0 comments on commit bf61143

Please sign in to comment.