Skip to content

Commit a1d806b

Browse files
authored
refactor: remove redundant PathBuf storage in CachedPath (#891)
1 parent d6a1e1f commit a1d806b

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

src/cache/cache_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ impl<Fs: FileSystem> Cache<Fs> {
5959
let is_node_modules = path.file_name().as_ref().is_some_and(|&name| name == "node_modules");
6060
let inside_node_modules =
6161
is_node_modules || parent.as_ref().is_some_and(|parent| parent.inside_node_modules);
62-
let parent_weak = parent.as_ref().map(|p| (Arc::downgrade(&p.0), p.to_path_buf()));
62+
let parent_weak = parent.as_ref().map(|p| Arc::downgrade(&p.0));
6363
let cached_path = CachedPath(Arc::new(CachedPathImpl::new(
6464
hash,
6565
path.to_path_buf().into_boxed_path(),

src/cache/cached_path.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ pub struct CachedPath(pub Arc<CachedPathImpl>);
2121
pub struct CachedPathImpl {
2222
pub hash: u64,
2323
pub path: Box<Path>,
24-
pub parent: Option<(Weak<CachedPathImpl>, PathBuf)>,
24+
pub parent: Option<Weak<CachedPathImpl>>,
2525
pub is_node_modules: bool,
2626
pub inside_node_modules: bool,
2727
pub meta: OnceLock<Option<(/* is_file */ bool, /* is_dir */ bool)>>, // None means not found.
2828
pub canonicalized: OnceLock<(Weak<CachedPathImpl>, PathBuf)>,
29-
pub node_modules: OnceLock<Option<(Weak<CachedPathImpl>, PathBuf)>>,
29+
pub node_modules: OnceLock<Option<Weak<CachedPathImpl>>>,
3030
pub package_json: OnceLock<Option<PackageJsonIndex>>,
3131
/// `tsconfig.json` found at path.
3232
pub tsconfig: OnceLock<Option<Arc<TsConfig>>>,
@@ -40,7 +40,7 @@ impl CachedPathImpl {
4040
path: Box<Path>,
4141
is_node_modules: bool,
4242
inside_node_modules: bool,
43-
parent: Option<(Weak<Self>, PathBuf)>,
43+
parent: Option<Weak<Self>>,
4444
) -> Self {
4545
Self {
4646
hash,
@@ -76,11 +76,11 @@ impl CachedPath {
7676
}
7777

7878
pub(crate) fn parent<Fs: FileSystem>(&self, cache: &Cache<Fs>) -> Option<Self> {
79-
self.0.parent.as_ref().and_then(|(weak, path_buf)| {
79+
self.0.parent.as_ref().and_then(|weak| {
8080
weak.upgrade().map(CachedPath).or_else(|| {
8181
// Weak pointer upgrade failed - parent was cleared from cache
82-
// Recreate it from the stored PathBuf
83-
Some(cache.value(path_buf))
82+
// Recreate it by deriving the parent path
83+
self.path().parent().map(|parent_path| cache.value(parent_path))
8484
})
8585
})
8686
}
@@ -110,14 +110,13 @@ impl CachedPath {
110110
) -> Option<Self> {
111111
self.node_modules
112112
.get_or_init(|| {
113-
self.module_directory("node_modules", cache, ctx)
114-
.map(|cp| (Arc::downgrade(&cp.0), cp.to_path_buf()))
113+
self.module_directory("node_modules", cache, ctx).map(|cp| Arc::downgrade(&cp.0))
115114
})
116115
.as_ref()
117-
.and_then(|(weak, path_buf)| {
116+
.and_then(|weak| {
118117
weak.upgrade().map(CachedPath).or_else(|| {
119-
// Weak pointer upgrade failed - recreate from stored PathBuf
120-
Some(cache.value(path_buf))
118+
// Weak pointer upgrade failed - recreate by deriving the node_modules path
119+
Some(self.push("node_modules", cache))
121120
})
122121
})
123122
}

0 commit comments

Comments
 (0)