]> git.lizzy.rs Git - rust.git/commitdiff
Auto merge of #95362 - scottmcm:calloc-arrays, r=Mark-Simulacrum
authorbors <bors@rust-lang.org>
Sun, 1 May 2022 00:50:46 +0000 (00:50 +0000)
committerbors <bors@rust-lang.org>
Sun, 1 May 2022 00:50:46 +0000 (00:50 +0000)
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.

library/alloc/src/vec/is_zero.rs

index 0efc4893c3c42847a696197a1a223687cd9f0741..868f2f1e3231b089fd906854216871a7feb63a84 100644 (file)
@@ -2,7 +2,7 @@
 
 #[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 @@ fn is_zero(&self) -> bool {
     }
 }
 
+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