Skip to content

Commit 7aab945

Browse files
authored
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:** - #9601
1 parent 09de6f4 commit 7aab945

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

.changeset/silent-cows-brake.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
swc: patch
3+
swc_common: patch
4+
swc_core: patch
5+
---
6+
7+
perf(es): Cache `current_dir()` system calls

crates/swc/src/config/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,9 @@ pub struct CallerOptions {
842842

843843
#[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"))))]
844844
fn default_cwd() -> PathBuf {
845-
::std::env::current_dir().unwrap()
845+
static CWD: Lazy<PathBuf> = Lazy::new(|| ::std::env::current_dir().unwrap());
846+
847+
CWD.clone()
846848
}
847849

848850
/// `.swcrc` file

crates/swc_common/src/plugin/metadata.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use std::env;
22

3+
use once_cell::sync::Lazy;
4+
35
use crate::collections::AHashMap;
46

57
/// Indexable key to the metadata context for a transform plugin.
@@ -48,12 +50,16 @@ impl TransformPluginMetadataContext {
4850
env: String,
4951
experimental: Option<AHashMap<String, String>>,
5052
) -> Self {
53+
static CWD: Lazy<Option<String>> = Lazy::new(|| {
54+
env::current_dir()
55+
.map(|cwd| cwd.as_path().to_string_lossy().to_string())
56+
.ok()
57+
});
58+
5159
TransformPluginMetadataContext {
5260
filename,
5361
env,
54-
cwd: env::current_dir()
55-
.map(|cwd| cwd.as_path().to_string_lossy().to_string())
56-
.ok(),
62+
cwd: CWD.clone(),
5763
experimental: experimental.unwrap_or_default(),
5864
}
5965
}

0 commit comments

Comments
 (0)