perf(es): Cache current_dir() system calls (#9683)
Some checks are pending
CI / Cargo fmt (push) Waiting to run
CI / Cargo clippy (push) Waiting to run
CI / Check license of dependencies (push) Waiting to run
CI / Check (macos-latest) (push) Waiting to run
CI / Check (ubuntu-latest) (push) Waiting to run
CI / Check (windows-latest) (push) Waiting to run
CI / Test wasm (binding_core_wasm) (push) Waiting to run
CI / Test wasm (binding_minifier_wasm) (push) Waiting to run
CI / Test wasm (binding_typescript_wasm) (push) Waiting to run
CI / List crates (push) Waiting to run
CI / Test - ${{ matrix.settings.crate }} - ${{ matrix.settings.os }} (push) Blocked by required conditions
CI / Test node bindings - ${{ matrix.os }} (macos-latest) (push) Waiting to run
CI / Test node bindings - ${{ matrix.os }} (windows-latest) (push) Waiting to run
CI / Test with @swc/cli (push) Waiting to run
CI / Miri (better_scoped_tls) (push) Waiting to run
CI / Miri (string_enum) (push) Waiting to run
CI / Miri (swc) (push) Waiting to run
CI / Miri (swc_bundler) (push) Waiting to run
CI / Done (push) Blocked by required conditions
Benchmark / Bench everything (push) Waiting to run
Publish crates (auto) / Publish cargo crates (push) Waiting to run

**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
This commit is contained in:
Donny/강동윤 2024-10-29 11:53:29 +09:00 committed by GitHub
parent 09de6f4065
commit 7aab945a21
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 19 additions and 4 deletions

View File

@ -0,0 +1,7 @@
---
swc: patch
swc_common: patch
swc_core: patch
---
perf(es): Cache `current_dir()` system calls

View File

@ -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

View File

@ -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<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(),
}
}