std: Improve downstream codegen in Command::env

This commit rejiggers the generics used in the implementation of
`Command::env` with the purpose of reducing the amount of codegen that
needs to happen in consumer crates, instead preferring to generate code
into libstd.

This was found when profiling the compile times of the `cc` crate where
the binary rlib produced had a lot of `BTreeMap` code compiled into it
but the crate doesn't actually use `BTreeMap`. It turns out that
`Command::env` is generic enough to codegen the entire implementation in
calling crates, but in this case there's no performance concern so it's
fine to compile the code into the standard library.

This change is done by removing the generic on the `CommandEnv` map
which is intended to handle case-insensitive variables on Windows.
Instead now a generic isn't used but rather a `use` statement defined
per-platform is used.

With this commit a debug build of `Command::new("foo").env("a", "b")`
drops from 21k lines of LLVM IR to 10k.
This commit is contained in:
Alex Crichton
2019-09-03 19:32:44 -07:00
parent 618768492f
commit 0b7ba6ec54
9 changed files with 56 additions and 70 deletions

View File

@@ -7,7 +7,7 @@ use crate::ptr;
use crate::sys::fd::FileDesc;
use crate::sys::fs::{File, OpenOptions};
use crate::sys::pipe::{self, AnonPipe};
use crate::sys_common::process::{CommandEnv, DefaultEnvKey};
use crate::sys_common::process::CommandEnv;
use crate::collections::BTreeMap;
use libc::{c_int, gid_t, uid_t, c_char, EXIT_SUCCESS, EXIT_FAILURE};
@@ -69,7 +69,7 @@ pub struct Command {
program: CString,
args: Vec<CString>,
argv: Argv,
env: CommandEnv<DefaultEnvKey>,
env: CommandEnv,
cwd: Option<CString>,
uid: Option<uid_t>,
@@ -201,7 +201,7 @@ impl Command {
self.stderr = Some(stderr);
}
pub fn env_mut(&mut self) -> &mut CommandEnv<DefaultEnvKey> {
pub fn env_mut(&mut self) -> &mut CommandEnv {
&mut self.env
}
@@ -271,7 +271,7 @@ impl CStringArray {
}
}
fn construct_envp(env: BTreeMap<DefaultEnvKey, OsString>, saw_nul: &mut bool) -> CStringArray {
fn construct_envp(env: BTreeMap<OsString, OsString>, saw_nul: &mut bool) -> CStringArray {
let mut result = CStringArray::with_capacity(env.len());
for (k, v) in env {
let mut k: OsString = k.into();