Skip to content

Commit

Permalink
perf(es): Cache current_dir() system calls (#9683)
Browse files Browse the repository at this point in the history
**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
  • Loading branch information
kdy1 authored Oct 29, 2024
1 parent 09de6f4 commit 7aab945
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
7 changes: 7 additions & 0 deletions .changeset/silent-cows-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
swc: patch
swc_common: patch
swc_core: patch
---

perf(es): Cache `current_dir()` system calls
4 changes: 3 additions & 1 deletion crates/swc/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf> = Lazy::new(|| ::std::env::current_dir().unwrap());

CWD.clone()
}

/// `.swcrc` file
Expand Down
12 changes: 9 additions & 3 deletions crates/swc_common/src/plugin/metadata.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -48,12 +50,16 @@ impl TransformPluginMetadataContext {
env: String,
experimental: Option<AHashMap<String, String>>,
) -> Self {
static CWD: Lazy<Option<String>> = 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(),
}
}
Expand Down

0 comments on commit 7aab945

Please sign in to comment.