Skip to content

Commit

Permalink
Remove a use of lazy_static! in cache.rs (#916)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
alexcrichton authored Feb 6, 2020
1 parent de85efc commit 8a7d403
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 29 deletions.
2 changes: 1 addition & 1 deletion crates/environ/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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" }
39 changes: 11 additions & 28 deletions crates/environ/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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<ModuleCacheEntryInner<'config>>);

struct ModuleCacheEntryInner<'config> {
Expand Down Expand Up @@ -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<String> {
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!(
Expand Down

0 comments on commit 8a7d403

Please sign in to comment.