Skip to content

Commit d64f875

Browse files
committed
perf(cache): remove package.json index arena indirection
1 parent 9705e35 commit d64f875

File tree

2 files changed

+5
-23
lines changed

2 files changed

+5
-23
lines changed

src/cache/cache_impl.rs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use cfg_if::cfg_if;
1111
#[cfg(feature = "yarn_pnp")]
1212
use once_cell::sync::OnceCell;
1313
use papaya::{HashMap, HashSet};
14-
use parking_lot::RwLock;
1514
use rustc_hash::FxHasher;
1615

1716
use super::borrowed_path::BorrowedCachedPath;
@@ -22,14 +21,11 @@ use crate::{
2221
context::ResolveContext as Ctx, path::PathUtil,
2322
};
2423

25-
pub type PackageJsonIndex = usize;
26-
2724
/// Cache implementation used for caching filesystem access.
2825
#[derive(Default)]
2926
pub struct Cache<Fs> {
3027
pub(crate) fs: Fs,
3128
pub(crate) paths: HashSet<CachedPath, BuildHasherDefault<IdentityHasher>>,
32-
pub(crate) package_jsons: RwLock<Vec<Arc<PackageJson>>>,
3329
/// Cache for raw/unbuilt tsconfigs (used when extending).
3430
pub(crate) tsconfigs_raw: HashMap<PathBuf, Arc<TsConfig>, BuildHasherDefault<FxHasher>>,
3531
/// Cache for built/resolved tsconfigs (used for resolution).
@@ -43,7 +39,6 @@ impl<Fs: FileSystem> Cache<Fs> {
4339
self.paths.pin().clear();
4440
self.tsconfigs_raw.pin().clear();
4541
self.tsconfigs_built.pin().clear();
46-
self.package_jsons.write().clear();
4742
}
4843

4944
#[allow(clippy::cast_possible_truncation)]
@@ -145,9 +140,7 @@ impl<Fs: FileSystem> Cache<Fs> {
145140
break;
146141
}
147142
}
148-
self.find_package_json_impl(&path, options, ctx).map(|option_index| {
149-
option_index.and_then(|index| self.package_jsons.read().get(index).cloned())
150-
})
143+
self.find_package_json_impl(&path, options, ctx)
151144
}
152145

153146
/// Find package.json of a path by traversing parent directories.
@@ -160,7 +153,7 @@ impl<Fs: FileSystem> Cache<Fs> {
160153
path: &CachedPath,
161154
options: &ResolveOptions,
162155
ctx: &mut Ctx,
163-
) -> Result<Option<PackageJsonIndex>, ResolveError> {
156+
) -> Result<Option<Arc<PackageJson>>, ResolveError> {
164157
// Change to `std::sync::OnceLock::get_or_try_init` when it is stable.
165158
path.package_json
166159
.get_or_try_init(|| {
@@ -184,16 +177,7 @@ impl<Fs: FileSystem> Cache<Fs> {
184177
real_path,
185178
package_json_bytes,
186179
)
187-
.map(|package_json| {
188-
let arc = Arc::new(package_json);
189-
let index = {
190-
let mut arena = self.package_jsons.write();
191-
let index = arena.len();
192-
arena.push(arc);
193-
index
194-
};
195-
Some(index)
196-
})
180+
.map(|package_json| Some(Arc::new(package_json)))
197181
.map_err(ResolveError::Json)
198182
// https://github.com/webpack/enhanced-resolve/blob/58464fc7cb56673c9aa849e68e6300239601e615/lib/DescriptionFileUtils.js#L68-L82
199183
.inspect(|_| {
@@ -306,7 +290,6 @@ impl<Fs: FileSystem> Cache<Fs> {
306290
.hasher(BuildHasherDefault::default())
307291
.resize_mode(papaya::ResizeMode::Blocking)
308292
.build(),
309-
package_jsons: RwLock::new(Vec::with_capacity(512)),
310293
tsconfigs_raw: HashMap::builder()
311294
.hasher(BuildHasherDefault::default())
312295
.resize_mode(papaya::ResizeMode::Blocking)

src/cache/cached_path.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ use cfg_if::cfg_if;
1111
use once_cell::sync::OnceCell as OnceLock;
1212

1313
use super::cache_impl::Cache;
14-
use super::cache_impl::PackageJsonIndex;
1514
use super::thread_local::SCRATCH_PATH;
16-
use crate::{FileSystem, TsConfig, context::ResolveContext as Ctx};
15+
use crate::{FileSystem, PackageJson, TsConfig, context::ResolveContext as Ctx};
1716

1817
#[derive(Clone)]
1918
pub struct CachedPath(pub Arc<CachedPathImpl>);
@@ -27,7 +26,7 @@ pub struct CachedPathImpl {
2726
pub meta: OnceLock<Option<(/* is_file */ bool, /* is_dir */ bool)>>, // None means not found.
2827
pub canonicalized: OnceLock<(Weak<Self>, PathBuf)>,
2928
pub node_modules: OnceLock<Option<Weak<Self>>>,
30-
pub package_json: OnceLock<Option<PackageJsonIndex>>,
29+
pub package_json: OnceLock<Option<Arc<PackageJson>>>,
3130
/// `tsconfig.json` found at path.
3231
pub tsconfig: OnceLock<Option<Arc<TsConfig>>>,
3332
/// `tsconfig.json` after resolving `references`, `files`, `include` and `extend`.

0 commit comments

Comments
 (0)