Skip to content

Commit 902c7d5

Browse files
committed
Tweak the calloc optimization to only apply to shortish-arrays
1 parent da9a557 commit 902c7d5

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

alloc/src/vec/is_zero.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,14 @@ unsafe impl<T> IsZero for *mut T {
5252
unsafe impl<T: IsZero, const N: usize> IsZero for [T; N] {
5353
#[inline]
5454
fn is_zero(&self) -> bool {
55-
self.iter().all(IsZero::is_zero)
55+
// Because this is generated as a runtime check, it's not obvious that
56+
// it's worth doing if the array is really long. The threshold here
57+
// is largely arbitrary, but was picked because as of 2022-05-01 LLVM
58+
// can const-fold the check in `vec![[0; 32]; n]` but not in
59+
// `vec![[0; 64]; n]`: https://godbolt.org/z/WTzjzfs5b
60+
// Feel free to tweak if you have better evidence.
61+
62+
N <= 32 && self.iter().all(IsZero::is_zero)
5663
}
5764
}
5865

0 commit comments

Comments
 (0)