-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Command-line arguments are cloned a lot on Unix #47164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Incidentally, I was just reading http://andrewkelley.me/post/zig-december-2017-in-review.html which says
and i was wondering about our code regarding this. |
Some ideas on reducing each source of allocation/cloning (on startup, and on iterator construction):
|
Ah, yes. On Windows the We definitely can't get to zero copies on Windows, because we at least need to do UTF-16 to UTF-8 conversion. |
#47165 eliminates the allocations/copies on startup. |
[unix] Don't clone command-line args on startup Fixes part of rust-lang#47164 and simplifies the `args` code on non-Apple Unix platforms. Note: This could change behavior for programs that use both `std::env::args` *and* unsafe code that mutates `argv` directly. However, these programs already behave differently on different platforms. The new behavior on non-Apple platforms is closer to the existing behavior on Apple platforms.
The
std::sys::unix::args
module does a lot of allocation and cloning of command-line parameters:std::sys::unix::args::init
copies all of the command-line arguments into aBox<Vec<Vec<u8>>>
(except on macOS and iOS).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
andBox
) 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).
The text was updated successfully, but these errors were encountered: