From b3d70c5c7f8bf41dfb7f24e559bf5c47b1426e48 Mon Sep 17 00:00:00 2001 From: Boshen Date: Mon, 24 Jul 2023 00:15:42 +0800 Subject: [PATCH] refactor(resolver): make the global cache hold less memory (#593) --- crates/oxc_resolver/src/cache.rs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/crates/oxc_resolver/src/cache.rs b/crates/oxc_resolver/src/cache.rs index 88cc96731..d6ee392d4 100644 --- a/crates/oxc_resolver/src/cache.rs +++ b/crates/oxc_resolver/src/cache.rs @@ -11,7 +11,7 @@ use crate::{package_json::PackageJson, FileMetadata, FileSystem, ResolveError}; pub struct Cache { fs: Fs, - cache: DashMap, BuildHasherDefault>, + cache: DashMap, Arc, BuildHasherDefault>, } impl Default for Cache { @@ -41,7 +41,7 @@ impl Cache { } pub fn canonicalize(&self, path: &Path) -> Option { - self.cache_value(path).symlink(&self.fs) + self.cache_value(path).symlink(&self.fs).map(Path::into_path_buf) } /// Get package.json of the given path. @@ -74,23 +74,23 @@ impl Cache { return Arc::clone(cache_entry.value()); } let parent = path.parent().map(|p| self.cache_value(p)); - let data = Arc::new(CacheValue::new(path.to_path_buf(), parent)); - self.cache.insert(path.to_path_buf(), Arc::clone(&data)); + let data = Arc::new(CacheValue::new(path.to_path_buf().into_boxed_path(), parent)); + self.cache.insert(path.to_path_buf().into_boxed_path(), Arc::clone(&data)); data } } #[derive(Debug, Clone)] pub struct CacheValue { - path: PathBuf, + path: Box, parent: Option>, meta: OnceLock>, - symlink: OnceLock>, + symlink: OnceLock>>, package_json: OnceLock, ResolveError>>>, } impl CacheValue { - fn new(path: PathBuf, parent: Option>) -> Self { + fn new(path: Box, parent: Option>) -> Self { Self { path, parent, @@ -112,8 +112,10 @@ impl CacheValue { self.meta(fs).is_some_and(|meta| meta.is_dir) } - fn symlink(&self, fs: &Fs) -> Option { - self.symlink.get_or_init(|| fs.canonicalize(&self.path).ok()).clone() + fn symlink(&self, fs: &Fs) -> Option> { + self.symlink + .get_or_init(|| fs.canonicalize(&self.path).map(PathBuf::into_boxed_path).ok()) + .clone() } fn package_json(