mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
Tweak the calloc optimization to only apply to shortish-arrays
This commit is contained in:
parent
61469b682c
commit
2830dbd64f
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
32
src/test/codegen/vec-calloc.rs
Normal file
32
src/test/codegen/vec-calloc.rs
Normal file
@ -0,0 +1,32 @@
|
||||
// compile-flags: -O
|
||||
// only-x86_64
|
||||
// ignore-debug
|
||||
|
||||
#![crate_type = "lib"]
|
||||
|
||||
// CHECK-LABEL: @vec_zero_scalar
|
||||
#[no_mangle]
|
||||
pub fn vec_zero_scalar(n: usize) -> Vec<i32> {
|
||||
// CHECK-NOT: __rust_alloc(
|
||||
// CHECK: __rust_alloc_zeroed(
|
||||
// CHECK-NOT: __rust_alloc(
|
||||
vec![0; n]
|
||||
}
|
||||
|
||||
// CHECK-LABEL: @vec_zero_rgb48
|
||||
#[no_mangle]
|
||||
pub fn vec_zero_rgb48(n: usize) -> Vec<[u16; 3]> {
|
||||
// CHECK-NOT: __rust_alloc(
|
||||
// CHECK: __rust_alloc_zeroed(
|
||||
// CHECK-NOT: __rust_alloc(
|
||||
vec![[0, 0, 0]; n]
|
||||
}
|
||||
|
||||
// CHECK-LABEL: @vec_zero_array_32
|
||||
#[no_mangle]
|
||||
pub fn vec_zero_array_32(n: usize) -> Vec<[i64; 32]> {
|
||||
// CHECK-NOT: __rust_alloc(
|
||||
// CHECK: __rust_alloc_zeroed(
|
||||
// CHECK-NOT: __rust_alloc(
|
||||
vec![[0_i64; 32]; n]
|
||||
}
|
Loading…
Reference in New Issue
Block a user