Description
The std::sys::unix::args
module does a lot of allocation and cloning of command-line parameters:
- On startup,
std::sys::unix::args::init
copies all of the command-line arguments into aBox<Vec<Vec<u8>>>
(except on macOS and iOS). - When
std::env::args
orargs_os
is called, it eagerly copies all of the args into a newVec<OsString>
.
On non-Apple systems, this means there is at least one allocation and clone per argument (plus 2 additional allocations, for the outer Vec
and Box
) even if they are never accessed. These extra allocations take up space on the heap for the duration of the program.
On both Apple and non-Apple systems, accessing any args causes at least one additional allocation and clone of every arg. Calling std::env::args
more than once causes all arguments to be cloned again, even if the caller doesn't iterate through all of them.
On Windows, for comparison, each arg is cloned lazily only when it is yielded from the iterator, so there are zero allocations or clones for args that are never accessed (update: at least, no clones in Rust code; see comments below).