diff --git a/.changeset/silent-cows-brake.md b/.changeset/silent-cows-brake.md new file mode 100644 index 000000000000..7e083c0549bc --- /dev/null +++ b/.changeset/silent-cows-brake.md @@ -0,0 +1,7 @@ +--- +swc: patch +swc_common: patch +swc_core: patch +--- + +perf(es): Cache `current_dir()` system calls diff --git a/crates/swc/src/config/mod.rs b/crates/swc/src/config/mod.rs index 39c2fd929560..7d60e2efba30 100644 --- a/crates/swc/src/config/mod.rs +++ b/crates/swc/src/config/mod.rs @@ -842,7 +842,9 @@ pub struct CallerOptions { #[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"))))] fn default_cwd() -> PathBuf { - ::std::env::current_dir().unwrap() + static CWD: Lazy = Lazy::new(|| ::std::env::current_dir().unwrap()); + + CWD.clone() } /// `.swcrc` file diff --git a/crates/swc_common/src/plugin/metadata.rs b/crates/swc_common/src/plugin/metadata.rs index 2f608e714e82..a4d6ed7160fd 100644 --- a/crates/swc_common/src/plugin/metadata.rs +++ b/crates/swc_common/src/plugin/metadata.rs @@ -1,5 +1,7 @@ use std::env; +use once_cell::sync::Lazy; + use crate::collections::AHashMap; /// Indexable key to the metadata context for a transform plugin. @@ -48,12 +50,16 @@ impl TransformPluginMetadataContext { env: String, experimental: Option>, ) -> Self { + static CWD: Lazy> = Lazy::new(|| { + env::current_dir() + .map(|cwd| cwd.as_path().to_string_lossy().to_string()) + .ok() + }); + TransformPluginMetadataContext { filename, env, - cwd: env::current_dir() - .map(|cwd| cwd.as_path().to_string_lossy().to_string()) - .ok(), + cwd: CWD.clone(), experimental: experimental.unwrap_or_default(), } }