Skip to content

Commit 9be837a

Browse files
elbywanstormslowly
andauthored
fix(pnp): cache the manifest by its own path (#69)
Co-authored-by: pshu <stormslowly@gmail.com>
1 parent 5974ee3 commit 9be837a

1 file changed

Lines changed: 28 additions & 8 deletions

File tree

src/lib.rs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ pub struct ResolverGeneric<Fs> {
116116
options: ResolveOptions,
117117
cache: Arc<Cache<Fs>>,
118118
#[cfg(feature = "yarn_pnp")]
119-
pnp_cache: Arc<DashMap<CachedPath, Option<pnp::Manifest>>>,
119+
pnp_manifest_content_cache: Arc<DashMap<CachedPath, Option<pnp::Manifest>>>,
120+
#[cfg(feature = "yarn_pnp")]
121+
pnp_manifest_path_cache: Arc<DashMap<PathBuf, Option<CachedPath>>>,
120122
}
121123

122124
impl<Fs> fmt::Debug for ResolverGeneric<Fs> {
@@ -137,7 +139,9 @@ impl<Fs: Send + Sync + FileSystem + Default> ResolverGeneric<Fs> {
137139
options: options.sanitize(),
138140
cache: Arc::new(Cache::new(Fs::default())),
139141
#[cfg(feature = "yarn_pnp")]
140-
pnp_cache: Arc::new(DashMap::default()),
142+
pnp_manifest_content_cache: Arc::new(DashMap::default()),
143+
#[cfg(feature = "yarn_pnp")]
144+
pnp_manifest_path_cache: Arc::new(DashMap::default()),
141145
}
142146
}
143147
}
@@ -148,7 +152,9 @@ impl<Fs: FileSystem + Send + Sync> ResolverGeneric<Fs> {
148152
options: options.sanitize(),
149153
cache: Arc::new(Cache::new(file_system)),
150154
#[cfg(feature = "yarn_pnp")]
151-
pnp_cache: Arc::new(DashMap::default()),
155+
pnp_manifest_content_cache: Arc::new(DashMap::default()),
156+
#[cfg(feature = "yarn_pnp")]
157+
pnp_manifest_path_cache: Arc::new(DashMap::default()),
152158
}
153159
}
154160

@@ -159,7 +165,9 @@ impl<Fs: FileSystem + Send + Sync> ResolverGeneric<Fs> {
159165
options: options.sanitize(),
160166
cache: Arc::clone(&self.cache),
161167
#[cfg(feature = "yarn_pnp")]
162-
pnp_cache: Arc::clone(&self.pnp_cache),
168+
pnp_manifest_content_cache: Arc::clone(&self.pnp_manifest_content_cache),
169+
#[cfg(feature = "yarn_pnp")]
170+
pnp_manifest_path_cache: Arc::clone(&self.pnp_manifest_path_cache),
163171
}
164172
}
165173

@@ -172,7 +180,10 @@ impl<Fs: FileSystem + Send + Sync> ResolverGeneric<Fs> {
172180
pub fn clear_cache(&self) {
173181
self.cache.clear();
174182
#[cfg(feature = "yarn_pnp")]
175-
self.pnp_cache.clear();
183+
{
184+
self.pnp_manifest_content_cache.clear();
185+
self.pnp_manifest_path_cache.clear();
186+
}
176187
}
177188

178189
/// Resolve `specifier` at an absolute path to a `directory`.
@@ -801,10 +812,19 @@ impl<Fs: FileSystem + Send + Sync> ResolverGeneric<Fs> {
801812
&self,
802813
cached_path: &CachedPath,
803814
) -> Ref<'_, CachedPath, Option<pnp::Manifest>> {
815+
let base_path = cached_path.to_path_buf();
816+
817+
let cached_manifest_path =
818+
self.pnp_manifest_path_cache.entry(base_path.clone()).or_insert_with(|| {
819+
pnp::find_closest_pnp_manifest_path(base_path).map(|p| self.cache.value(&p))
820+
});
821+
822+
let cache_key = cached_manifest_path.as_ref().unwrap_or(cached_path);
823+
804824
let entry = self
805-
.pnp_cache
806-
.entry(cached_path.clone())
807-
.or_insert_with(|| pnp::find_pnp_manifest(cached_path.path()).unwrap());
825+
.pnp_manifest_content_cache
826+
.entry(cache_key.clone())
827+
.or_insert_with(|| pnp::load_pnp_manifest(cache_key.path()).ok());
808828

809829
entry.downgrade()
810830
}

0 commit comments

Comments
 (0)