Skip to content

Commit d34e43b

Browse files
authored
Optimize Build::env_cache: Store Option<Arc<str>> as key (#832)
1 parent f74fe61 commit d34e43b

File tree

1 file changed

+31
-32
lines changed

1 file changed

+31
-32
lines changed

src/lib.rs

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ pub struct Build {
126126
warnings_into_errors: bool,
127127
warnings: Option<bool>,
128128
extra_warnings: Option<bool>,
129-
env_cache: Arc<Mutex<HashMap<String, Option<String>>>>,
129+
env_cache: Arc<Mutex<HashMap<String, Option<Arc<str>>>>>,
130130
apple_sdk_root_cache: Arc<Mutex<HashMap<String, OsString>>>,
131131
emit_rerun_if_env_changed: bool,
132132
}
@@ -1605,9 +1605,8 @@ impl Build {
16051605
Some(true) => "-MT",
16061606
Some(false) => "-MD",
16071607
None => {
1608-
let features = self
1609-
.getenv("CARGO_CFG_TARGET_FEATURE")
1610-
.unwrap_or(String::new());
1608+
let features = self.getenv("CARGO_CFG_TARGET_FEATURE");
1609+
let features = features.as_deref().unwrap_or_default();
16111610
if features.contains("crt-static") {
16121611
"-MT"
16131612
} else {
@@ -1827,9 +1826,8 @@ impl Build {
18271826
}
18281827

18291828
if self.static_flag.is_none() {
1830-
let features = self
1831-
.getenv("CARGO_CFG_TARGET_FEATURE")
1832-
.unwrap_or(String::new());
1829+
let features = self.getenv("CARGO_CFG_TARGET_FEATURE");
1830+
let features = features.as_deref().unwrap_or_default();
18331831
if features.contains("crt-static") {
18341832
cmd.args.push("-static".into());
18351833
}
@@ -2381,6 +2379,7 @@ impl Build {
23812379
}
23822380
let host = self.get_host()?;
23832381
let target = self.get_target()?;
2382+
let target = &*target;
23842383
let (env, msvc, gnu, traditional, clang) = if self.cpp {
23852384
("CXX", "cl.exe", "g++", "c++", "clang++")
23862385
} else {
@@ -2412,7 +2411,7 @@ impl Build {
24122411
// semi-buggy build scripts which are shared in
24132412
// makefiles/configure scripts (where spaces are far more
24142413
// lenient)
2415-
let mut t = Tool::with_clang_driver(PathBuf::from(tool.trim()), driver_mode);
2414+
let mut t = Tool::with_clang_driver(tool, driver_mode);
24162415
if let Some(cc_wrapper) = wrapper {
24172416
t.cc_wrapper_path = Some(PathBuf::from(cc_wrapper));
24182417
}
@@ -2472,7 +2471,7 @@ impl Build {
24722471
format!("arm-kmc-eabi-{}", gnu)
24732472
} else if target.starts_with("aarch64-kmc-solid_") {
24742473
format!("aarch64-kmc-elf-{}", gnu)
2475-
} else if self.get_host()? != target {
2474+
} else if &*self.get_host()? != target {
24762475
let prefix = self.prefix_for_target(&target);
24772476
match prefix {
24782477
Some(prefix) => {
@@ -2499,10 +2498,10 @@ impl Build {
24992498
"CUDA compilation currently assumes empty pre-existing args"
25002499
);
25012500
let nvcc = match self.getenv_with_target_prefixes("NVCC") {
2502-
Err(_) => "nvcc".into(),
2503-
Ok(nvcc) => nvcc,
2501+
Err(_) => PathBuf::from("nvcc"),
2502+
Ok(nvcc) => PathBuf::from(&*nvcc),
25042503
};
2505-
let mut nvcc_tool = Tool::with_features(PathBuf::from(nvcc), None, self.cuda);
2504+
let mut nvcc_tool = Tool::with_features(nvcc, None, self.cuda);
25062505
nvcc_tool
25072506
.args
25082507
.push(format!("-ccbin={}", tool.path.display()).into());
@@ -2589,7 +2588,7 @@ impl Build {
25892588
}
25902589

25912590
/// Returns compiler path, optional modifier name from whitelist, and arguments vec
2592-
fn env_tool(&self, name: &str) -> Option<(String, Option<String>, Vec<String>)> {
2591+
fn env_tool(&self, name: &str) -> Option<(PathBuf, Option<String>, Vec<String>)> {
25932592
let tool = match self.getenv_with_target_prefixes(name) {
25942593
Ok(tool) => tool,
25952594
Err(_) => return None,
@@ -2598,8 +2597,8 @@ impl Build {
25982597
// If this is an exact path on the filesystem we don't want to do any
25992598
// interpretation at all, just pass it on through. This'll hopefully get
26002599
// us to support spaces-in-paths.
2601-
if Path::new(&tool).exists() {
2602-
return Some((tool, None, Vec::new()));
2600+
if Path::new(&*tool).exists() {
2601+
return Some((PathBuf::from(&*tool), None, Vec::new()));
26032602
}
26042603

26052604
// Ok now we want to handle a couple of scenarios. We'll assume from
@@ -2638,15 +2637,15 @@ impl Build {
26382637
if known_wrappers.contains(&file_stem) {
26392638
if let Some(compiler) = parts.next() {
26402639
return Some((
2641-
compiler.to_string(),
2640+
compiler.into(),
26422641
Some(maybe_wrapper.to_string()),
26432642
parts.map(|s| s.to_string()).collect(),
26442643
));
26452644
}
26462645
}
26472646

26482647
Some((
2649-
maybe_wrapper.to_string(),
2648+
maybe_wrapper.into(),
26502649
Self::rustc_wrapper_fallback(),
26512650
parts.map(|s| s.to_string()).collect(),
26522651
))
@@ -2665,7 +2664,7 @@ impl Build {
26652664
if stdlib.is_empty() {
26662665
Ok(None)
26672666
} else {
2668-
Ok(Some(stdlib))
2667+
Ok(Some(stdlib.to_string()))
26692668
}
26702669
} else {
26712670
let target = self.get_target()?;
@@ -3070,30 +3069,30 @@ impl Build {
30703069
prefixes.first().map(|prefix| *prefix))
30713070
}
30723071

3073-
fn get_target(&self) -> Result<Cow<str>, Error> {
3072+
fn get_target(&self) -> Result<Arc<str>, Error> {
30743073
match &self.target {
3075-
Some(t) => Ok(Cow::Borrowed(&t)),
3076-
None => Ok(Cow::Owned(self.getenv_unwrap("TARGET")?)),
3074+
Some(t) => Ok(t.clone()),
3075+
None => self.getenv_unwrap("TARGET"),
30773076
}
30783077
}
30793078

3080-
fn get_host(&self) -> Result<Cow<str>, Error> {
3079+
fn get_host(&self) -> Result<Arc<str>, Error> {
30813080
match &self.host {
3082-
Some(h) => Ok(Cow::Borrowed(&h)),
3083-
None => Ok(Cow::Owned(self.getenv_unwrap("HOST")?)),
3081+
Some(h) => Ok(h.clone()),
3082+
None => self.getenv_unwrap("HOST"),
30843083
}
30853084
}
30863085

3087-
fn get_opt_level(&self) -> Result<Cow<str>, Error> {
3086+
fn get_opt_level(&self) -> Result<Arc<str>, Error> {
30883087
match &self.opt_level {
3089-
Some(ol) => Ok(Cow::Borrowed(&ol)),
3090-
None => Ok(Cow::Owned(self.getenv_unwrap("OPT_LEVEL")?)),
3088+
Some(ol) => Ok(ol.clone()),
3089+
None => self.getenv_unwrap("OPT_LEVEL"),
30913090
}
30923091
}
30933092

30943093
fn get_debug(&self) -> bool {
30953094
self.debug.unwrap_or_else(|| match self.getenv("DEBUG") {
3096-
Some(s) => s != "false",
3095+
Some(s) => &*s != "false",
30973096
None => false,
30983097
})
30993098
}
@@ -3136,7 +3135,7 @@ impl Build {
31363135
}
31373136
}
31383137

3139-
fn getenv(&self, v: &str) -> Option<String> {
3138+
fn getenv(&self, v: &str) -> Option<Arc<str>> {
31403139
// Returns true for environment variables cargo sets for build scripts:
31413140
// https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts
31423141
//
@@ -3158,13 +3157,13 @@ impl Build {
31583157
if self.emit_rerun_if_env_changed && !provided_by_cargo(v) {
31593158
self.print(&format_args!("cargo:rerun-if-env-changed={}", v));
31603159
}
3161-
let r = env::var(v).ok();
3160+
let r = env::var(v).ok().map(Arc::from);
31623161
self.print(&format_args!("{} = {:?}", v, r));
31633162
cache.insert(v.to_string(), r.clone());
31643163
r
31653164
}
31663165

3167-
fn getenv_unwrap(&self, v: &str) -> Result<String, Error> {
3166+
fn getenv_unwrap(&self, v: &str) -> Result<Arc<str>, Error> {
31683167
match self.getenv(v) {
31693168
Some(s) => Ok(s),
31703169
None => Err(Error::new(
@@ -3174,7 +3173,7 @@ impl Build {
31743173
}
31753174
}
31763175

3177-
fn getenv_with_target_prefixes(&self, var_base: &str) -> Result<String, Error> {
3176+
fn getenv_with_target_prefixes(&self, var_base: &str) -> Result<Arc<str>, Error> {
31783177
let target = self.get_target()?;
31793178
let host = self.get_host()?;
31803179
let kind = if host == target { "HOST" } else { "TARGET" };

0 commit comments

Comments
 (0)