Skip to content

Commit 715eef0

Browse files
committed
cache shared_helpers::parse_rustc_verbose result
Signed-off-by: onur-ozkan <[email protected]>
1 parent 68f09fc commit 715eef0

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/bootstrap/src/utils/shared_helpers.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use std::fs::OpenOptions;
1212
use std::io::Write;
1313
use std::process::Command;
1414
use std::str::FromStr;
15+
use std::sync::Once;
1516

1617
#[cfg(test)]
1718
mod tests;
@@ -65,9 +66,21 @@ pub fn exe(name: &str, target: &str) -> String {
6566
/// Panics if "RUSTC_VERBOSE" is defined with the value that is not an unsigned integer.
6667
#[inline(always)]
6768
pub fn parse_rustc_verbose() -> usize {
68-
match env::var("RUSTC_VERBOSE") {
69-
Ok(s) => usize::from_str(&s).expect("RUSTC_VERBOSE should be an integer"),
70-
Err(_) => 0,
69+
static mut VERBOSITY: usize = 0;
70+
static INIT: Once = Once::new();
71+
72+
// SAFETY: Write only happens once.
73+
unsafe {
74+
// Result of this function never changes, so cache it to avoid recomputing
75+
// it repeatedly as it’s called thousands of times from the rustc shim.
76+
INIT.call_once(|| {
77+
VERBOSITY = match env::var("RUSTC_VERBOSE") {
78+
Ok(s) => usize::from_str(&s).expect("RUSTC_VERBOSE should be an integer"),
79+
Err(_) => 0,
80+
};
81+
});
82+
83+
VERBOSITY
7184
}
7285
}
7386

0 commit comments

Comments
 (0)