Description
Quite a lot of std
s FFI code uses mem::zeroed
to create empty structures that are to be filled by FFI. E.g.:
rust/library/std/src/sys/pal/windows/time.rs
Lines 68 to 72 in d2f335d
This is unnecessary, since the C code does not require the structures to be initialized (you wouldn't zero out structures in C either). Thus, this pattern just reduces performance, as it results in the initialization of potentially very large structures such as sockaddr_storage
. We should get rid of this pattern and replace it with proper handling of uninitialized data through MaybeUninit
.
Edit (after discussion below): In some instances one might decide to keep the zero-initialization behaviour, but I think this should still go through MaybeUninit::zeroed
instead of mem::zeroed
to make the point of initialization explicit (by introducing .assume_init()
calls in the right places.
I'll probably do the network code myself (I want to clean some things up there anyway), but I'm happy to mentor you if you'd like to help with other instances such as the filesystem code (library/std/src/sys/pal/*/fs.rs
). Just contact me here or on Zulip. The best way to find the pattern is probably by searching for mem::zeroed
in library/std/src/sys
.