From 15dcf1fdeea8734d3f4b61944afe51afad83acd3 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Mon, 3 Feb 2020 16:49:33 +1100 Subject: [PATCH] Fix how the `RUSTC_CTFE_BACKTRACE` env var is gotten. This environment variable is currently obtained very frequently in CTFE-heavy code; using `lazy_static` avoids repeating the work. For the `ctfe-stress-4` benchmark this eliminates 67% of allocations done, and for `coercions` it eliminates 17% of allocations done. --- Cargo.lock | 1 + src/librustc/Cargo.toml | 1 + src/librustc/mir/interpret/error.rs | 6 +++++- 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 9c359b464ef1d..8b2da4d448ce6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3086,6 +3086,7 @@ dependencies = [ "fmt_macros", "graphviz", "jobserver", + "lazy_static 1.4.0", "log", "measureme", "parking_lot", diff --git a/src/librustc/Cargo.toml b/src/librustc/Cargo.toml index b65635be54a3f..7dfc5d55056ab 100644 --- a/src/librustc/Cargo.toml +++ b/src/librustc/Cargo.toml @@ -16,6 +16,7 @@ fmt_macros = { path = "../libfmt_macros" } graphviz = { path = "../libgraphviz" } jobserver = "0.1" scoped-tls = "1.0" +lazy_static = "1.3" log = { version = "0.4", features = ["release_max_level_info", "std"] } rustc-rayon = "0.3.0" rustc-rayon-core = "0.3.0" diff --git a/src/librustc/mir/interpret/error.rs b/src/librustc/mir/interpret/error.rs index 349dbd74ad1c9..5199aca311990 100644 --- a/src/librustc/mir/interpret/error.rs +++ b/src/librustc/mir/interpret/error.rs @@ -223,7 +223,11 @@ impl From for InterpErrorInfo<'tcx> { impl<'tcx> From> for InterpErrorInfo<'tcx> { fn from(kind: InterpError<'tcx>) -> Self { - let backtrace = match env::var("RUSTC_CTFE_BACKTRACE") { + lazy_static::lazy_static! { + static ref ENV_VAR: Result = env::var("RUSTC_CTFE_BACKTRACE"); + } + + let backtrace = match *ENV_VAR { // Matching `RUST_BACKTRACE` -- we treat "0" the same as "not present". Ok(ref val) if val != "0" => { let mut backtrace = Backtrace::new_unresolved();