Optimize arg parsing: Avoid O(n) Vec::remove

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-07-21 12:44:54 +10:00
parent ef72f851f7
commit c418c2dbbe
No known key found for this signature in database
GPG key ID: 591C0B03040416D6

View file

@ -186,9 +186,20 @@ async fn entry(jobserver_client: LazyJobserverClient) -> Result<()> {
// `cargo run -- --help` gives ["target/debug/cargo-binstall", "--help"] // `cargo run -- --help` gives ["target/debug/cargo-binstall", "--help"]
// `cargo binstall --help` gives ["/home/ryan/.cargo/bin/cargo-binstall", "binstall", "--help"] // `cargo binstall --help` gives ["/home/ryan/.cargo/bin/cargo-binstall", "binstall", "--help"]
let mut args: Vec<OsString> = std::env::args_os().collect(); let mut args: Vec<OsString> = std::env::args_os().collect();
if args.len() > 1 && args[1] == "binstall" { let args = if args.len() > 1 && args[1] == "binstall" {
args.remove(1); // Equivalent to
} //
// args.remove(1);
//
// But is O(1)
args.swap(0, 1);
let mut args = args.into_iter();
drop(args.next().unwrap());
args
} else {
args.into_iter()
};
// Load options // Load options
let mut opts = Options::parse_from(args); let mut opts = Options::parse_from(args);