diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2019-09-03 19:32:44 -0700 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2019-09-05 11:43:35 -0700 |
| commit | 0b7ba6ec54db24b676d376665692a49d0ecd603a (patch) | |
| tree | 25df682f7c109db23c0e59dacd72af3aa58610df /src/libstd/sys/windows | |
| parent | 618768492f0c731fcb770dc2d178abe840846419 (diff) | |
| download | rust-0b7ba6ec54db24b676d376665692a49d0ecd603a.tar.gz rust-0b7ba6ec54db24b676d376665692a49d0ecd603a.zip | |
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.
Diffstat (limited to 'src/libstd/sys/windows')
| -rw-r--r-- | src/libstd/sys/windows/process.rs | 24 |
1 files changed, 11 insertions, 13 deletions
diff --git a/src/libstd/sys/windows/process.rs b/src/libstd/sys/windows/process.rs index 05e0ca67064..8658deb8546 100644 --- a/src/libstd/sys/windows/process.rs +++ b/src/libstd/sys/windows/process.rs @@ -19,7 +19,7 @@ use crate::sys::pipe::{self, AnonPipe}; use crate::sys::stdio; use crate::sys::cvt; use crate::sys_common::{AsInner, FromInner, IntoInner}; -use crate::sys_common::process::{CommandEnv, EnvKey}; +use crate::sys_common::process::CommandEnv; use crate::borrow::Borrow; use libc::{c_void, EXIT_SUCCESS, EXIT_FAILURE}; @@ -30,30 +30,28 @@ use libc::{c_void, EXIT_SUCCESS, EXIT_FAILURE}; #[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)] #[doc(hidden)] -pub struct WindowsEnvKey(OsString); +pub struct EnvKey(OsString); -impl From<OsString> for WindowsEnvKey { +impl From<OsString> for EnvKey { fn from(k: OsString) -> Self { let mut buf = k.into_inner().into_inner(); buf.make_ascii_uppercase(); - WindowsEnvKey(FromInner::from_inner(FromInner::from_inner(buf))) + EnvKey(FromInner::from_inner(FromInner::from_inner(buf))) } } -impl From<WindowsEnvKey> for OsString { - fn from(k: WindowsEnvKey) -> Self { k.0 } +impl From<EnvKey> for OsString { + fn from(k: EnvKey) -> Self { k.0 } } -impl Borrow<OsStr> for WindowsEnvKey { +impl Borrow<OsStr> for EnvKey { fn borrow(&self) -> &OsStr { &self.0 } } -impl AsRef<OsStr> for WindowsEnvKey { +impl AsRef<OsStr> for EnvKey { fn as_ref(&self) -> &OsStr { &self.0 } } -impl EnvKey for WindowsEnvKey {} - fn ensure_no_nuls<T: AsRef<OsStr>>(str: T) -> io::Result<T> { if str.as_ref().encode_wide().any(|b| b == 0) { @@ -66,7 +64,7 @@ fn ensure_no_nuls<T: AsRef<OsStr>>(str: T) -> io::Result<T> { pub struct Command { program: OsString, args: Vec<OsString>, - env: CommandEnv<WindowsEnvKey>, + env: CommandEnv, cwd: Option<OsString>, flags: u32, detach: bool, // not currently exposed in std::process @@ -110,7 +108,7 @@ impl Command { pub fn arg(&mut self, arg: &OsStr) { self.args.push(arg.to_os_string()) } - pub fn env_mut(&mut self) -> &mut CommandEnv<WindowsEnvKey> { + pub fn env_mut(&mut self) -> &mut CommandEnv { &mut self.env } pub fn cwd(&mut self, dir: &OsStr) { @@ -498,7 +496,7 @@ fn make_command_line(prog: &OsStr, args: &[OsString]) -> io::Result<Vec<u16>> { } } -fn make_envp(maybe_env: Option<BTreeMap<WindowsEnvKey, OsString>>) +fn make_envp(maybe_env: Option<BTreeMap<EnvKey, OsString>>) -> io::Result<(*mut c_void, Vec<u16>)> { // On Windows we pass an "environment block" which is not a char**, but // rather a concatenation of null-terminated k=v\0 sequences, with a final |
