From 8a7d403fce79f974bde37d8840adb33b9b061910 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 6 Feb 2020 16:39:20 -0600 Subject: [PATCH] Remove a use of `lazy_static!` in `cache.rs` (#916) There's not really much reason to amortize the cost of this mtime calculation here since it's only done with debug assertions anyway, so let's avoid an extra dependency and just have a function do it inline. --- crates/environ/Cargo.toml | 2 +- crates/environ/src/cache.rs | 39 +++++++++++-------------------------- 2 files changed, 12 insertions(+), 29 deletions(-) diff --git a/crates/environ/Cargo.toml b/crates/environ/Cargo.toml index d4d8867c37..2f53823e64 100644 --- a/crates/environ/Cargo.toml +++ b/crates/environ/Cargo.toml @@ -26,7 +26,6 @@ sha2 = "0.8.0" base64 = "0.11.0" serde = { version = "1.0.94", features = ["derive"] } bincode = "1.1.4" -lazy_static = "1.3.0" log = { version = "0.4.8", default-features = false } zstd = "0.5" toml = "0.5.5" @@ -47,6 +46,7 @@ pretty_env_logger = "0.3.0" rand = { version = "0.7.0", default-features = false, features = ["small_rng"] } cranelift-codegen = { version = "0.56", features = ["enable-serde", "all-arch"] } filetime = "0.2.7" +lazy_static = "1.3.0" [badges] maintenance = { status = "actively-developed" } diff --git a/crates/environ/src/cache.rs b/crates/environ/src/cache.rs index 15a9a01c61..a8258054d7 100644 --- a/crates/environ/src/cache.rs +++ b/crates/environ/src/cache.rs @@ -5,7 +5,6 @@ use crate::module_environ::FunctionBodyData; use cranelift_codegen::{ir, isa}; use cranelift_entity::PrimaryMap; use cranelift_wasm::DefinedFuncIndex; -use lazy_static::lazy_static; use log::{debug, trace, warn}; use serde::{Deserialize, Serialize}; use sha2::{Digest, Sha256}; @@ -21,32 +20,6 @@ mod worker; pub use config::{create_new_config, CacheConfig}; use worker::Worker; -lazy_static! { - static ref SELF_MTIME: String = { - std::env::current_exe() - .map_err(|_| warn!("Failed to get path of current executable")) - .ok() - .and_then(|path| { - fs::metadata(&path) - .map_err(|_| warn!("Failed to get metadata of current executable")) - .ok() - }) - .and_then(|metadata| { - metadata - .modified() - .map_err(|_| warn!("Failed to get metadata of current executable")) - .ok() - }) - .map_or_else( - || "no-mtime".to_string(), - |mtime| match mtime.duration_since(std::time::UNIX_EPOCH) { - Ok(duration) => format!("{}", duration.as_millis()), - Err(err) => format!("m{}", err.duration().as_millis()), - }, - ) - }; -} - pub struct ModuleCacheEntry<'config>(Option>); struct ModuleCacheEntryInner<'config> { @@ -142,11 +115,21 @@ impl<'config> ModuleCacheEntryInner<'config> { ) -> Self { let hash = Sha256Hasher::digest(module, function_body_inputs); let compiler_dir = if cfg!(debug_assertions) { + fn self_mtime() -> Option { + let path = std::env::current_exe().ok()?; + let metadata = path.metadata().ok()?; + let mtime = metadata.modified().ok()?; + Some(match mtime.duration_since(std::time::UNIX_EPOCH) { + Ok(dur) => format!("{}", dur.as_millis()), + Err(err) => format!("m{}", err.duration().as_millis()), + }) + } + let self_mtime = self_mtime().unwrap_or("no-mtime".to_string()); format!( "{comp_name}-{comp_ver}-{comp_mtime}", comp_name = compiler_name, comp_ver = env!("GIT_REV"), - comp_mtime = *SELF_MTIME, + comp_mtime = self_mtime, ) } else { format!(