Skip to content

Commit ae03ecd

Browse files
committed
Auto merge of #96596 - scottmcm:limited-calloc, r=Mark-Simulacrum
Tweak the vec-calloc runtime check to only apply to shortish-arrays r? `@Mark-Simulacrum` `@nbdd0121` pointed out in rust-lang/rust#95362 (comment) that LLVM currently doesn't constant-fold the `IsZero` check for long arrays, so that seems like a reasonable justification for limiting it. It appears that it's based on length, not byte size, (https://godbolt.org/z/4s48Y81dP), so that's what I used in the PR. Maybe it's a ["the number of inlining shall be three"](https://youtu.be/s4wnuiCwTGU?t=320) sort of situation. Certainly there's more that could be done here -- that generated code that checks long arrays byte-by-byte is highly suboptimal, for example -- but this is an easy, low-risk tweak.
2 parents 7604635 + 902c7d5 commit ae03ecd

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)