memset not being optimized out when initializing array contents
Apologies as there's most likely already a tracking issue for this, but I wasn't able to find it. When initializing an array's contents, it seems that the initial memset is not optimized out:
https://godbolt.org/g/pbgnlq
let mut arr = [0; 1000];
for i in 0..arr.len() {
arr[i] = i;
}
println!("{}", arr[5])
Clang has the same issue actually, except there memset would have to be called explicitly.
If clang has the same issue, it's likely an LLVM bug, not our bug, I'd guess.
Steve Klabnik at 2016-12-26 14:47:13
Quite possibly yes, the difference is that everything starts out uninitialized so you wouldn't be running into this case by default. Could MIR omit the check to prevent it from cascading down to the IR level?
Learn OpenGL ES at 2016-12-26 16:10:30
Sounds like it's related to #35662
bluss at 2016-12-26 16:42:39
It seems to be present in older versions of Rust too (at least when comparing with rust.godbolt.org). I tried switching to enumerate and that didn't cause the memset to go away.
Learn OpenGL ES at 2016-12-26 17:38:25
Quite possibly yes, the difference is that everything starts out uninitialized so you wouldn't be running into this case by default.
Use
std::mem::uninitialized()if you want the default behaviour of C.Simonas Kazlauskas at 2016-12-26 18:21:22
Hi, I don't believe I said I wanted the behaviour of C? I only wanted to clarify that we wouldn't run into this bug as often there because arrays start out uninitialized. I'd prefer the behavior of Rust but without the redundant zero fill. :)
Learn OpenGL ES at 2016-12-26 21:16:25
Is it even a good idea to allocate 4 KB off the stack? And, when allocating off the heap, you can just do
(0..1000).collect()to a vector and issue won't appear as internal array will be allocated and filled just once.Ingvar Stepanyan at 2016-12-30 15:30:10
Triage: Problem remains in Rust 1.72. Ideally this would be fixed by LLVM, but the incentives for LLVM to do so might be weak since this is mostly a problem in safe languages. So to get this fixed a Rust specific MIR optimization pass is probably needed.
Martin Nordholts at 2023-09-14 14:11:54