You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#![no_std]fnedit(d:&mut[u8]){
d[40] = 1;
d[80] = 1;
d[120] = 1;
d[160] = 1;}// Returning the array is finepubfnarray_good() -> [u8;200]{letmut d = [0u8;200];edit(&mut d);
d
}// Setting the bytes within the option is also finepubfnoption_good() -> Option<[u8;200]>{letmut o = Some([0u8;200]);ifletSome(refmut d) = o {edit(d);}
o
}// When returning an initialized array in an Option,// the initialization gets split into multiple separate memset/memclr calls,// just to optimize away a few redundant byte clears.pubfnoption_bad() -> Option<[u8;200]>{Some(array_good())}
I tried this code:
On godbolt: https://godbolt.org/z/e8z3ornos
I expected to see this happen: In all three cases I expect roughly similar code with a single call to
memclr
followed by four stores.Instead, this happened: The first implementation splits the initialization into five different calls to
memclr
. This is unnecessary and inefficient.The behavior is not target specific. The behavior is the same on x86, for larger arrays and initialized segments it will also call
memset
.-C opt-level=1
doesn't inline and thus suppresses the issue.Meta
rustc --version --verbose
:This behavior is present between 1.45.0 and current nightly. Before 1.45.0 all three implementations split the initialization.
I noticed this when looking at #83022 (comment)
The text was updated successfully, but these errors were encountered: