Skip to content

Command-line arguments are cloned a lot on Unix #47164

Open
@mbrubeck

Description

@mbrubeck

The std::sys::unix::args module does a lot of allocation and cloning of command-line parameters:

  1. On startup, std::sys::unix::args::init copies all of the command-line arguments into a Box<Vec<Vec<u8>>> (except on macOS and iOS).
  2. When std::env::args or args_os is called, it eagerly copies all of the args into a new Vec<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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory: An issue proposing an enhancement or a PR with one.I-slowIssue: Problems and improvements with respect to performance of generated code.O-linuxOperating system: LinuxT-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions