See the following code:
pub fn foo() -> [[i32; 1000]; 1000] {
[[0; 1000]; 1000]
}
pub fn bar() -> [i32; 1_000_000] {
[0; 1_000_000]
}
Ideally they should generate the same code as [[i32; 1000]; 1000] is essentially just [i32; 1_000_000]. However, the first function does one memset to set a [i32; 1000] on the stack, then use memcpy to generate the big array, while the second just uses a single memset.
The code generated from the first is way larger than the second, and I would also expect it to be much slower given its repeatedly invoking memcpy.
See the following code:
Ideally they should generate the same code as
[[i32; 1000]; 1000]is essentially just[i32; 1_000_000]. However, the first function does onememsetto set a[i32; 1000]on the stack, then usememcpyto generate the big array, while the second just uses a singlememset.The code generated from the first is way larger than the second, and I would also expect it to be much slower given its repeatedly invoking
memcpy.