about summary refs log tree commit diff
path: root/library/alloc/src/vec
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-05-01 00:50:46 +0000
committerbors <bors@rust-lang.org>2022-05-01 00:50:46 +0000
commitbf611439e3239ad3f74bd76cc46a4e89b87d8219 (patch)
treeb10670aa268126bb05f46eaa1808fe47d1012bb2 /library/alloc/src/vec
parent2c858a7c3f189eb11ad89d9bf9f2e87cac9d2b76 (diff)
parent8034c45a07ff782b32f3fdc054338b524847aea1 (diff)
downloadrust-bf611439e3239ad3f74bd76cc46a4e89b87d8219.tar.gz
rust-bf611439e3239ad3f74bd76cc46a4e89b87d8219.zip
Auto merge of #95362 - scottmcm:calloc-arrays, r=Mark-Simulacrum
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.
Diffstat (limited to 'library/alloc/src/vec')
-rw-r--r--library/alloc/src/vec/is_zero.rs9
1 files changed, 8 insertions, 1 deletions
diff --git a/library/alloc/src/vec/is_zero.rs b/library/alloc/src/vec/is_zero.rs
index 0efc4893c3c..868f2f1e323 100644
--- a/library/alloc/src/vec/is_zero.rs
+++ b/library/alloc/src/vec/is_zero.rs
@@ -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;
 }
 
@@ -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