perf(common): Avoid string re-allocation (#2318)

This commit is contained in:
LongYinan 2021-09-30 13:50:25 +08:00 committed by GitHub
parent 6a41e9a0be
commit fee270fe57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 6 deletions

View File

@ -6,7 +6,7 @@ edition = "2018"
license = "Apache-2.0/MIT"
name = "swc_common"
repository = "https://github.com/swc-project/swc.git"
version = "0.13.1"
version = "0.13.2"
[features]
concurrent = ["parking_lot"]

View File

@ -23,6 +23,7 @@ use crate::{
rustc_data_structures::stable_hasher::StableHasher,
sync::{Lock, LockGuard, Lrc, MappedLockGuard},
};
use once_cell::sync::Lazy;
#[cfg(feature = "sourcemap")]
use sourcemap::SourceMapBuilder;
use std::{
@ -30,12 +31,14 @@ use std::{
cmp::{max, min},
env, fs,
hash::Hash,
io::{self, Read},
io,
path::{Path, PathBuf},
sync::atomic::{AtomicUsize, Ordering::SeqCst},
};
use tracing::debug;
static CURRENT_DIR: Lazy<Option<PathBuf>> = Lazy::new(|| env::current_dir().ok());
// _____________________________________________________________________________
// SourceFile, MultiByteChar, FileName, FileLines
//
@ -64,14 +67,12 @@ impl FileLoader for RealFileLoader {
if path.is_absolute() {
Some(path.to_path_buf())
} else {
env::current_dir().ok().map(|cwd| cwd.join(path))
CURRENT_DIR.as_ref().map(|cwd| cwd.join(path))
}
}
fn read_file(&self, path: &Path) -> io::Result<String> {
let mut src = String::new();
fs::File::open(path)?.read_to_string(&mut src)?;
Ok(src)
fs::read_to_string(path)
}
}