about summary refs log tree commit diff
path: root/library/alloc/src/vec
diff options
context:
space:
mode:
authorScott McMurray <scottmcm@users.noreply.github.com>2022-05-01 00:16:16 -0700
committerScott McMurray <scottmcm@users.noreply.github.com>2022-05-01 22:28:11 -0700
commit2830dbd64f50b4a8025025f01578e45cbf9d3719 (patch)
tree3f673bd79d8b9645a36c6615eaf781dfd6139448 /library/alloc/src/vec
parent61469b682c2b0bf9cebc4622f1859e2bb3b7ebca (diff)
downloadrust-2830dbd64f50b4a8025025f01578e45cbf9d3719.tar.gz
rust-2830dbd64f50b4a8025025f01578e45cbf9d3719.zip
Tweak the calloc optimization to only apply to shortish-arrays
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 868f2f1e323..edf270db81d 100644
--- a/library/alloc/src/vec/is_zero.rs
+++ b/library/alloc/src/vec/is_zero.rs
@@ -52,7 +52,14 @@ 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)
+        // Because this is generated as a runtime check, it's not obvious that
+        // it's worth doing if the array is really long.  The threshold here
+        // is largely arbitrary, but was picked because as of 2022-05-01 LLVM
+        // can const-fold the check in `vec![[0; 32]; n]` but not in
+        // `vec![[0; 64]; n]`: https://godbolt.org/z/WTzjzfs5b
+        // Feel free to tweak if you have better evidence.
+
+        N <= 32 && self.iter().all(IsZero::is_zero)
     }
 }