From 7aab945a2199be06e20a35ec0d197fc817a48d9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Tue, 29 Oct 2024 11:53:29 +0900 Subject: [PATCH] perf(es): Cache `current_dir()` system calls (#9683) **Description:** Cache `current_dir()` system calls. Those are needless because we already cache `current_dir` in some places so it will break if the user changes cwd anyway. **Related issue:** - https://github.com/swc-project/swc/issues/9601 --- .changeset/silent-cows-brake.md | 7 +++++++ crates/swc/src/config/mod.rs | 4 +++- crates/swc_common/src/plugin/metadata.rs | 12 +++++++++--- 3 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 .changeset/silent-cows-brake.md diff --git a/.changeset/silent-cows-brake.md b/.changeset/silent-cows-brake.md new file mode 100644 index 00000000000..7e083c0549b --- /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 39c2fd92956..7d60e2efba3 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 2f608e714e8..a4d6ed7160f 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(), } }